Misc #10721


Failing test because of DNS server

Added by anthonycrumley (Anthony Crumley) over 7 years ago. Updated over 7 years ago.



The following test is failing:

[ 5/52] TestNetHTTP#test_failure_message_includes_failed_domain_and_port = 0.32 s

  1. Failure:
    TestNetHTTP#test_failure_message_includes_failed_domain_and_port [/vagrant/test/net/http/test_http.rb:196]:
    Expected "should have raised" to include "doesnotexist.bogus:80".

The problem is caused by my ISP, Charter Cable, returning a search results page instead of a failed DNS lookup for http://doesnotexist.bogus. If I change my DNS server setting on my computer to use Google DNS then the test passes.

The attached patch uses a MiniTest stub to produce the DNS failure which results in the expected error message regardless of how the machine's DNS server responds to http://doesnotexist.bogus.

Thanks for everything you all do,



dns_lookup.patch (1.06 KB) dns_lookup.patch anthonycrumley (Anthony Crumley), 01/09/2015 06:29 AM
dns_lookup_v2.patch (1.18 KB) dns_lookup_v2.patch anthonycrumley (Anthony Crumley), 01/10/2015 12:32 AM

Updated by anthonycrumley (Anthony Crumley) over 7 years ago

Improved the original solution by adding assert_raises.

Updated by zzak (Zachary Scott) over 7 years ago

  • Status changed from Open to Assigned

Were you working on a similar patch?

Updated by zzak (Zachary Scott) over 7 years ago

  • Assignee set to normalperson (Eric Wong)

Updated by normalperson (Eric Wong) over 7 years ago


Were you working on a similar patch?

Not really, r49175 was because I was offline completely.
Anthony's ISP is online and doing bad things, but his patch
looks good to me.

I can commit _v2 of his patch in a day or so (unless anybody has
objections). I'll shorten "the dns_lookup_failure_message" variable
name to "failure_message" so it fits in 80 columns.

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

You can use assert_raise_with_message.

diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb
index 4d82fd7..4f5093d 100644
--- a/test/net/http/test_http.rb
+++ b/test/net/http/test_http.rb
@@ -1,5 +1,6 @@
 # coding: US-ASCII
 require 'test/unit'
+require 'minitest/mock'
 require 'net/http'
 require 'stringio'
 require_relative 'utils'
@@ -189,11 +190,12 @@ class TestNetHTTP < Test::Unit::TestCase
   def test_failure_message_includes_failed_domain_and_port
-    begin
-      Net::HTTP.get(URI.parse("http://doesnotexist.bogus"))
-      fail "should have raised"
-    rescue => e
-      assert_includes e.message, "doesnotexist.bogus:80"
+    uri = URI.parse("http://doesnotexist.bogus")
+    assert_raise_with_message(RuntimeError, /doesnotexist.bogus:80/) do
+      failure_message = "SocketError: getaddrinfo: Name or service not known"
+      TCPSocket.stub :open, proc {raise failure_message} do
+        Net::HTTP.get(uri)
+      end

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

Applied in changeset r49231.

test_http.rb: test without DNS access

  • test/net/http/test_http.rb: get rid of accessing DNS actually
    for some servers returning wrong results.
    [ruby-core:67454] [Bug #10721]

Also available in: Atom PDF