Fix ServerDiesTest and FailureTest which depended on you running apache.

This CL fixes the issue that URL in both of these tests defaulted to
no-port which meaned the tests were going to http://localhost.
On some machines, that might point to an existing server running
on port 80 (for me it was apache2 configured by
autotest). This CL changes FailureTest to use a different
hostname that should never match. In the case of the ServerDiesTest,
we use the correct port of the server we kill during the test.

I've also added documentation to what these tests should actually do.

BUG=chromium:383200
TEST=Ran unittests with and without apache up.

Change-Id: I6b4168eada48dcd59fad23ed7ff6577b18e4ef81
Reviewed-on: https://chromium-review.googlesource.com/203347
Reviewed-by: Chris Sosa <sosa@chromium.org>
Commit-Queue: Chris Sosa <sosa@chromium.org>
Tested-by: Chris Sosa <sosa@chromium.org>
diff --git a/http_fetcher_unittest.cc b/http_fetcher_unittest.cc
index 3392098..b2848f3 100644
--- a/http_fetcher_unittest.cc
+++ b/http_fetcher_unittest.cc
@@ -679,6 +679,9 @@
 }
 
 namespace {
+// This delegate kills the server attached to it after receiving any bytes.
+// This can be used for testing what happens when you try to fetch data and
+// the server dies.
 class FailureHttpFetcherTestDelegate : public HttpFetcherDelegate {
  public:
   FailureHttpFetcherTestDelegate(PythonHttpServer* server)
@@ -704,7 +707,7 @@
   }
   virtual void TransferComplete(HttpFetcher* fetcher, bool successful) {
     EXPECT_FALSE(successful);
-    EXPECT_EQ(404, fetcher->http_response_code());
+    EXPECT_EQ(0, fetcher->http_response_code());
     g_main_loop_quit(loop_);
   }
   virtual void TransferTerminated(HttpFetcher* fetcher) {
@@ -717,6 +720,8 @@
 
 
 TYPED_TEST(HttpFetcherTest, FailureTest) {
+  // This test ensures that a fetcher responds correctly when a server isn't
+  // available at all.
   if (this->test_.IsMock())
     return;
   GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
@@ -728,7 +733,7 @@
 
     StartTransferArgs start_xfer_args = {
       fetcher.get(),
-      this->test_.SmallUrl(0)
+      "http://host_doesnt_exist99999999",
     };
 
     g_timeout_add(0, StartTransfer, &start_xfer_args);
@@ -740,18 +745,26 @@
 }
 
 TYPED_TEST(HttpFetcherTest, ServerDiesTest) {
+  // This test starts a new http server and kills it after receiving its first
+  // set of bytes. It test whether or not our fetcher eventually gives up on
+  // retries and aborts correctly.
   if (this->test_.IsMock())
     return;
   GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
   {
-    FailureHttpFetcherTestDelegate delegate(new PythonHttpServer);
+    PythonHttpServer* server = new PythonHttpServer();
+    int port = server->GetPort();
+    ASSERT_TRUE(server->started_);
+
+    // Handles destruction and claims ownership.
+    FailureHttpFetcherTestDelegate delegate(server);
     delegate.loop_ = loop;
     scoped_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher());
     fetcher->set_delegate(&delegate);
 
     StartTransferArgs start_xfer_args = {
       fetcher.get(),
-      LocalServerUrlForPath(0,
+      LocalServerUrlForPath(port,
                             base::StringPrintf("/flaky/%d/%d/%d/%d", kBigLength,
                                                kFlakyTruncateLength,
                                                kFlakySleepEvery,