Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions spec/webmock_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ describe WebMock do
rescue ex : WebMock::NetConnectNotAllowedError
ex.message.not_nil!.strip.should eq(
<<-MSG
Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/foo?a=1 with body "Hello!" with headers {"Foo" => "Bar", "Content-Length" => "6", "Host" => "www.example.com"}
Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/foo?a=1 with body "Hello!" with headers {"Foo" => "Bar", "Connection" => "close", "Content-Length" => "6", "Host" => "www.example.com"}

You can stub this request with the following snippet:

WebMock.stub(:post, "http://www.example.com/foo?a=1").
with(body: "Hello!", headers: {"Foo" => "Bar"}).
with(body: "Hello!", headers: {"Foo" => "Bar", "Connection" => "close"}).
to_return(body: "")
MSG
)
Expand All @@ -298,7 +298,7 @@ describe WebMock do
rescue ex : WebMock::NetConnectNotAllowedError
ex.message.not_nil!.strip.should eq(
<<-MSG
Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/foo?a=1 with headers {"Content-Length" => "0", "Host" => "www.example.com"}
Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/foo?a=1 with headers {"Connection" => "close", "Content-Length" => "0", "Host" => "www.example.com"}

You can stub this request with the following snippet:

Expand All @@ -317,7 +317,7 @@ describe WebMock do
rescue ex : WebMock::NetConnectNotAllowedError
ex.message.not_nil!.strip.should eq(
<<-MSG
Real HTTP connections are disabled. Unregistered request: GET https://www.example.com/ with headers {"Host" => "www.example.com"}
Real HTTP connections are disabled. Unregistered request: GET https://www.example.com/ with headers {"Connection" => "close", "Host" => "www.example.com"}

You can stub this request with the following snippet:

Expand All @@ -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
12 changes: 12 additions & 0 deletions src/webmock.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module WebMock
extend self

@@allow_net_connect = false
@@consume_after_match = false
@@registry = StubRegistry.new
@@callbacks = CallbackRegistry.new

Expand All @@ -29,10 +30,21 @@ module WebMock
@@allow_net_connect
end

def consume_after_match=(@@consume_after_match)
end

def consume_after_match?
@@consume_after_match
end

def find_stub(request : HTTP::Request)
@@registry.find_stub(request)
end

def consume_stub(request : HTTP::Request)
@@registry.consume_stub(request)
end

def callbacks
@@callbacks
end
Expand Down
7 changes: 6 additions & 1 deletion src/webmock/core_ext.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ end
class HTTP::Client
private def exec_internal(request : HTTP::Request)
request.scheme = "https" if tls?
stub = WebMock.find_stub(request)
request.headers["Host"] = host_header unless request.headers.has_key?("Host")
stub = if WebMock.consume_after_match?
WebMock.consume_stub(request)
else
WebMock.find_stub(request)
end
return stub.exec(request) if stub

if WebMock.allows_net_connect?
Expand Down
8 changes: 8 additions & 0 deletions src/webmock/stub_registry.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ struct WebMock::StubRegistry
def find_stub(request)
@stubs.find &.matches?(request)
end

def consume_stub(request)
stub = find_stub(request)
if stub
@stubs.delete(stub)
end
stub
end
end