Skip to content

Commit

Permalink
The stubbed Net::HTTPResponse#uri now returns request.uri, aligning i…
Browse files Browse the repository at this point in the history
…t with the behavior of an actual Net::HTTPResponse.
  • Loading branch information
bblimke committed Feb 20, 2024
1 parent fb4c084 commit 5a1237f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/webmock/http_lib_adapters/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def request(request, body = nil, &block)
@socket = Net::HTTP.socket_type.new
WebMock::CallbackRegistry.invoke_callbacks(
{lib: :net_http}, request_signature, webmock_response)
build_net_http_response(webmock_response, &block)
build_net_http_response(webmock_response, request.uri, &block)
elsif WebMock.net_connect_allowed?(request_signature.uri)
check_right_http_connection
after_request = lambda do |response|
Expand Down Expand Up @@ -144,7 +144,7 @@ def start(&block)
end
end

def build_net_http_response(webmock_response, &block)
def build_net_http_response(webmock_response, request_uri, &block)
response = Net::HTTPResponse.send(:response_class, webmock_response.status[0].to_s).new("1.0", webmock_response.status[0].to_s, webmock_response.status[1])
body = webmock_response.body
body = nil if webmock_response.status[0].to_s == '204'
Expand All @@ -159,6 +159,8 @@ def build_net_http_response(webmock_response, &block)

response.instance_variable_set(:@read, true)

response.uri = request_uri

response.extend Net::WebMockHTTPResponse

if webmock_response.should_timeout
Expand Down
6 changes: 6 additions & 0 deletions spec/acceptance/net_http/net_http_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
expect(response.body).to be_a(Net::ReadAdapter)
end

it "should return response that returns request uri" do
uri = URI.parse("http://localhost:#{port}/")
response = Net::HTTP.get_response(uri)
expect(response.uri).to eql(uri)
end

it "should handle requests with block passed to read_body", net_connect: true do
body = "".dup
req = Net::HTTP::Get.new("/")
Expand Down
18 changes: 18 additions & 0 deletions spec/acceptance/net_http/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ class TestMarshalingInWebMockNetHTTP
expect(Net::HTTP.start("www.example.com") { |http| http.request(req)}.body).to eq("abc")
end

context "returning response that returns request uri" do
context "when making a real request" do
it "should return response that reports request uri" do
WebMock.allow_net_connect!
uri = URI.parse("http://localhost:#{port}/")
response = Net::HTTP.get_response(uri)
expect(response.uri).to eql(uri)
end
end

it "should return response that returns request uri" do
stub_request(:get, "www.example.com")
uri = URI.parse("http://www.example.com/")
response = Net::HTTP.get_response(uri)
expect(response.uri).to eql(URI.parse("http://www.example.com/"))
end
end

it "should handle Net::HTTP::Post#body_stream" do
stub_http_request(:post, "www.example.com").with(body: "my_params").to_return(body: "abc")
req = Net::HTTP::Post.new("/")
Expand Down

0 comments on commit 5a1237f

Please sign in to comment.