From 5a1237f0342a5c92708ea71915876feac9a923a4 Mon Sep 17 00:00:00 2001 From: Bartosz Blimke Date: Tue, 20 Feb 2024 03:45:41 +0100 Subject: [PATCH] The stubbed Net::HTTPResponse#uri now returns request.uri, aligning it with the behavior of an actual Net::HTTPResponse. --- lib/webmock/http_lib_adapters/net_http.rb | 6 ++++-- spec/acceptance/net_http/net_http_shared.rb | 6 ++++++ spec/acceptance/net_http/net_http_spec.rb | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/webmock/http_lib_adapters/net_http.rb b/lib/webmock/http_lib_adapters/net_http.rb index 02359667..fd2a4a6f 100644 --- a/lib/webmock/http_lib_adapters/net_http.rb +++ b/lib/webmock/http_lib_adapters/net_http.rb @@ -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| @@ -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' @@ -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 diff --git a/spec/acceptance/net_http/net_http_shared.rb b/spec/acceptance/net_http/net_http_shared.rb index c7d7ebb8..9ab3379c 100644 --- a/spec/acceptance/net_http/net_http_shared.rb +++ b/spec/acceptance/net_http/net_http_shared.rb @@ -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("/") diff --git a/spec/acceptance/net_http/net_http_spec.rb b/spec/acceptance/net_http/net_http_spec.rb index c4f53b00..ad921f5a 100644 --- a/spec/acceptance/net_http/net_http_spec.rb +++ b/spec/acceptance/net_http/net_http_spec.rb @@ -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("/")