unindexed icon indicating copy to clipboard operation
unindexed copied to clipboard

Can't reproduce FileServer chi/example - 404 page not found

Open sinetoami opened this issue 6 years ago • 0 comments

I've tried a tons of solutions to get work a file server on my project. Even in my test the standard example does not work properly. What I ever receive is 404 page not found. Here's a piece of my code, including server and test:

## ./server.go
func newRouter() *chi.Mux {
	r := chi.NewRouter()
	r.Mount("/api/v3", routes())
	r.Handle("/*", http.FileServer(unindexed.Dir("../assets/")))
	return r
}

func routes() http.Handler {
	r := chi.NewRouter()
	r.Get("/", home)
	return r
}

## ./server_test.go
func TestAssetsFileServer(t *testing.T) {
	r := newRouter()
	mockServer := httptest.NewServer(r)

	resp, err := http.Get(mockServer.URL + "/api/v3/assets")
	if err != nil {
		t.Fatal(err)
	}

	if resp.StatusCode != http.StatusOK {
		t.Errorf("Status should be 200, got %d", resp.StatusCode)
	}

	contentType := resp.Header.Get("Content-Type")
	expectedContentType := "text/html; charset=utf-8"
	if expectedContentType != contentType {
		t.Errorf("Wrong content type, expected %s, got %s", expectedContentType, contentType)
	}
}

The version of my Golang is 1.13, working on Linux. So, what am I doing wrong? Thanks!

sinetoami avatar Oct 18 '19 02:10 sinetoami