diff --git a/lib/webmock/http_lib_adapters/em_http_request_adapter.rb b/lib/webmock/http_lib_adapters/em_http_request_adapter.rb index 6b29b6b99..fc410e09f 100644 --- a/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +++ b/lib/webmock/http_lib_adapters/em_http_request_adapter.rb @@ -99,6 +99,11 @@ def setup(response, uri, error = nil) end end + def connection_completed + @state = :response_header + send_request(request_signature.headers, request_signature.body) + end + def send_request(head, body) WebMock::RequestRegistry.instance.requested_signatures.put(request_signature) @@ -164,7 +169,7 @@ def build_webmock_response end def build_request_signature - headers, body = @req.headers, @req.body + headers, body = build_request, @req.body @conn.middleware.select {|m| m.respond_to?(:request) }.each do |m| headers, body = m.request(self, headers, body) @@ -178,8 +183,6 @@ def build_request_signature body = form_encode_body(body) if body.is_a?(Hash) - headers = @req.headers - if headers['authorization'] && headers['authorization'].is_a?(Array) headers['Authorization'] = WebMock::Util::Headers.basic_auth_header(headers.delete('authorization')) end diff --git a/spec/acceptance/em_http_request/em_http_request_spec.rb b/spec/acceptance/em_http_request/em_http_request_spec.rb index ce83a3086..bf19f9d78 100644 --- a/spec/acceptance/em_http_request/em_http_request_spec.rb +++ b/spec/acceptance/em_http_request/em_http_request_spec.rb @@ -71,6 +71,35 @@ def request(client, head, body) end end + it "only calls request middleware once" do + stub_request(:get, "www.example.com") + + middleware = Class.new do + def self.called! + @called = called + 1 + end + + def self.called + @called || 0 + end + + def request(client, head, body) + self.class.called! + [head, body] + end + end + + EM.run do + conn = EventMachine::HttpRequest.new('http://www.example.com/') + conn.use middleware + http = conn.get + http.callback do + expect(middleware.called).to eq(1) + EM.stop + end + end + end + let(:response_middleware) do Class.new do def response(resp) @@ -119,6 +148,33 @@ def response(resp) context 'making a real request', net_connect: true do before { WebMock.allow_net_connect! } include_examples "em-http-request middleware/after_request hook integration" + + it "only calls request middleware once" do + middleware = Class.new do + def self.called! + @called = called + 1 + end + + def self.called + @called || 0 + end + + def request(client, head, body) + self.class.called! + [head, body] + end + end + + EM.run do + conn = EventMachine::HttpRequest.new(webmock_server_url) + conn.use middleware + http = conn.get + http.callback do + expect(middleware.called).to eq(1) + EM.stop + end + end + end end context 'when the request is stubbed' do