Skip to content
Browse files

'+' in url encoded body is treated as a space.

  • Loading branch information...
1 parent 3b9d173 commit 4eb8971b63a6229537a5730ae67fcce55079076f @bblimke committed Mar 1, 2013
View
2 lib/webmock/request_pattern.rb
@@ -201,7 +201,7 @@ def body_as_hash(body, content_type)
when :xml then
Crack::XML.parse(body)
else
- WebMock::Util::QueryMapper.query_to_values(body)
+ WebMock::Util::QueryMapper.query_to_values(body, :form_url_encoded => true)
end
end
View
2 lib/webmock/request_stub.rb
@@ -81,7 +81,7 @@ def self.from_request_signature(signature)
if signature.body.to_s != ''
body = if signature.url_encoded?
- WebMock::Util::QueryMapper.query_to_values(signature.body)
+ WebMock::Util::QueryMapper.query_to_values(signature.body, :form_url_encoded => true)
else
signature.body
end
View
1 lib/webmock/util/query_mapper.rb
@@ -66,6 +66,7 @@ def self.query_to_values(query, options={})
value = true if value.nil?
key = Addressable::URI.unencode_component(key)
if value != true
+ value.gsub!(/\+/, " ") if options[:form_url_encoded]
value = Addressable::URI.unencode_component(value)
end
if options[:notation] == :flat
View
14 spec/acceptance/shared/stubbing_requests.rb
@@ -105,20 +105,20 @@
describe "when body is declared as a hash" do
before(:each) do
stub_request(:post, "www.example.com").
- with(:body => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} })
+ with(:body => {:a => '1', :b => 'five x', 'c' => {'d' => ['e', 'f']} })
end
describe "for request with url encoded body" do
it "should match request if hash matches body" do
http_request(
:post, "http://www.example.com/",
- :body => 'a=1&c[d][]=e&c[d][]=f&b=five').status.should == "200"
+ :body => 'a=1&c[d][]=e&c[d][]=f&b=five+x').status.should == "200"
end
it "should match request if hash matches body in different order of params" do
http_request(
:post, "http://www.example.com/",
- :body => 'a=1&c[d][]=e&b=five&c[d][]=f').status.should == "200"
+ :body => 'a=1&c[d][]=e&b=five+x&c[d][]=f').status.should == "200"
end
it "should not match if hash doesn't match url encoded body" do
@@ -152,6 +152,14 @@
:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
:body => "{\"foo\":\"2010-01-01\"}").status.should == "200"
end
+
+ it "should match if any of the strings have spaces" do
+ WebMock.reset!
+ stub_request(:post, "www.example.com").with(:body => {"foo" => "a b c"})
+ http_request(
+ :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
+ :body => "{\"foo\":\"a b c\"}").status.should == "200"
+ end
end
describe "for request with xml body and content type is set to xml" do

0 comments on commit 4eb8971

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