From 1f849cf54d09e106002f24257b1cf8b31780c97a Mon Sep 17 00:00:00 2001 From: LE Manh Cuong Date: Thu, 11 Oct 2018 10:16:23 +0700 Subject: [PATCH] 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 Run-TryBot: Brad Fitzpatrick --- cmd/godoc/main.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/cmd/godoc/main.go b/cmd/godoc/main.go index 19b5a453..5d633877 100644 --- a/cmd/godoc/main.go +++ b/cmd/godoc/main.go @@ -29,13 +29,13 @@ package main 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 @@ var ( 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 @@ func handleURLFlag() { // 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")