Skip to content

Commit

Permalink
Fix KeyError with missing Host in HTTP::Request
Browse files Browse the repository at this point in the history
When calling `HTTP::Client#exec` with an `HTTP::Request` instance the
Host header is not automatically filled by the http client, so we need
to add it manually or the `host_matches?` check in the stub will fail
with a `KeyError` exception.
  • Loading branch information
felixbuenemann committed Jul 21, 2018
1 parent 7e7e7ff commit 00574dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spec/webmock_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,16 @@ describe WebMock do
end
end

it "works with exec" do
WebMock.wrap do
WebMock.stub(:get, "http://www.example.com")

client = HTTP::Client.new "www.example.com"
request = HTTP::Request.new("GET", "/")
client.exec request
end
end

describe ".calls" do
it "returns 0 by default" do
WebMock.wrap do
Expand Down
1 change: 1 addition & 0 deletions src/webmock/core_ext.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ end
class HTTP::Client
private def exec_internal(request : HTTP::Request)
request.scheme = "https" if tls?
request.headers["Host"] = host_header unless request.headers.has_key?("Host")
stub = WebMock.find_stub(request)
return stub.exec(request) if stub

Expand Down

0 comments on commit 00574dd

Please sign in to comment.