Skip to content

Commit

Permalink
Mirror Net::HTTP handling of headers as symbols
Browse files Browse the repository at this point in the history
  as brought up in #486
  • Loading branch information
davidbegin committed Sep 6, 2015
1 parent edb2245 commit aabd4ea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/webmock/http_lib_adapters/net_http.rb
Expand Up @@ -273,8 +273,20 @@ def self.request_signature_from_request(net_http, request, body = nil)

headers = Hash[*request.to_hash.map {|k,v| [k, v]}.inject([]) {|r,x| r + x}]

headers.reject! {|k,v| k =~ /[Aa]uthorization/ && v.first =~ /^Basic / } #we added it to url userinfo
# If you try and make a request with headers that are symbols,
# Net Http raises the following NoMethodError.
#
# WebMock normalizes headers when creating a RequestSignature,
# and will update all headers from symbols to strings.
# This means that you could have a test passing with WebMock, but failing in reality.
header_as_symbol = headers.keys.find {|header| header.is_a? Symbol}
if header_as_symbol
raise NoMethodError.new(
"undefined method `split' for :#{header_as_symbol}:Symbol (NoMethodError)`"
)
end

headers.reject! {|k,v| k =~ /[Aa]uthorization/ && v.first =~ /^Basic / } #we added it to url userinfo

if request.body_stream
body = request.body_stream.read
Expand Down
28 changes: 27 additions & 1 deletion spec/acceptance/net_http/net_http_spec.rb
Expand Up @@ -9,7 +9,7 @@
describe "Net:HTTP" do
include_examples "with WebMock"

let(:port){ WebMockServer.instance.port }
let(:port) { WebMockServer.instance.port }

describe "marshalling" do
class TestMarshalingInWebMockNetHTTP
Expand Down Expand Up @@ -106,6 +106,31 @@ class TestMarshalingInWebMockNetHTTP
expect(Net::HTTP.start("www.example.com") { |query| query.get("/") }.body).to eq("abc"*100000)
end

it "preserves the same functionality as the Net Http library around headers as symbols" do
uri = URI.parse("http://google.com/")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request[:InvalidHeaderSinceItsASymbol] = "this will not be valid"

begin
http.request(request)
rescue => e
error_with_webmock = e
end

WebMock.disable!

begin
http.request(request)
rescue => e
error_without_webmock = e
end

WebMock.enable!

expect(error_with_webmock.class).to be error_without_webmock.class
end

it "should handle multiple values for the same response header" do
stub_http_request(:get, "www.example.com").to_return(:headers => { 'Set-Cookie' => ['foo=bar', 'bar=bazz'] })
response = Net::HTTP.get_response(URI.parse("http://www.example.com/"))
Expand Down Expand Up @@ -169,6 +194,7 @@ class TestMarshalingInWebMockNetHTTP
@http.use_ssl = true
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

describe "when net http is allowed" do
it "should not connect to the server until the request", :net_connect => true do
WebMock.allow_net_connect!
Expand Down

0 comments on commit aabd4ea

Please sign in to comment.