Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ruby http gem v1.0.0+ #554

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions lib/webmock/http_lib_adapters/http_rb/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,34 @@ def to_webmock
webmock_response
end

def self.from_webmock(webmock_response, request_signature = nil)
status = Status.new(webmock_response.status.first)
headers = webmock_response.headers || {}
body = Body.new Streamer.new webmock_response.body
uri = URI request_signature.uri.to_s if request_signature
class << self
def from_webmock(webmock_response, request_signature = nil)
status = Status.new(webmock_response.status.first)
headers = webmock_response.headers || {}
body = Body.new Streamer.new webmock_response.body
uri = normalize_uri(request_signature && request_signature.uri)

new(status, "1.1", headers, body, uri)
return new(status, "1.1", headers, body, uri) if HTTP::VERSION < "1.0.0"

new({
:status => status,
:version => "1.1",
:headers => headers,
:body => body,
:uri => uri
})
end

private

def normalize_uri(uri)
return unless uri

uri = URI.parse uri
uri.port = nil if uri.default_port && uri.port == uri.default_port

uri
end
end
end
end
7 changes: 6 additions & 1 deletion lib/webmock/http_lib_adapters/http_rb/webmock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ def replay

return unless webmock_response

raise Errno::ETIMEDOUT if webmock_response.should_timeout
raise_timeout_error if webmock_response.should_timeout
webmock_response.raise_error_if_any

invoke_callbacks(webmock_response, :real_request => false)
::HTTP::Response.from_webmock webmock_response, request_signature
end

def raise_timeout_error
raise Errno::ETIMEDOUT if HTTP::VERSION < "1.0.0"
raise HTTP::ConnectionError, "connection error: #{Errno::ETIMEDOUT.new}"
end

def perform
return unless ::WebMock.net_connect_allowed?(request_signature.uri)
response = @perform.call
Expand Down
24 changes: 19 additions & 5 deletions spec/acceptance/http_rb/http_rb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,26 @@
end
end

it "restores request uri on replayed response object" do
uri = Addressable::URI.parse "http://example.com/foo"
context "restored request uri on replayed response object" do
it "keeps non-default port" do
stub_request :get, "example.com:1234/foo"
response = HTTP.get "http://example.com:1234/foo"

stub_request :get, "example.com/foo"
response = HTTP.get uri
expect(response.uri.to_s).to eq "http://example.com:1234/foo"
end

it "does not injects default port" do
stub_request :get, "example.com/foo"
response = HTTP.get "http://example.com/foo"

expect(response.uri.to_s).to eq "http://example.com/foo"
end

it "strips out default port even if it was explicitly given" do
stub_request :get, "example.com/foo"
response = HTTP.get "http://example.com:80/foo"

expect(response.uri.to_s).to eq uri.to_s
expect(response.uri.to_s).to eq "http://example.com/foo"
end
end
end
6 changes: 4 additions & 2 deletions spec/acceptance/http_rb/http_rb_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ def http_request(method, uri, options = {})
end

def client_timeout_exception_class
Errno::ETIMEDOUT
return Errno::ETIMEDOUT if HTTP::VERSION < "1.0.0"
HTTP::ConnectionError
end

def connection_refused_exception_class
Errno::ECONNREFUSED
return Errno::ECONNREFUSED if HTTP::VERSION < "1.0.0"
HTTP::ConnectionError
end

def http_library
Expand Down