cmd/godoc: do not use httptest

httptest assumes it's running a test, it registers its own flags, which
means godoc ends up with mysterious flags.

By implement an http.ResponseWriter, we do not need to use httptest.

Fixes golang/go#28138

Change-Id: Ia0de8597c3edb0e7bdea6d8b3b2f1618a12f9239
Reviewed-on: https://go-review.googlesource.com/c/141417
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/cmd/godoc/main.go b/cmd/godoc/main.go
index 19b5a45..5d63387 100644
--- a/cmd/godoc/main.go
+++ b/cmd/godoc/main.go
@@ -29,13 +29,13 @@
 
 import (
 	"archive/zip"
+	"bytes"
 	_ "expvar" // to serve /debug/vars
 	"flag"
 	"fmt"
 	"go/build"
 	"log"
 	"net/http"
-	"net/http/httptest"
 	_ "net/http/pprof" // to serve /debug/pprof/*
 	"net/url"
 	"os"
@@ -94,6 +94,17 @@
 	notesRx = flag.String("notes", "BUG", "regular expression matching note markers to show")
 )
 
+// An httpResponseRecorder is an http.ResponseWriter
+type httpResponseRecorder struct {
+	body   *bytes.Buffer
+	header http.Header
+	code   int
+}
+
+func (w *httpResponseRecorder) Header() http.Header         { return w.header }
+func (w *httpResponseRecorder) Write(b []byte) (int, error) { return len(b), nil }
+func (w *httpResponseRecorder) WriteHeader(code int)        { w.code = code }
+
 func usage() {
 	fmt.Fprintf(os.Stderr, "usage: godoc -http="+defaultAddr+"\n")
 	flag.PrintDefaults()
@@ -122,22 +133,22 @@
 
 		// Invoke default HTTP handler to serve request
 		// to our buffering httpWriter.
-		w := httptest.NewRecorder()
+		w := &httpResponseRecorder{code: 200, header: make(http.Header), body: new(bytes.Buffer)}
 		http.DefaultServeMux.ServeHTTP(w, req)
 
 		// Return data, error, or follow redirect.
-		switch w.Code {
+		switch w.code {
 		case 200: // ok
-			os.Stdout.Write(w.Body.Bytes())
+			os.Stdout.Write(w.body.Bytes())
 			return
 		case 301, 302, 303, 307: // redirect
-			redirect := w.HeaderMap.Get("Location")
+			redirect := w.header.Get("Location")
 			if redirect == "" {
-				log.Fatalf("HTTP %d without Location header", w.Code)
+				log.Fatalf("HTTP %d without Location header", w.code)
 			}
 			urlstr = redirect
 		default:
-			log.Fatalf("HTTP error %d", w.Code)
+			log.Fatalf("HTTP error %d", w.code)
 		}
 	}
 	log.Fatalf("too many redirects")