Skip to content

Commit

Permalink
em-http-request adapter now works correctly when :query option value …
Browse files Browse the repository at this point in the history
…is a string
  • Loading branch information
bblimke committed Nov 7, 2010
1 parent a7cb4fa commit 178bfd1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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 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 ## 1.5.0


Expand Down
26 changes: 14 additions & 12 deletions lib/webmock/http_lib_adapters/em_http_request.rb
Expand Up @@ -3,6 +3,8 @@
module EventMachine module EventMachine
class HttpRequest class HttpRequest


include HttpEncoding

class WebMockFakeHttpClient < EventMachine::HttpClient class WebMockFakeHttpClient < EventMachine::HttpClient


def setup(response, uri, error = nil) def setup(response, uri, error = nil)
Expand All @@ -17,7 +19,7 @@ def setup(response, uri, error = nil)
end end
end end
end end

def unbind def unbind
end end


Expand All @@ -33,7 +35,7 @@ def send_request_with_webmock(&block)
if WebMock::RequestRegistry.instance.registered_request?(request_signature) if WebMock::RequestRegistry.instance.registered_request?(request_signature)
webmock_response = WebMock::RequestRegistry.instance.response_for_request(request_signature) webmock_response = WebMock::RequestRegistry.instance.response_for_request(request_signature)
WebMock::CallbackRegistry.invoke_callbacks( 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 = WebMockFakeHttpClient.new(nil)
client.on_error("WebMock timeout error") if webmock_response.should_timeout client.on_error("WebMock timeout error") if webmock_response.should_timeout
client.setup(make_raw_response(webmock_response), @uri, client.setup(make_raw_response(webmock_response), @uri,
Expand All @@ -54,13 +56,13 @@ def send_request_with_webmock(&block)
raise WebMock::NetConnectNotAllowedError.new(request_signature) raise WebMock::NetConnectNotAllowedError.new(request_signature)
end end
end end

alias_method :send_request_without_webmock, :send_request alias_method :send_request_without_webmock, :send_request
alias_method :send_request, :send_request_with_webmock alias_method :send_request, :send_request_with_webmock



private private

def build_webmock_response(http) def build_webmock_response(http)
webmock_response = WebMock::Response.new webmock_response = WebMock::Response.new
webmock_response.status = [http.response_header.status, http.response_header.http_reason] webmock_response.status = [http.response_header.status, http.response_header.http_reason]
Expand All @@ -78,7 +80,7 @@ def build_request_signature
options = @options options = @options
method = @method method = @method
uri = @uri uri = @uri
end end


if options[:authorization] || options['authorization'] if options[:authorization] || options['authorization']
auth = (options[:authorization] || options['authorization']) auth = (options[:authorization] || options['authorization'])
Expand All @@ -87,9 +89,9 @@ def build_request_signature
options.reject! {|k,v| k.to_s == 'authorization' } #we added it to url userinfo options.reject! {|k,v| k.to_s == 'authorization' } #we added it to url userinfo
uri.userinfo = userinfo uri.userinfo = userinfo
end 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( WebMock::RequestSignature.new(
method.downcase.to_sym, method.downcase.to_sym,
uri.to_s, uri.to_s,
Expand All @@ -99,11 +101,11 @@ def build_request_signature
end end




def make_raw_response(response) def make_raw_response(response)
response.raise_error_if_any response.raise_error_if_any

status, headers, body = response.status, response.headers, response.body status, headers, body = response.status, response.headers, response.body

response_string = [] response_string = []
response_string << "HTTP/1.1 #{status[0]} #{status[1]}" response_string << "HTTP/1.1 #{status[0]} #{status[1]}"


Expand Down
5 changes: 5 additions & 0 deletions spec/em_http_request_spec.rb
Expand Up @@ -25,5 +25,10 @@
http_request(:get, "http://www.example.com/?x=3", :query => {"a" => ["b", "c"]}).body.should == "abc" http_request(:get, "http://www.example.com/?x=3", :query => {"a" => ["b", "c"]}).body.should == "abc"
end 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
end end

0 comments on commit 178bfd1

Please sign in to comment.