Skip to content

Commit

Permalink
em-http-request adapter is activated by replacing EventMachine::HttpR…
Browse files Browse the repository at this point in the history
…equest constant, instead of monkeypatching the original class.
  • Loading branch information
bblimke committed Nov 11, 2010
1 parent 6c5e0ec commit 86f36d2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@

* The expectation failure message now contains a list of made requests. Thanks to Martyn Loughran for suggesting this feature.

* Added `WebMock.print_executed_requests` method which can be useful to find out what requests were made until given point.
* Added `WebMock.print_executed_requests` method which can be useful to find out what requests were made until a given point.

* em-http-request adapter is activated by replacing EventMachine::HttpRequest constant, instead of monkeypatching the original class.

This technique is borrowed from em-http-request native mocking module. It allows switching WebMock adapter on an off, and using interchangeably with em-http-request native mocking i.e:

EventMachine::WebMockHttpRequest.activate!
EventMachine::WebMockHttpRequest.deactivate!

Thanks to Martyn Loughran for suggesting this feature.

* 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.

Expand Down
18 changes: 15 additions & 3 deletions lib/webmock/http_lib_adapters/em_http_request.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
if defined?(EventMachine::HttpRequest)

module EventMachine
class HttpRequest
OriginalHttpRequest = HttpRequest unless const_defined?(:OriginalHttpRequest)

class WebMockHttpRequest < EventMachine::HttpRequest

include HttpEncoding

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

def setup(response, uri, error = nil)
@uri = uri
Expand Down Expand Up @@ -36,7 +38,7 @@ def send_request_with_webmock(&block)
webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
WebMock::CallbackRegistry.invoke_callbacks(
{:lib => :em_http_request}, request_signature, webmock_response)
client = WebMockFakeHttpClient.new(nil)
client = WebMockHttpClient.new(nil)
client.on_error("WebMock timeout error") if webmock_response.should_timeout
client.setup(make_raw_response(webmock_response), @uri,
webmock_response.should_timeout ? "WebMock timeout error" : nil)
Expand Down Expand Up @@ -118,8 +120,18 @@ def make_raw_response(response)
response_string.join("\n")
end

def self.activate!
EventMachine.send(:remove_const, :HttpRequest)
EventMachine.send(:const_set, :HttpRequest, WebMockHttpRequest)
end

def self.deactivate!
EventMachine.send(:remove_const, :HttpRequest)
EventMachine.send(:const_set, :HttpRequest, OriginalHttpRequest)
end
end
end

EventMachine::WebMockHttpRequest.activate!

end

0 comments on commit 86f36d2

Please sign in to comment.