Skip to content

Commit

Permalink
Replaces Fakeweb with Webmock in tests.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JonMidhir committed Jun 3, 2017
1 parent 96d1c0a commit 647dbb6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -2,7 +2,6 @@ source 'https://rubygems.org'
gemspec

gem 'rake'
gem 'fakeweb', '~> 1.3'
gem 'mongrel', '1.2.0.pre2'

group :development do
Expand All @@ -16,4 +15,5 @@ group :test do
gem 'simplecov', require: false
gem 'aruba'
gem 'cucumber', '~> 2.3'
gem 'webmock'
end
77 changes: 39 additions & 38 deletions spec/httparty/request_spec.rb
Expand Up @@ -157,46 +157,47 @@
expect(@request.instance_variable_get(:@raw_request)['authorization']).not_to be_nil
end

context 'digest_auth' do
before do
context 'digest_auth' do
before do
response_sequence = [
{status: ['401', 'Unauthorized' ],
{status: ['401', 'Unauthorized' ], headers: {
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']}
]
FakeWeb.register_uri(:get, "http://api.foo.com/v1",
response_sequence)
end
]
stub_request(:get, "http://api.foo.com/v1").and_return(response_sequence)
end

it 'should not send credentials more than once' do
response_sequence = [
{status: ['401', 'Unauthorized' ],
{status: ['401', 'Unauthorized' ], headers: {
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',
set_cookie: 'custom-cookie=1234567',
},
set_cookie: 'custom-cookie=1234567'
}
},
{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)

@request.options[:digest_auth] = {username: 'foobar', password: 'secret'}
@request.options[:digest_auth] = {username: 'foobar', password: 'secret'}
response = @request.perform { |v| }
expect(response.code).to eq(401)

raw_request = @request.instance_variable_get(:@raw_request)
expect(raw_request['Authorization']).not_to be_nil
end
expect(raw_request['Authorization']).not_to be_nil
end

it 'should not be used when configured and the response is 200' do
FakeWeb.register_uri(:get, "http://api.foo.com/v1",
status: 200)
@request.options[:digest_auth] = {username: 'foobar', password: 'secret'}
stub_request(:get, "http://api.foo.com/v1").and_return(status: 200)
@request.options[:digest_auth] = {username: 'foobar', password: 'secret'}
response = @request.perform { |v| }
expect(response.code).to eq(200)

Expand All @@ -206,7 +207,7 @@
end

it "should be used when configured and the response is 401" do
@request.options[:digest_auth] = {username: 'foobar', password: 'secret'}
@request.options[:digest_auth] = {username: 'foobar', password: 'secret'}
response = @request.perform { |v| }
expect(response.code).to eq(200)

Expand All @@ -215,16 +216,16 @@
end

it 'should maintain cookies returned from a 401 response' do
@request.options[:digest_auth] = {username: 'foobar', password: 'secret'}
@request.options[:digest_auth] = {username: 'foobar', password: 'secret'}
response = @request.perform {|v|}
expect(response.code).to eq(200)

raw_request = @request.instance_variable_get(:@raw_request)
expect(raw_request.get_fields('cookie')).to eql ["custom-cookie=1234567"]
end

it 'should merge cookies from request and a 401 response' do

@request.options[:digest_auth] = {username: 'foobar', password: 'secret'}
@request.options[:headers] = {'cookie' => 'request-cookie=test'}
response = @request.perform {|v|}
Expand All @@ -243,7 +244,7 @@
end

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', {
base_uri: 'foo.com'
})
Expand Down Expand Up @@ -526,7 +527,7 @@
response = stub_response "Content"
response.initialize_http_header("Content-Type" => "text/plain;charset = utf-lols")
resp = @request.perform
expect(response_charset).to_not be_empty
expect(response_charset).to_not be_empty
# This encoding does not exist, thus the string should not be encodd with it
expect(resp.body.encoding).to_not eq(response_charset)
expect(resp.body).to eq("Content")
Expand Down Expand Up @@ -568,8 +569,8 @@

it "calls block given to perform with each redirect" do
@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")
FakeWeb.register_uri(:get, "http://api.foo.com/v2", body: "<hash><foo>bar</foo></hash>")
stub_request(:get, "http://test.com/redirect").and_return(status: [300, "REDIRECT"], headers: { location: "http://api.foo.com/v2" })
stub_request(:get, "http://api.foo.com/v2").and_return(body: "<hash><foo>bar</foo></hash>")
body = ""
response = @request.perform { |chunk| body += chunk }
expect(body.length).to eq(27)
Expand All @@ -590,9 +591,9 @@

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)
FakeWeb.register_uri(:get, "http://test.com/redirect", status: [300, "REDIRECT"], location: "http://api.foo.com/v2")
FakeWeb.register_uri(:get, "http://api.foo.com/v2", status: [300, "REDIRECT"], location: "/v3")
FakeWeb.register_uri(:get, "http://api.foo.com/v3", body: "<hash><foo>bar</foo></hash>")
stub_request(:get, "http://test.com/redirect").and_return(status: [300, "REDIRECT"], headers: { location: "http://api.foo.com/v2" })
stub_request(:get, "http://api.foo.com/v2").and_return(status: [300, "REDIRECT"], headers: { location: "/v3" })
stub_request(:get, "http://api.foo.com/v3").and_return(body: "<hash><foo>bar</foo></hash>")
response = @request.perform
expect(response.request.base_uri.to_s).to eq("http://api.foo.com")
expect(response.request.path.to_s).to eq("/v3")
Expand All @@ -603,7 +604,7 @@

it "raises an error if redirect has duplicate location header" do
@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)
end

Expand All @@ -613,8 +614,8 @@
end

it "redirects including port" do
FakeWeb.register_uri(:get, "http://withport.com:3000/v1", status: [301, "Moved Permanently"], location: "http://withport.com:3000/v2")
FakeWeb.register_uri(:get, "http://withport.com:3000/v2", status: 200)
stub_request(:get, "http://withport.com:3000/v1").and_return(status: [301, "Moved Permanently"], headers: { location: "http://withport.com:3000/v2" })
stub_request(:get, "http://withport.com:3000/v2").and_return(status: 200)
request = HTTParty::Request.new(Net::HTTP::Get, 'http://withport.com:3000/v1')
response = request.perform
expect(response.request.base_uri.to_s).to eq("http://withport.com:3000")
Expand Down Expand Up @@ -1230,19 +1231,19 @@
end

context 'with Accept-Encoding header' do
it 'should disable content decoding if present' do
it 'should disable content decoding if present' do
request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', headers:{'Accept-Encoding' => 'custom'})
request.send(:setup_raw_request)
expect(request.instance_variable_get(:@raw_request).decode_content).to eq(false)
end

it 'should disable content decoding if present and lowercase' do
it 'should disable content decoding if present and lowercase' do
request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', headers:{'accept-encoding' => 'custom'})
request.send(:setup_raw_request)
expect(request.instance_variable_get(:@raw_request).decode_content).to eq(false)
end

it 'should disable content decoding if present' do
it 'should disable content decoding if present' do
request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1')
request.send(:setup_raw_request)
expect(request.instance_variable_get(:@raw_request).decode_content).to eq(true)
Expand Down
4 changes: 2 additions & 2 deletions spec/httparty/ssl_spec.rb
Expand Up @@ -3,11 +3,11 @@
RSpec.describe HTTParty::Request do
context "SSL certificate verification" do
before do
FakeWeb.allow_net_connect = true
WebMock.allow_net_connect!
end

after do
FakeWeb.allow_net_connect = false
WebMock.disable_net_connect!
end

it "should fail when no trusted CA list is specified, by default" do
Expand Down
8 changes: 4 additions & 4 deletions spec/httparty_spec.rb
Expand Up @@ -333,7 +333,7 @@ def self.parse(body)

it "should be able parse response with custom parser" do
@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')
expect(custom_parsed_response[:sexy]).to eq(true)
end
Expand Down Expand Up @@ -388,7 +388,7 @@ def self.parse uri

it "should process a request with a uri instance parsed from the uri_adapter" do
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
expect(@klass.get(uri).parsed_response).to eq('stuff')
end
Expand Down Expand Up @@ -421,7 +421,7 @@ def self.parse uri
expect(o[:connection_adapter_options]).to eq(connection_adapter_options)
HTTParty::ConnectionAdapter.call(u, o)
}.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
expect(@klass.get(uri).parsed_response).to eq('stuff')
end
Expand Down Expand Up @@ -861,7 +861,7 @@ def self.inherited(subclass)

it "should accept webcal URIs" do
uri = 'http://google.com/'
FakeWeb.register_uri(:get, uri, body: 'stuff')
stub_request(:get, uri).and_return(body: 'stuff')
uri = 'webcal://google.com/'
expect do
HTTParty.get(uri)
Expand Down
10 changes: 1 addition & 9 deletions spec/spec_helper.rb
Expand Up @@ -2,7 +2,7 @@
SimpleCov.start

require "httparty"
require "fakeweb"
require 'webmock/rspec'

def file_fixture(filename)
open(File.join(File.dirname(__FILE__), 'fixtures', "#{filename}")).read
Expand All @@ -14,14 +14,6 @@ def file_fixture(filename)
config.include HTTParty::StubResponse
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|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
Expand Down

0 comments on commit 647dbb6

Please sign in to comment.