Skip to content
Browse files

Add header support for POST method as well

  • Loading branch information...
1 parent 007e79c commit 63f590bf1a6d0cf49f2e1b62241bd8823a35fd0b @chris chris committed
Showing with 25 additions and 0 deletions.
  1. +25 −0 lib/webrat_headers_patch.rb
View
25 lib/webrat_headers_patch.rb
@@ -12,6 +12,31 @@ def get_with_headers(url, data, headers = nil)
@response = mechanize.get({url: url, headers: headers}, data)
end
+ # Replace Webrat version to allow headers (like #get). Only line
+ # changed from the Webrat implementation is the last line where
+ # it calls mechanize, now passing the headers argument.
+ def post_with_headers(url, data, headers = nil)
+ post_data = data.inject({}) do |memo, param|
+ case param
+ when Hash
+ param.each {|attribute, value| memo[attribute] = value }
+ memo
+ when Array
+ case param.last
+ when Hash
+ param.last.each {|attribute, value| memo["#{param.first}[#{attribute}]"] = value }
+ else
+ memo[param.first] = param.last
+ end
+ memo
+ end
+ end
+ @response = mechanize.post(url, post_data, headers)
+ end
+
alias_method :get_without_headers, :get
alias_method :get, :get_with_headers
+
+ alias_method :post_without_headers, :post
+ alias_method :post, :post_with_headers
end

0 comments on commit 63f590b

Please sign in to comment.
Something went wrong with that request. Please try again.