1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00
httparty/spec/support/stub_response.rb

51 lines
1.8 KiB
Ruby
Raw Normal View History

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")
Convert specs to RSpec 3.1.7 syntax with Transpec This conversion is done by Transpec 2.3.8 with the following command: transpec -f spec * 328 conversions from: obj.should to: expect(obj).to * 238 conversions from: == expected to: eq(expected) * 42 conversions from: obj.should_receive(:message) to: expect(obj).to receive(:message) * 26 conversions from: it { should ... } to: it { is_expected.to ... } * 26 conversions from: lambda { }.should to: expect { }.to * 21 conversions from: mock('something') to: double('something') * 21 conversions from: obj.stub(:message => value) to: allow(obj).to receive_messages(:message => value) * 19 conversions from: be_true to: be_truthy * 19 conversions from: its(:attr) { } to: describe '#attr' do subject { super().attr }; it { } end * 18 conversions from: obj.stub!(:message) to: allow(obj).to receive(:message) * 15 conversions from: obj.should_not_receive(:message) to: expect(obj).not_to receive(:message) * 10 conversions from: obj.should_not to: expect(obj).not_to * 7 conversions from: obj.stub(:message) to: allow(obj).to receive(:message) * 6 conversions from: stub('something') to: double('something') * 5 conversions from: be_false to: be_falsey * 4 conversions from: expect { }.not_to raise_error(SpecificErrorClass) to: expect { }.not_to raise_error * 4 conversions from: lambda { }.should_not to: expect { }.not_to * 3 conversions from: =~ /pattern/ to: match(/pattern/) * 2 conversions from: obj.stub_chain(:message1, :message2) to: allow(obj).to receive_message_chain(:message1, :message2) * 1 conversion from: === expected to: be === expected * 1 conversion from: it { should_not ... } to: it { is_expected.not_to ... } For more details: https://github.com/yujinakayama/transpec#supported-conversions
2014-12-06 19:12:39 -05:00
allow(response).to receive(:body).and_return(data)
http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', format: format)
Convert specs to RSpec 3.1.7 syntax with Transpec This conversion is done by Transpec 2.3.8 with the following command: transpec -f spec * 328 conversions from: obj.should to: expect(obj).to * 238 conversions from: == expected to: eq(expected) * 42 conversions from: obj.should_receive(:message) to: expect(obj).to receive(:message) * 26 conversions from: it { should ... } to: it { is_expected.to ... } * 26 conversions from: lambda { }.should to: expect { }.to * 21 conversions from: mock('something') to: double('something') * 21 conversions from: obj.stub(:message => value) to: allow(obj).to receive_messages(:message => value) * 19 conversions from: be_true to: be_truthy * 19 conversions from: its(:attr) { } to: describe '#attr' do subject { super().attr }; it { } end * 18 conversions from: obj.stub!(:message) to: allow(obj).to receive(:message) * 15 conversions from: obj.should_not_receive(:message) to: expect(obj).not_to receive(:message) * 10 conversions from: obj.should_not to: expect(obj).not_to * 7 conversions from: obj.stub(:message) to: allow(obj).to receive(:message) * 6 conversions from: stub('something') to: double('something') * 5 conversions from: be_false to: be_falsey * 4 conversions from: expect { }.not_to raise_error(SpecificErrorClass) to: expect { }.not_to raise_error * 4 conversions from: lambda { }.should_not to: expect { }.not_to * 3 conversions from: =~ /pattern/ to: match(/pattern/) * 2 conversions from: obj.stub_chain(:message1, :message2) to: allow(obj).to receive_message_chain(:message1, :message2) * 1 conversion from: === expected to: be === expected * 1 conversion from: it { should_not ... } to: it { is_expected.not_to ... } For more details: https://github.com/yujinakayama/transpec#supported-conversions
2014-12-06 19:12:39 -05:00
allow(http_request).to receive_message_chain(:http, :request).and_return(response)
Convert specs to RSpec 3.1.7 syntax with Transpec This conversion is done by Transpec 2.3.8 with the following command: transpec -f spec * 328 conversions from: obj.should to: expect(obj).to * 238 conversions from: == expected to: eq(expected) * 42 conversions from: obj.should_receive(:message) to: expect(obj).to receive(:message) * 26 conversions from: it { should ... } to: it { is_expected.to ... } * 26 conversions from: lambda { }.should to: expect { }.to * 21 conversions from: mock('something') to: double('something') * 21 conversions from: obj.stub(:message => value) to: allow(obj).to receive_messages(:message => value) * 19 conversions from: be_true to: be_truthy * 19 conversions from: its(:attr) { } to: describe '#attr' do subject { super().attr }; it { } end * 18 conversions from: obj.stub!(:message) to: allow(obj).to receive(:message) * 15 conversions from: obj.should_not_receive(:message) to: expect(obj).not_to receive(:message) * 10 conversions from: obj.should_not to: expect(obj).not_to * 7 conversions from: obj.stub(:message) to: allow(obj).to receive(:message) * 6 conversions from: stub('something') to: double('something') * 5 conversions from: be_false to: be_falsey * 4 conversions from: expect { }.not_to raise_error(SpecificErrorClass) to: expect { }.not_to raise_error * 4 conversions from: lambda { }.should_not to: expect { }.not_to * 3 conversions from: =~ /pattern/ to: match(/pattern/) * 2 conversions from: obj.stub_chain(:message1, :message2) to: allow(obj).to receive_message_chain(:message1, :message2) * 1 conversion from: === expected to: be === expected * 1 conversion from: it { should_not ... } to: it { is_expected.not_to ... } For more details: https://github.com/yujinakayama/transpec#supported-conversions
2014-12-06 19:12:39 -05:00
expect(HTTParty::Request).to receive(:new).and_return(http_request)
end
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)
Convert specs to RSpec 3.1.7 syntax with Transpec This conversion is done by Transpec 2.3.8 with the following command: transpec -f spec * 328 conversions from: obj.should to: expect(obj).to * 238 conversions from: == expected to: eq(expected) * 42 conversions from: obj.should_receive(:message) to: expect(obj).to receive(:message) * 26 conversions from: it { should ... } to: it { is_expected.to ... } * 26 conversions from: lambda { }.should to: expect { }.to * 21 conversions from: mock('something') to: double('something') * 21 conversions from: obj.stub(:message => value) to: allow(obj).to receive_messages(:message => value) * 19 conversions from: be_true to: be_truthy * 19 conversions from: its(:attr) { } to: describe '#attr' do subject { super().attr }; it { } end * 18 conversions from: obj.stub!(:message) to: allow(obj).to receive(:message) * 15 conversions from: obj.should_not_receive(:message) to: expect(obj).not_to receive(:message) * 10 conversions from: obj.should_not to: expect(obj).not_to * 7 conversions from: obj.stub(:message) to: allow(obj).to receive(:message) * 6 conversions from: stub('something') to: double('something') * 5 conversions from: be_false to: be_falsey * 4 conversions from: expect { }.not_to raise_error(SpecificErrorClass) to: expect { }.not_to raise_error * 4 conversions from: lambda { }.should_not to: expect { }.not_to * 3 conversions from: =~ /pattern/ to: match(/pattern/) * 2 conversions from: obj.stub_chain(:message1, :message2) to: allow(obj).to receive_message_chain(:message1, :message2) * 1 conversion from: === expected to: be === expected * 1 conversion from: it { should_not ... } to: it { is_expected.not_to ... } For more details: https://github.com/yujinakayama/transpec#supported-conversions
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
yield(response) if block_given?
2012-02-15 14:46:01 -05:00
http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', options)
Convert specs to RSpec 3.1.7 syntax with Transpec This conversion is done by Transpec 2.3.8 with the following command: transpec -f spec * 328 conversions from: obj.should to: expect(obj).to * 238 conversions from: == expected to: eq(expected) * 42 conversions from: obj.should_receive(:message) to: expect(obj).to receive(:message) * 26 conversions from: it { should ... } to: it { is_expected.to ... } * 26 conversions from: lambda { }.should to: expect { }.to * 21 conversions from: mock('something') to: double('something') * 21 conversions from: obj.stub(:message => value) to: allow(obj).to receive_messages(:message => value) * 19 conversions from: be_true to: be_truthy * 19 conversions from: its(:attr) { } to: describe '#attr' do subject { super().attr }; it { } end * 18 conversions from: obj.stub!(:message) to: allow(obj).to receive(:message) * 15 conversions from: obj.should_not_receive(:message) to: expect(obj).not_to receive(:message) * 10 conversions from: obj.should_not to: expect(obj).not_to * 7 conversions from: obj.stub(:message) to: allow(obj).to receive(:message) * 6 conversions from: stub('something') to: double('something') * 5 conversions from: be_false to: be_falsey * 4 conversions from: expect { }.not_to raise_error(SpecificErrorClass) to: expect { }.not_to raise_error * 4 conversions from: lambda { }.should_not to: expect { }.not_to * 3 conversions from: =~ /pattern/ to: match(/pattern/) * 2 conversions from: obj.stub_chain(:message1, :message2) to: allow(obj).to receive_message_chain(:message1, :message2) * 1 conversion from: === expected to: be === expected * 1 conversion from: it { should_not ... } to: it { is_expected.not_to ... } For more details: https://github.com/yujinakayama/transpec#supported-conversions
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
Convert specs to RSpec 3.1.7 syntax with Transpec This conversion is done by Transpec 2.3.8 with the following command: transpec -f spec * 328 conversions from: obj.should to: expect(obj).to * 238 conversions from: == expected to: eq(expected) * 42 conversions from: obj.should_receive(:message) to: expect(obj).to receive(:message) * 26 conversions from: it { should ... } to: it { is_expected.to ... } * 26 conversions from: lambda { }.should to: expect { }.to * 21 conversions from: mock('something') to: double('something') * 21 conversions from: obj.stub(:message => value) to: allow(obj).to receive_messages(:message => value) * 19 conversions from: be_true to: be_truthy * 19 conversions from: its(:attr) { } to: describe '#attr' do subject { super().attr }; it { } end * 18 conversions from: obj.stub!(:message) to: allow(obj).to receive(:message) * 15 conversions from: obj.should_not_receive(:message) to: expect(obj).not_to receive(:message) * 10 conversions from: obj.should_not to: expect(obj).not_to * 7 conversions from: obj.stub(:message) to: allow(obj).to receive(:message) * 6 conversions from: stub('something') to: double('something') * 5 conversions from: be_false to: be_falsey * 4 conversions from: expect { }.not_to raise_error(SpecificErrorClass) to: expect { }.not_to raise_error * 4 conversions from: lambda { }.should_not to: expect { }.not_to * 3 conversions from: =~ /pattern/ to: match(/pattern/) * 2 conversions from: obj.stub_chain(:message1, :message2) to: allow(obj).to receive_message_chain(:message1, :message2) * 1 conversion from: === expected to: be === expected * 1 conversion from: it { should_not ... } to: it { is_expected.not_to ... } For more details: https://github.com/yujinakayama/transpec#supported-conversions
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
def stub_response(body, code = '200')
code = code.to_s
@request.options[:base_uri] ||= 'http://localhost'
2010-05-04 23:09:28 -04:00
unless defined?(@http) && @http
@http = Net::HTTP.new('localhost', 80)
Convert specs to RSpec 3.1.7 syntax with Transpec This conversion is done by Transpec 2.3.8 with the following command: transpec -f spec * 328 conversions from: obj.should to: expect(obj).to * 238 conversions from: == expected to: eq(expected) * 42 conversions from: obj.should_receive(:message) to: expect(obj).to receive(:message) * 26 conversions from: it { should ... } to: it { is_expected.to ... } * 26 conversions from: lambda { }.should to: expect { }.to * 21 conversions from: mock('something') to: double('something') * 21 conversions from: obj.stub(:message => value) to: allow(obj).to receive_messages(:message => value) * 19 conversions from: be_true to: be_truthy * 19 conversions from: its(:attr) { } to: describe '#attr' do subject { super().attr }; it { } end * 18 conversions from: obj.stub!(:message) to: allow(obj).to receive(:message) * 15 conversions from: obj.should_not_receive(:message) to: expect(obj).not_to receive(:message) * 10 conversions from: obj.should_not to: expect(obj).not_to * 7 conversions from: obj.stub(:message) to: allow(obj).to receive(:message) * 6 conversions from: stub('something') to: double('something') * 5 conversions from: be_false to: be_falsey * 4 conversions from: expect { }.not_to raise_error(SpecificErrorClass) to: expect { }.not_to raise_error * 4 conversions from: lambda { }.should_not to: expect { }.not_to * 3 conversions from: =~ /pattern/ to: match(/pattern/) * 2 conversions from: obj.stub_chain(:message1, :message2) to: allow(obj).to receive_message_chain(:message1, :message2) * 1 conversion from: === expected to: be === expected * 1 conversion from: it { should_not ... } to: it { is_expected.not_to ... } For more details: https://github.com/yujinakayama/transpec#supported-conversions
2014-12-06 19:12:39 -05:00
allow(@request).to receive(:http).and_return(@http)
end
# CODE_TO_OBJ currently missing 308
if code == '308'
response = Net::HTTPRedirection.new("1.1", code, body)
else
response = Net::HTTPResponse::CODE_TO_OBJ[code].new("1.1", code, body)
end
Convert specs to RSpec 3.1.7 syntax with Transpec This conversion is done by Transpec 2.3.8 with the following command: transpec -f spec * 328 conversions from: obj.should to: expect(obj).to * 238 conversions from: == expected to: eq(expected) * 42 conversions from: obj.should_receive(:message) to: expect(obj).to receive(:message) * 26 conversions from: it { should ... } to: it { is_expected.to ... } * 26 conversions from: lambda { }.should to: expect { }.to * 21 conversions from: mock('something') to: double('something') * 21 conversions from: obj.stub(:message => value) to: allow(obj).to receive_messages(:message => value) * 19 conversions from: be_true to: be_truthy * 19 conversions from: its(:attr) { } to: describe '#attr' do subject { super().attr }; it { } end * 18 conversions from: obj.stub!(:message) to: allow(obj).to receive(:message) * 15 conversions from: obj.should_not_receive(:message) to: expect(obj).not_to receive(:message) * 10 conversions from: obj.should_not to: expect(obj).not_to * 7 conversions from: obj.stub(:message) to: allow(obj).to receive(:message) * 6 conversions from: stub('something') to: double('something') * 5 conversions from: be_false to: be_falsey * 4 conversions from: expect { }.not_to raise_error(SpecificErrorClass) to: expect { }.not_to raise_error * 4 conversions from: lambda { }.should_not to: expect { }.not_to * 3 conversions from: =~ /pattern/ to: match(/pattern/) * 2 conversions from: obj.stub_chain(:message1, :message2) to: allow(obj).to receive_message_chain(:message1, :message2) * 1 conversion from: === expected to: be === expected * 1 conversion from: it { should_not ... } to: it { is_expected.not_to ... } For more details: https://github.com/yujinakayama/transpec#supported-conversions
2014-12-06 19:12:39 -05:00
allow(response).to receive(:body).and_return(body)
Convert specs to RSpec 3.1.7 syntax with Transpec This conversion is done by Transpec 2.3.8 with the following command: transpec -f spec * 328 conversions from: obj.should to: expect(obj).to * 238 conversions from: == expected to: eq(expected) * 42 conversions from: obj.should_receive(:message) to: expect(obj).to receive(:message) * 26 conversions from: it { should ... } to: it { is_expected.to ... } * 26 conversions from: lambda { }.should to: expect { }.to * 21 conversions from: mock('something') to: double('something') * 21 conversions from: obj.stub(:message => value) to: allow(obj).to receive_messages(:message => value) * 19 conversions from: be_true to: be_truthy * 19 conversions from: its(:attr) { } to: describe '#attr' do subject { super().attr }; it { } end * 18 conversions from: obj.stub!(:message) to: allow(obj).to receive(:message) * 15 conversions from: obj.should_not_receive(:message) to: expect(obj).not_to receive(:message) * 10 conversions from: obj.should_not to: expect(obj).not_to * 7 conversions from: obj.stub(:message) to: allow(obj).to receive(:message) * 6 conversions from: stub('something') to: double('something') * 5 conversions from: be_false to: be_falsey * 4 conversions from: expect { }.not_to raise_error(SpecificErrorClass) to: expect { }.not_to raise_error * 4 conversions from: lambda { }.should_not to: expect { }.not_to * 3 conversions from: =~ /pattern/ to: match(/pattern/) * 2 conversions from: obj.stub_chain(:message1, :message2) to: allow(obj).to receive_message_chain(:message1, :message2) * 1 conversion from: === expected to: be === expected * 1 conversion from: it { should_not ... } to: it { is_expected.not_to ... } For more details: https://github.com/yujinakayama/transpec#supported-conversions
2014-12-06 19:12:39 -05:00
allow(@http).to receive(:request).and_return(response)
response
end
end
end