webmock icon indicating copy to clipboard operation
webmock copied to clipboard

net_connect_explicit_allowed? should not be case-sensitive

Open dmolesUC opened this issue 6 years ago • 2 comments

By the time a URI gets to net_connect_explicit_allowed?, the hostname has been lowercased, but the comparison with Config.instance.allow is still case-sensitive.

Ordinarily this isn't an issue, but I ran into it testing on a Mac with some code that generated URLs with Socket.gethostname, which returned a title-cased name, in this case Anachronic.local.

RSpec test case:

describe WebMock do
  describe :disable_net_connect! do
    before(:each) do
      WebMock.allow_net_connect! # just to be sure
    end
    describe :allow do
      it 'should accept Socket.gethostname' do
        hostname = Socket.gethostname # will be title-cased on macOS
        WebMock.disable_net_connect!(allow: hostname)
        port = 8080 # or some other port you do have something running on
        uri = URI.parse("http://#{hostname}:#{port}/")
        resp = Net::HTTP.get(uri)
        expect(resp).not_to be_nil
      end

      it 'should accept anything in uppercase, really' do
        hostname = "EXAMPLE.ORG"
        WebMock.disable_net_connect!(allow: hostname)
        uri = URI.parse("http://#{hostname}/")
        resp = Net::HTTP.get(uri)
        expect(resp).not_to be_nil
      end
    end
  end
end

It's not hard to work around once you know about it (just downcase any hostname before passing it as allow:) but it is inconsistent.

dmolesUC avatar May 16 '19 22:05 dmolesUC

Thank you @dmolesUC3! If you would you like to provide a PR with the fix, you are very welcome.

bblimke avatar May 16 '19 22:05 bblimke

I'll try to have a look—if not today then some time next week.

dmolesUC avatar May 17 '19 16:05 dmolesUC