From 178bfd1c611dfa7426123f48a0e9df446aab2d7c Mon Sep 17 00:00:00 2001 From: Bartosz Blimke Date: Sun, 7 Nov 2010 19:54:28 +0000 Subject: [PATCH] em-http-request adapter now works correctly when :query option value is a string --- CHANGELOG.md | 2 ++ .../http_lib_adapters/em_http_request.rb | 26 ++++++++++--------- spec/em_http_request_spec.rb | 5 ++++ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ecb35efb1..d1dea90b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/webmock/http_lib_adapters/em_http_request.rb b/lib/webmock/http_lib_adapters/em_http_request.rb index e7f835126..94d5f68de 100644 --- a/lib/webmock/http_lib_adapters/em_http_request.rb +++ b/lib/webmock/http_lib_adapters/em_http_request.rb @@ -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]}" diff --git a/spec/em_http_request_spec.rb b/spec/em_http_request_spec.rb index 22feb3871..784c5bca2 100644 --- a/spec/em_http_request_spec.rb +++ b/spec/em_http_request_spec.rb @@ -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