2010-01-29 15:20:15 -05:00
|
|
|
module HTTParty
|
|
|
|
module StubResponse
|
|
|
|
def stub_http_response_with(filename)
|
|
|
|
format = filename.split('.').last.intern
|
|
|
|
data = file_fixture(filename)
|
|
|
|
|
|
|
|
response = Net::HTTPOK.new("1.1", 200, "Content for you")
|
2014-12-06 19:12:39 -05:00
|
|
|
allow(response).to receive(:body).and_return(data)
|
2010-01-29 15:20:15 -05:00
|
|
|
|
2014-05-15 16:45:32 -04:00
|
|
|
http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', format: format)
|
2014-12-06 19:12:39 -05:00
|
|
|
allow(http_request).to receive_message_chain(:http, :request).and_return(response)
|
2010-01-29 15:20:15 -05:00
|
|
|
|
2014-12-06 19:12:39 -05:00
|
|
|
expect(HTTParty::Request).to receive(:new).and_return(http_request)
|
2010-01-29 15:20:15 -05:00
|
|
|
end
|
|
|
|
|
2015-04-18 06:27:50 -04:00
|
|
|
def stub_chunked_http_response_with(chunks, options = {format: "html"})
|
2012-02-15 14:46:01 -05:00
|
|
|
response = Net::HTTPResponse.new("1.1", 200, nil)
|
2014-12-06 19:12:39 -05:00
|
|
|
allow(response).to receive(:chunked_data).and_return(chunks)
|
2012-02-15 14:46:01 -05:00
|
|
|
def response.read_body(&block)
|
|
|
|
@body || chunked_data.each(&block)
|
|
|
|
end
|
|
|
|
|
2014-11-03 15:53:06 -05:00
|
|
|
http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', options)
|
2014-12-06 19:12:39 -05:00
|
|
|
allow(http_request).to receive_message_chain(:http, :request).and_yield(response).and_return(response)
|
2012-02-15 14:46:01 -05:00
|
|
|
|
2014-12-06 19:12:39 -05:00
|
|
|
expect(HTTParty::Request).to receive(:new).and_return(http_request)
|
2012-02-15 14:46:01 -05:00
|
|
|
end
|
|
|
|
|
2010-01-29 15:20:15 -05:00
|
|
|
def stub_response(body, code = 200)
|
2011-01-18 15:36:56 -05:00
|
|
|
@request.options[:base_uri] ||= 'http://localhost'
|
2010-05-04 23:09:28 -04:00
|
|
|
unless defined?(@http) && @http
|
2010-01-29 15:20:15 -05:00
|
|
|
@http = Net::HTTP.new('localhost', 80)
|
2014-12-06 19:12:39 -05:00
|
|
|
allow(@request).to receive(:http).and_return(@http)
|
2010-01-29 15:20:15 -05:00
|
|
|
end
|
|
|
|
|
2015-07-09 10:02:18 -04:00
|
|
|
# CODE_TO_OBJ currently missing 308
|
|
|
|
if code.to_s == '308'
|
|
|
|
response = Net::HTTPRedirection.new("1.1", code, body)
|
|
|
|
else
|
|
|
|
response = Net::HTTPResponse::CODE_TO_OBJ[code.to_s].new("1.1", code, body)
|
|
|
|
end
|
2014-12-06 19:12:39 -05:00
|
|
|
allow(response).to receive(:body).and_return(body)
|
2010-01-29 15:20:15 -05:00
|
|
|
|
2014-12-06 19:12:39 -05:00
|
|
|
allow(@http).to receive(:request).and_return(response)
|
2010-01-29 15:20:15 -05:00
|
|
|
response
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|