From 56287b4368f1cf8d6c544e5be4e19814d3dbb65b Mon Sep 17 00:00:00 2001 From: Eric Oestrich Date: Mon, 2 Apr 2012 14:53:03 -0400 Subject: [PATCH] Pass through SERVER_PORT when stubbing to rack If the server verifies a signature that includes the port, the webmock stub would otherwise make the request invalid --- lib/webmock/rack_response.rb | 3 ++- spec/acceptance/shared/returning_declared_responses.rb | 8 +++++--- spec/support/my_rack_app.rb | 6 ++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/webmock/rack_response.rb b/lib/webmock/rack_response.rb index b000b741b..6bdafb697 100644 --- a/lib/webmock/rack_response.rb +++ b/lib/webmock/rack_response.rb @@ -35,7 +35,8 @@ def build_rack_env(request) 'CONTENT_LENGTH' => body.size, 'PATH_INFO' => uri.path, 'QUERY_STRING' => uri.query || '', - 'SERVER_NAME' => uri.host + 'SERVER_NAME' => uri.host, + 'SERVER_PORT' => uri.port } env['HTTP_AUTHORIZATION'] = 'Basic ' + [uri.userinfo].pack('m').delete("\r\n") if uri.userinfo diff --git a/spec/acceptance/shared/returning_declared_responses.rb b/spec/acceptance/shared/returning_declared_responses.rb index 4facb3f9e..faae23bac 100644 --- a/spec/acceptance/shared/returning_declared_responses.rb +++ b/spec/acceptance/shared/returning_declared_responses.rb @@ -238,12 +238,14 @@ def call(request) end describe "when response is declared as an Rack app" do - before(:each) do + it "should return response returned by the rack app" do stub_request(:any, "http://www.example.com/greet").to_rack(MyRackApp) + http_request(:post, 'http://www.example.com/greet', :body => 'name=Jimmy').body.should == 'Good to meet you, Jimmy!' end - it "should return response returned by the rack app" do - http_request(:post, 'http://www.example.com/greet', :body => 'name=Jimmy').body.should == 'Good to meet you, Jimmy!' + it "should pass along the port number to the rack app" do + stub_request(:get, "http://www.example.com/compute").to_rack(MyRackApp) + http_request(:get, "http://www.example.com/compute").status.should == "200" end end diff --git a/spec/support/my_rack_app.rb b/spec/support/my_rack_app.rb index 65f9af0a4..a1410ef7a 100644 --- a/spec/support/my_rack_app.rb +++ b/spec/support/my_rack_app.rb @@ -26,6 +26,12 @@ def self.call(env) when ['POST', '/greet'] name = env["rack.input"].read[/name=([^&]*)/, 1] || "World" [200, {}, ["Good to meet you, #{name}!"]] + when ['GET', '/compute'] + if env['SERVER_PORT'] == 80 + [200, {}, [""]] + else + [401, {}, [""]] + end else [404, {}, ['']] end