Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start connection in Net::HTTP adapter for mocked requests #960

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/webmock/http_lib_adapters/net_http.rb
Expand Up @@ -77,6 +77,8 @@ def request(request, body = nil, &block)
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)

if webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
return start_without_connect { request(request, body, &block) } unless started?

@socket = Net::HTTP.socket_type.new
WebMock::CallbackRegistry.invoke_callbacks(
{lib: :net_http}, request_signature, webmock_response)
Expand Down
18 changes: 18 additions & 0 deletions spec/acceptance/net_http/net_http_spec.rb
Expand Up @@ -213,6 +213,24 @@ class TestMarshalingInWebMockNetHTTP
end
end

it "should start mocked Net::HTTP" do
uri = URI.parse('http://www.example.com/')
stub_http_request(:get, uri).to_return(body: "the body")

http = Net::HTTP.new(uri.host)
req = Net::HTTP::Get.new('/')
expected_started = [false, true]

expect(http).to receive(:request).twice.and_wrap_original do |m, *args, &block|
expect(expected_started.shift).to equal(m.receiver.started?)
m.call(*args, &block)
end

response = http.request(req)

expect(response.body).to eq("the body")
end

describe "connecting on Net::HTTP.start" do
before(:each) do
@http = Net::HTTP.new('www.google.com', 443)
Expand Down