Skip to content

net_http_connect_on_start: true does not correctly stub out URLs if it cannot be resolved by the DNS #914

@kryptonat

Description

@kryptonat

I'm running into file descriptor leakages and found out that you can set net_http_connect_on_start option to true as recommended here.

However when this option is enabled, it's not able to correctly stub out a request with subdomains URLs that fail to resolve DNS lookup. This seems like a bug.

I expect mocked URL's to not actually try to HTTP connect when net_http_connect_on_start is enabled, similar to the behavior when net_http_connect_on_start is disabled.

Actual

    WebMock.disable_net_connect!(net_http_connect_on_start: true)

    # Stubbing URL with subdomain, results in an error.
    stub_request(:get, 'http://api.foobar.com').to_return(body: 'hi')
    Net::HTTP.get(URI('http://api.foobar.com'))
    # => SocketError: Failed to open TCP connection to api.foobar.com:80 (getaddrinfo: nodename nor servname provided, or not known)
 
    # Stubbing URL with no subdomain, can be stubbed successfully.
    stub_request(:get, 'http://foobar.com').to_return(body: 'hi')
    Net::HTTP.get(URI('http://foobar.com'))
    # => "hi"

    # When net_http_connect_on_start defaults to false...
    WebMock.disable_net_connect!

    # Stubbing URL with subdomain, can be stubbed successfully.
    Net::HTTP.get(URI('http://api.foobar.com'))
    # => "hi"

Expected

    WebMock.disable_net_connect!(net_http_connect_on_start: true)
    stub_request(:get, 'http://api.foobar.com').to_return(body: 'hi')
    # => "hi"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions