Permalink
Browse files

em-http-request adapter is activated by replacing EventMachine::HttpR…

…equest constant, instead of monkeypatching the original class.
  • Loading branch information...
1 parent 6c5e0ec commit 86f36d2d89775fc46a06a95dfb0cc1f54a8ea69f @bblimke committed Nov 11, 2010
Showing with 25 additions and 4 deletions.
  1. +10 −1 CHANGELOG.md
  2. +15 −3 lib/webmock/http_lib_adapters/em_http_request.rb
View
@@ -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.
@@ -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
@@ -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)
@@ -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.