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

Replaces Fakeweb with Webmock in tests.

Fakeweb hasn't had a release in years and sadly no longer appears to be
maintained. As a result tests are failing on more modern Rubies (2.4.x).
This change replaces Fakeweb with Webmock, which is actively maintained.
This commit is contained in:
Jon Hope 2017-06-03 22:25:10 +01:00
parent 96d1c0a0f3
commit 647dbb69a5
5 changed files with 47 additions and 54 deletions

View file

@ -2,7 +2,6 @@ source 'https://rubygems.org'
gemspec gemspec
gem 'rake' gem 'rake'
gem 'fakeweb', '~> 1.3'
gem 'mongrel', '1.2.0.pre2' gem 'mongrel', '1.2.0.pre2'
group :development do group :development do
@ -16,4 +15,5 @@ group :test do
gem 'simplecov', require: false gem 'simplecov', require: false
gem 'aruba' gem 'aruba'
gem 'cucumber', '~> 2.3' gem 'cucumber', '~> 2.3'
gem 'webmock'
end end

View file

@ -160,29 +160,31 @@ RSpec.describe HTTParty::Request do
context 'digest_auth' do context 'digest_auth' do
before do before do
response_sequence = [ response_sequence = [
{status: ['401', 'Unauthorized' ], {status: ['401', 'Unauthorized' ], headers: {
www_authenticate: 'Digest realm="Log Viewer", qop="auth", nonce="2CA0EC6B0E126C4800E56BA0C0003D3C", opaque="5ccc069c403ebaf9f0171e9517f40e41", stale=false', www_authenticate: 'Digest realm="Log Viewer", qop="auth", nonce="2CA0EC6B0E126C4800E56BA0C0003D3C", opaque="5ccc069c403ebaf9f0171e9517f40e41", stale=false',
set_cookie: 'custom-cookie=1234567', set_cookie: 'custom-cookie=1234567',
}
}, },
{status: ['200', 'OK']} {status: ['200', 'OK']}
] ]
FakeWeb.register_uri(:get, "http://api.foo.com/v1", stub_request(:get, "http://api.foo.com/v1").and_return(response_sequence)
response_sequence)
end end
it 'should not send credentials more than once' do it 'should not send credentials more than once' do
response_sequence = [ response_sequence = [
{status: ['401', 'Unauthorized' ], {status: ['401', 'Unauthorized' ], headers: {
www_authenticate: 'Digest realm="Log Viewer", qop="auth", nonce="2CA0EC6B0E126C4800E56BA0C0003D3C", opaque="5ccc069c403ebaf9f0171e9517f40e41", stale=false', www_authenticate: 'Digest realm="Log Viewer", qop="auth", nonce="2CA0EC6B0E126C4800E56BA0C0003D3C", opaque="5ccc069c403ebaf9f0171e9517f40e41", stale=false',
set_cookie: 'custom-cookie=1234567', set_cookie: 'custom-cookie=1234567'
}
}, },
{status: ['401', 'Unauthorized' ], {status: ['401', 'Unauthorized' ], headers: {
www_authenticate: 'Digest realm="Log Viewer", qop="auth", nonce="2CA0EC6B0E126C4800E56BA0C0003D3C", opaque="5ccc069c403ebaf9f0171e9517f40e41", stale=false', www_authenticate: 'Digest realm="Log Viewer", qop="auth", nonce="2CA0EC6B0E126C4800E56BA0C0003D3C", opaque="5ccc069c403ebaf9f0171e9517f40e41", stale=false',
set_cookie: 'custom-cookie=1234567', set_cookie: 'custom-cookie=1234567'
}
}, },
{status: ['404', 'Not found']} {status: ['404', 'Not found']}
] ]
FakeWeb.register_uri(:get, "http://api.foo.com/v1", stub_request(:get, "http://api.foo.com/v1").and_return(
response_sequence) response_sequence)
@request.options[:digest_auth] = {username: 'foobar', password: 'secret'} @request.options[:digest_auth] = {username: 'foobar', password: 'secret'}
@ -194,8 +196,7 @@ RSpec.describe HTTParty::Request do
end end
it 'should not be used when configured and the response is 200' do it 'should not be used when configured and the response is 200' do
FakeWeb.register_uri(:get, "http://api.foo.com/v1", stub_request(:get, "http://api.foo.com/v1").and_return(status: 200)
status: 200)
@request.options[:digest_auth] = {username: 'foobar', password: 'secret'} @request.options[:digest_auth] = {username: 'foobar', password: 'secret'}
response = @request.perform { |v| } response = @request.perform { |v| }
expect(response.code).to eq(200) expect(response.code).to eq(200)
@ -243,7 +244,7 @@ RSpec.describe HTTParty::Request do
end end
it 'should normalize base uri when specified as request option' do it 'should normalize base uri when specified as request option' do
FakeWeb.register_uri(:get, 'http://foo.com/resource', :body => 'Bar') stub_request(:get, 'http://foo.com/resource').to_return(:body => 'Bar')
response = HTTParty.get('/resource', { response = HTTParty.get('/resource', {
base_uri: 'foo.com' base_uri: 'foo.com'
}) })
@ -568,8 +569,8 @@ RSpec.describe HTTParty::Request do
it "calls block given to perform with each redirect" do it "calls block given to perform with each redirect" do
@request = HTTParty::Request.new(Net::HTTP::Get, 'http://test.com/redirect', format: :xml) @request = HTTParty::Request.new(Net::HTTP::Get, 'http://test.com/redirect', format: :xml)
FakeWeb.register_uri(:get, "http://test.com/redirect", status: [300, "REDIRECT"], location: "http://api.foo.com/v2") stub_request(:get, "http://test.com/redirect").and_return(status: [300, "REDIRECT"], headers: { location: "http://api.foo.com/v2" })
FakeWeb.register_uri(:get, "http://api.foo.com/v2", body: "<hash><foo>bar</foo></hash>") stub_request(:get, "http://api.foo.com/v2").and_return(body: "<hash><foo>bar</foo></hash>")
body = "" body = ""
response = @request.perform { |chunk| body += chunk } response = @request.perform { |chunk| body += chunk }
expect(body.length).to eq(27) expect(body.length).to eq(27)
@ -590,9 +591,9 @@ RSpec.describe HTTParty::Request do
it "handles multiple redirects and relative location headers on different hosts" do it "handles multiple redirects and relative location headers on different hosts" do
@request = HTTParty::Request.new(Net::HTTP::Get, 'http://test.com/redirect', format: :xml) @request = HTTParty::Request.new(Net::HTTP::Get, 'http://test.com/redirect', format: :xml)
FakeWeb.register_uri(:get, "http://test.com/redirect", status: [300, "REDIRECT"], location: "http://api.foo.com/v2") stub_request(:get, "http://test.com/redirect").and_return(status: [300, "REDIRECT"], headers: { location: "http://api.foo.com/v2" })
FakeWeb.register_uri(:get, "http://api.foo.com/v2", status: [300, "REDIRECT"], location: "/v3") stub_request(:get, "http://api.foo.com/v2").and_return(status: [300, "REDIRECT"], headers: { location: "/v3" })
FakeWeb.register_uri(:get, "http://api.foo.com/v3", body: "<hash><foo>bar</foo></hash>") stub_request(:get, "http://api.foo.com/v3").and_return(body: "<hash><foo>bar</foo></hash>")
response = @request.perform response = @request.perform
expect(response.request.base_uri.to_s).to eq("http://api.foo.com") expect(response.request.base_uri.to_s).to eq("http://api.foo.com")
expect(response.request.path.to_s).to eq("/v3") expect(response.request.path.to_s).to eq("/v3")
@ -603,7 +604,7 @@ RSpec.describe HTTParty::Request do
it "raises an error if redirect has duplicate location header" do it "raises an error if redirect has duplicate location header" do
@request = HTTParty::Request.new(Net::HTTP::Get, 'http://test.com/redirect', format: :xml) @request = HTTParty::Request.new(Net::HTTP::Get, 'http://test.com/redirect', format: :xml)
FakeWeb.register_uri(:get, "http://test.com/redirect", status: [300, "REDIRECT"], location: ["http://api.foo.com/v2","http://api.foo.com/v2"]) stub_request(:get, "http://test.com/redirect").and_return(status: [300, "REDIRECT"], headers: { location: ["http://api.foo.com/v2","http://api.foo.com/v2"] })
expect {@request.perform}.to raise_error(HTTParty::DuplicateLocationHeader) expect {@request.perform}.to raise_error(HTTParty::DuplicateLocationHeader)
end end
@ -613,8 +614,8 @@ RSpec.describe HTTParty::Request do
end end
it "redirects including port" do it "redirects including port" do
FakeWeb.register_uri(:get, "http://withport.com:3000/v1", status: [301, "Moved Permanently"], location: "http://withport.com:3000/v2") stub_request(:get, "http://withport.com:3000/v1").and_return(status: [301, "Moved Permanently"], headers: { location: "http://withport.com:3000/v2" })
FakeWeb.register_uri(:get, "http://withport.com:3000/v2", status: 200) stub_request(:get, "http://withport.com:3000/v2").and_return(status: 200)
request = HTTParty::Request.new(Net::HTTP::Get, 'http://withport.com:3000/v1') request = HTTParty::Request.new(Net::HTTP::Get, 'http://withport.com:3000/v1')
response = request.perform response = request.perform
expect(response.request.base_uri.to_s).to eq("http://withport.com:3000") expect(response.request.base_uri.to_s).to eq("http://withport.com:3000")

View file

@ -3,11 +3,11 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
RSpec.describe HTTParty::Request do RSpec.describe HTTParty::Request do
context "SSL certificate verification" do context "SSL certificate verification" do
before do before do
FakeWeb.allow_net_connect = true WebMock.allow_net_connect!
end end
after do after do
FakeWeb.allow_net_connect = false WebMock.disable_net_connect!
end end
it "should fail when no trusted CA list is specified, by default" do it "should fail when no trusted CA list is specified, by default" do

View file

@ -333,7 +333,7 @@ RSpec.describe HTTParty do
it "should be able parse response with custom parser" do it "should be able parse response with custom parser" do
@klass.parser parser @klass.parser parser
FakeWeb.register_uri(:get, 'http://twitter.com/statuses/public_timeline.xml', body: 'tweets') stub_request(:get, 'http://twitter.com/statuses/public_timeline.xml').and_return(body: 'tweets')
custom_parsed_response = @klass.get('http://twitter.com/statuses/public_timeline.xml') custom_parsed_response = @klass.get('http://twitter.com/statuses/public_timeline.xml')
expect(custom_parsed_response[:sexy]).to eq(true) expect(custom_parsed_response[:sexy]).to eq(true)
end end
@ -388,7 +388,7 @@ RSpec.describe HTTParty do
it "should process a request with a uri instance parsed from the uri_adapter" do it "should process a request with a uri instance parsed from the uri_adapter" do
uri = 'http://foo.com/bar' uri = 'http://foo.com/bar'
FakeWeb.register_uri(:get, uri, body: 'stuff') stub_request(:get, uri).and_return(body: 'stuff')
@klass.uri_adapter uri_adapter @klass.uri_adapter uri_adapter
expect(@klass.get(uri).parsed_response).to eq('stuff') expect(@klass.get(uri).parsed_response).to eq('stuff')
end end
@ -421,7 +421,7 @@ RSpec.describe HTTParty do
expect(o[:connection_adapter_options]).to eq(connection_adapter_options) expect(o[:connection_adapter_options]).to eq(connection_adapter_options)
HTTParty::ConnectionAdapter.call(u, o) HTTParty::ConnectionAdapter.call(u, o)
}.with(URI.parse(uri), kind_of(Hash)) }.with(URI.parse(uri), kind_of(Hash))
FakeWeb.register_uri(:get, uri, body: 'stuff') stub_request(:get, uri).and_return(body: 'stuff')
@klass.connection_adapter connection_adapter, connection_adapter_options @klass.connection_adapter connection_adapter, connection_adapter_options
expect(@klass.get(uri).parsed_response).to eq('stuff') expect(@klass.get(uri).parsed_response).to eq('stuff')
end end
@ -861,7 +861,7 @@ RSpec.describe HTTParty do
it "should accept webcal URIs" do it "should accept webcal URIs" do
uri = 'http://google.com/' uri = 'http://google.com/'
FakeWeb.register_uri(:get, uri, body: 'stuff') stub_request(:get, uri).and_return(body: 'stuff')
uri = 'webcal://google.com/' uri = 'webcal://google.com/'
expect do expect do
HTTParty.get(uri) HTTParty.get(uri)

View file

@ -2,7 +2,7 @@ require "simplecov"
SimpleCov.start SimpleCov.start
require "httparty" require "httparty"
require "fakeweb" require 'webmock/rspec'
def file_fixture(filename) def file_fixture(filename)
open(File.join(File.dirname(__FILE__), 'fixtures', "#{filename}")).read open(File.join(File.dirname(__FILE__), 'fixtures', "#{filename}")).read
@ -14,14 +14,6 @@ RSpec.configure do |config|
config.include HTTParty::StubResponse config.include HTTParty::StubResponse
config.include HTTParty::SSLTestHelper config.include HTTParty::SSLTestHelper
config.before(:suite) do
FakeWeb.allow_net_connect = false
end
config.after(:suite) do
FakeWeb.allow_net_connect = true
end
config.expect_with :rspec do |expectations| config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end end