Gert Cuykens Gert Cuykens - 1 year ago 58
HTTP Question

Why does http.Get result in two requests instead of one?

Create a test file

and run
go test

Why does the following print two lines instead of just the first one?



=== RUN TestRootHandler
./bower_components/ ???
--- PASS: TestRootHandler (0.00s)
main_test.go:32: 200 - ./bower_components/Hello World.html


// RootHandler for HTTP
func RootHandler(root string, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := os.Open(root + r.URL.Path)
if err != nil {
h.ServeHTTP(w, r)
fmt.Println(root + r.URL.Path)
r.URL.Path = root + r.URL.Path
h.ServeHTTP(w, r)

// TestRootHandler
func TestRootHandler(t *testing.T) {
ts := httptest.NewServer(RootHandler("./bower_components", http.FileServer(http.Dir("./"))))
defer ts.Close()
res, err := http.Get(ts.URL + "/index.html")
if err != nil {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Logf("%d - %s", res.StatusCode, body)

Let me know if you don't understand the question then I will setup a github repository so you can just run the go test command to see what I mean.

Answer Source

That's just the way how http.FileServer() is written. Quoting from its doc:

As a special case, the returned file server redirects any request ending in "/index.html" to the same path, without the final "index.html".

This is what you experience: you were requesting /bower_components/index.html, so the handler returned by http.FileServer() sends a redirect:

HTTP/1.1 301 Moved Permanently
Location: ./

And http.Get() being well-behaved, follows this redirect, and performs another HTTP GET, now without the index.html, and the http.FileServer() handler will try and serve the index.html in such case.