Skip to content

Commit

Permalink
Do not alter real (non-stubbed) request headers when handling em-http…
Browse files Browse the repository at this point in the history
…-request requests.
  • Loading branch information
bblimke committed Aug 13, 2023
1 parent 833291d commit ef566e8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/webmock/http_lib_adapters/em_http_request_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def setup(response, uri, error = nil)

def connection_completed
@state = :response_header
send_request(request_signature.headers, request_signature.body)
send_request(*headers_and_body_processed_by_middleware)
end

def send_request(head, body)
Expand Down Expand Up @@ -168,12 +168,18 @@ def build_webmock_response
webmock_response
end

def build_request_signature
headers, body = build_request, @req.body

@conn.middleware.select {|m| m.respond_to?(:request) }.each do |m|
headers, body = m.request(self, headers, body)
def headers_and_body_processed_by_middleware
@headers_and_body_processed_by_middleware ||= begin
head, body = build_request, @req.body
@conn.middleware.each do |m|
head, body = m.request(self, head, body) if m.respond_to?(:request)
end
[head, body]
end
end

def build_request_signature
headers, body = headers_and_body_processed_by_middleware

method = @req.method
uri = @req.uri.clone
Expand Down
12 changes: 12 additions & 0 deletions spec/acceptance/em_http_request/em_http_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ def response(resp)
before { WebMock.allow_net_connect! }
include_examples "em-http-request middleware/after_request hook integration"

it "doesn't modify headers" do
EM.run do
conn = EventMachine::HttpRequest.new(webmock_server_url)
http = conn.post(head: { 'content-length' => '4' }, body: 'test')
expect(conn).to receive(:send_data).with(/POST \/ HTTP\/1.1\r\nContent-Length: 4\r\nConnection: close\r\nHost: localhost:\d+\r\nUser-Agent: EventMachine HttpClient\r\nAccept-Encoding: gzip, compressed\r\n\r\n/).and_call_original
expect(conn).to receive(:send_data).with('test')
http.callback do
EM.stop
end
end
end

it "only calls request middleware once" do
middleware = Class.new do
def self.called!
Expand Down

0 comments on commit ef566e8

Please sign in to comment.