From c7027aa6e08266ac1f78b6ae882382e382afca01 Mon Sep 17 00:00:00 2001 From: Barry Mitchelson Date: Mon, 20 Apr 2009 14:04:49 +0100 Subject: [PATCH] Fixed issue with passing custom headers in Merb --- lib/webrat/merb_session.rb | 8 +++++--- spec/integration/merb/app/controllers/testing.rb | 4 ++++ spec/integration/merb/config/router.rb | 1 + spec/integration/merb/spec/webrat_spec.rb | 6 ++++++ spec/private/merb/merb_session_spec.rb | 4 ++-- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/webrat/merb_session.rb b/lib/webrat/merb_session.rb index fcc9099..e8856a5 100644 --- a/lib/webrat/merb_session.rb +++ b/lib/webrat/merb_session.rb @@ -38,10 +38,12 @@ module Webrat end def do_request(url, data, headers, method) - @response = request(url, + env = { :params => (data && data.any?) ? data : nil, - :headers => headers, - :method => method) + :method => method + }.merge(headers || {}) + + @response = request(url, env) end end diff --git a/spec/integration/merb/app/controllers/testing.rb b/spec/integration/merb/app/controllers/testing.rb index 31ccbeb..736a9ac 100644 --- a/spec/integration/merb/app/controllers/testing.rb +++ b/spec/integration/merb/app/controllers/testing.rb @@ -15,4 +15,8 @@ class Testing < Application redirect "http://google.com" end + def custom_headers + return request.env['my_custom_header'] + end + end diff --git a/spec/integration/merb/config/router.rb b/spec/integration/merb/config/router.rb index c57ec70..2cfb5a9 100644 --- a/spec/integration/merb/config/router.rb +++ b/spec/integration/merb/config/router.rb @@ -30,4 +30,5 @@ Merb::Router.prepare do match("/").to(:controller => "testing", :action => "show_form") match("/internal_redirect").to(:controller => "testing", :action => "internal_redirect") match("/external_redirect").to(:controller => "testing", :action => "external_redirect") + match("/custom_headers").to(:controller => "testing", :action => "custom_headers") end diff --git a/spec/integration/merb/spec/webrat_spec.rb b/spec/integration/merb/spec/webrat_spec.rb index bdaad20..96b41e1 100644 --- a/spec/integration/merb/spec/webrat_spec.rb +++ b/spec/integration/merb/spec/webrat_spec.rb @@ -29,4 +29,10 @@ describe "Webrat" do response = visit "/external_redirect" response.status.should == 302 end + + it "should allow custom headers" do + header("my_custom_header", "this_is_my_custom_header") + response = visit "/custom_headers" + response.should contain("this_is_my_custom_header") + end end diff --git a/spec/private/merb/merb_session_spec.rb b/spec/private/merb/merb_session_spec.rb index 25560f7..4f0253a 100644 --- a/spec/private/merb/merb_session_spec.rb +++ b/spec/private/merb/merb_session_spec.rb @@ -7,7 +7,7 @@ describe Webrat::MerbSession do session = Webrat::MerbSession.new response = OpenStruct.new response.status = 200 - session.should_receive(:request).with('url', {:params=> nil, :method=>"GET", :headers=>nil}).and_return(response) + session.should_receive(:request).with('url', {:params=> nil, :method=>"GET"}).and_return(response) session.get('url', {}, nil) end @@ -17,7 +17,7 @@ describe Webrat::MerbSession do response = OpenStruct.new response.status = 200 - session.should_receive(:request).with('url', {:params=>nil, :method=>request_method.upcase, :headers=>nil}).and_return(response) + session.should_receive(:request).with('url', {:params=>nil, :method=>request_method.upcase}).and_return(response) session.send(request_method, 'url', {}, nil) end end -- 1.6.0