Permalink
Browse files

em-http-request adapter now works correctly when :query option value …

…is a string
  • Loading branch information...
1 parent a7cb4fa commit 178bfd1c611dfa7426123f48a0e9df446aab2d7c @bblimke committed Nov 7, 2010
Showing with 21 additions and 12 deletions.
  1. +2 −0 CHANGELOG.md
  2. +14 −12 lib/webmock/http_lib_adapters/em_http_request.rb
  3. +5 −0 spec/em_http_request_spec.rb
View
@@ -14,6 +14,8 @@
* Fixed issue with stubbing requests with request body declared as a hash, when json was not required. Thanks to Erik Michaels-Ober for reporting the issue.
+* Fixed issue with em-http-request adapter which didn't work when :query option value was as a string, not a hash. Thanks to Chee Yeo for reporting the issue.
+
## 1.5.0
@@ -3,6 +3,8 @@
module EventMachine
class HttpRequest
+ include HttpEncoding
+
class WebMockFakeHttpClient < EventMachine::HttpClient
def setup(response, uri, error = nil)
@@ -17,7 +19,7 @@ def setup(response, uri, error = nil)
end
end
end
-
+
def unbind
end
@@ -33,7 +35,7 @@ def send_request_with_webmock(&block)
if WebMock::RequestRegistry.instance.registered_request?(request_signature)
webmock_response = WebMock::RequestRegistry.instance.response_for_request(request_signature)
WebMock::CallbackRegistry.invoke_callbacks(
- {:lib => :em_http_request}, request_signature, webmock_response)
+ {:lib => :em_http_request}, request_signature, webmock_response)
client = WebMockFakeHttpClient.new(nil)
client.on_error("WebMock timeout error") if webmock_response.should_timeout
client.setup(make_raw_response(webmock_response), @uri,
@@ -54,13 +56,13 @@ def send_request_with_webmock(&block)
raise WebMock::NetConnectNotAllowedError.new(request_signature)
end
end
-
+
alias_method :send_request_without_webmock, :send_request
alias_method :send_request, :send_request_with_webmock
-
+
private
-
+
def build_webmock_response(http)
webmock_response = WebMock::Response.new
webmock_response.status = [http.response_header.status, http.response_header.http_reason]
@@ -78,7 +80,7 @@ def build_request_signature
options = @options
method = @method
uri = @uri
- end
+ end
if options[:authorization] || options['authorization']
auth = (options[:authorization] || options['authorization'])
@@ -87,9 +89,9 @@ def build_request_signature
options.reject! {|k,v| k.to_s == 'authorization' } #we added it to url userinfo
uri.userinfo = userinfo
end
-
- uri.query_values = (uri.query_values || {}).merge(options[:query]) if options[:query]
-
+
+ uri.query = encode_query(@req.uri, options[:query])[2..-1]
+
WebMock::RequestSignature.new(
method.downcase.to_sym,
uri.to_s,
@@ -99,11 +101,11 @@ def build_request_signature
end
- def make_raw_response(response)
+ def make_raw_response(response)
response.raise_error_if_any
-
+
status, headers, body = response.status, response.headers, response.body
-
+
response_string = []
response_string << "HTTP/1.1 #{status[0]} #{status[1]}"
@@ -25,5 +25,10 @@
http_request(:get, "http://www.example.com/?x=3", :query => {"a" => ["b", "c"]}).body.should == "abc"
end
+ it "should work with optional query params declared as string" do
+ stub_http_request(:get, "www.example.com/?x=3&a[]=b&a[]=c").to_return(:body => "abc")
+ http_request(:get, "http://www.example.com/?x=3", :query => "a[]=b&a[]=c").body.should == "abc"
+ end
+
end
end

0 comments on commit 178bfd1

Please sign in to comment.