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>
This commit is contained in:
parent
c75e7e6983
commit
1f849cf54d
|
@ -29,13 +29,13 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
|
"bytes"
|
||||||
_ "expvar" // to serve /debug/vars
|
_ "expvar" // to serve /debug/vars
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/build"
|
"go/build"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
|
||||||
_ "net/http/pprof" // to serve /debug/pprof/*
|
_ "net/http/pprof" // to serve /debug/pprof/*
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
@ -94,6 +94,17 @@ var (
|
||||||
notesRx = flag.String("notes", "BUG", "regular expression matching note markers to show")
|
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() {
|
func usage() {
|
||||||
fmt.Fprintf(os.Stderr, "usage: godoc -http="+defaultAddr+"\n")
|
fmt.Fprintf(os.Stderr, "usage: godoc -http="+defaultAddr+"\n")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
|
@ -122,22 +133,22 @@ func handleURLFlag() {
|
||||||
|
|
||||||
// Invoke default HTTP handler to serve request
|
// Invoke default HTTP handler to serve request
|
||||||
// to our buffering httpWriter.
|
// to our buffering httpWriter.
|
||||||
w := httptest.NewRecorder()
|
w := &httpResponseRecorder{code: 200, header: make(http.Header), body: new(bytes.Buffer)}
|
||||||
http.DefaultServeMux.ServeHTTP(w, req)
|
http.DefaultServeMux.ServeHTTP(w, req)
|
||||||
|
|
||||||
// Return data, error, or follow redirect.
|
// Return data, error, or follow redirect.
|
||||||
switch w.Code {
|
switch w.code {
|
||||||
case 200: // ok
|
case 200: // ok
|
||||||
os.Stdout.Write(w.Body.Bytes())
|
os.Stdout.Write(w.body.Bytes())
|
||||||
return
|
return
|
||||||
case 301, 302, 303, 307: // redirect
|
case 301, 302, 303, 307: // redirect
|
||||||
redirect := w.HeaderMap.Get("Location")
|
redirect := w.header.Get("Location")
|
||||||
if redirect == "" {
|
if redirect == "" {
|
||||||
log.Fatalf("HTTP %d without Location header", w.Code)
|
log.Fatalf("HTTP %d without Location header", w.code)
|
||||||
}
|
}
|
||||||
urlstr = redirect
|
urlstr = redirect
|
||||||
default:
|
default:
|
||||||
log.Fatalf("HTTP error %d", w.Code)
|
log.Fatalf("HTTP error %d", w.code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Fatalf("too many redirects")
|
log.Fatalf("too many redirects")
|
||||||
|
|
Loading…
Reference in New Issue