Skip to content

Commit

Permalink
Add failing spec demonstrating em-http-request bug.
Browse files Browse the repository at this point in the history
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
myronmarston committed May 15, 2012
1 parent d083e68 commit 3fb913d
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions spec/acceptance/em_http_request/em_http_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 3fb913d

Please sign in to comment.