Permalink
Browse files

Add failing spec demonstrating em-http-request bug.

When a response modifying middleware is used with em-http-request, and a real request is made, the middleware can modify the response before the after_request is invoked. This prevents VCR from being able to record the response accurately.  The after_request hook should be invoked before the em-http-request middleware.

Related to myronmarston/vcr#169.
  • Loading branch information...
1 parent d083e68 commit 3fb913dbd96e29e6227d915d328a052bd2d48502 @myronmarston myronmarston committed May 15, 2012
Showing with 37 additions and 5 deletions.
  1. +37 −5 spec/acceptance/em_http_request/em_http_request_spec.rb
View
42 spec/acceptance/em_http_request/em_http_request_spec.rb
@@ -38,19 +38,21 @@ def request(client, head, body)
end
end
- it "should work with response middleware" do
- stub_request(:get, "www.example.com").to_return(:body => 'foo')
-
- middleware = Class.new do
+ let(:response_middleware) do
+ Class.new do
def response(resp)
resp.response = 'bar'
end
end
+ end
+
+ it "should work with response middleware" do
+ stub_request(:get, "www.example.com").to_return(:body => 'foo')
EM.run do
conn = EventMachine::HttpRequest.new('http://www.example.com/')
- conn.use middleware
+ conn.use response_middleware
http = conn.get
@@ -60,6 +62,36 @@ def response(resp)
end
end
end
+
+ let(:webmock_server_url) { "http://#{WebMockServer.instance.host_with_port}/" }
+
+ shared_examples_for "em-http-request middleware/after_request hook integration" do
+ it 'yields the original raw body to the after_request hook even if a response middleware modifies the body' do
+ yielded_response_body = nil
+ ::WebMock.after_request do |request, response|
+ yielded_response_body = response.body
+ end
+
+ EM::HttpRequest.use response_middleware
+
+ EM.run do
+ http = EventMachine::HttpRequest.new(webmock_server_url).get
+ http.callback { EM.stop }
+ end
+
+ yielded_response_body.should eq("hello world")
+ end
+ end
+
+ context 'making a real request' do
+ before { WebMock.allow_net_connect! }
+ include_examples "em-http-request middleware/after_request hook integration"
+ end
+
+ context 'when the request is stubbed' do
+ before { stub_request(:get, webmock_server_url).to_return(:body => 'hello world') }
+ include_examples "em-http-request middleware/after_request hook integration"
+ end
end
# not pretty, but it works

0 comments on commit 3fb913d

Please sign in to comment.