diff --git a/handler/handler.go b/handler/handler.go index ebea200..5dfff06 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -11,7 +11,8 @@ var indexPage string func New(matrixHostname, matrixBaseURL string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { - http.NotFound(w, r) + w.WriteHeader(http.StatusMethodNotAllowed) + w.Write([]byte("405 method not allowed\n")) return } @@ -24,10 +25,12 @@ func New(matrixHostname, matrixBaseURL string) http.HandlerFunc { w.Header().Add("content-type", "application/json") w.WriteHeader(http.StatusOK) w.Write([]byte(`{"m.homeserver": {"base_url": "` + matrixBaseURL + `"}}`)) - default: + case "/": w.Header().Add("content-type", "text/html") w.WriteHeader(http.StatusOK) w.Write([]byte(indexPage)) + default: + http.NotFound(w, r) } } } diff --git a/handler/handler_test.go b/handler/handler_test.go index 5e0aa5f..8f7c414 100644 --- a/handler/handler_test.go +++ b/handler/handler_test.go @@ -49,6 +49,20 @@ func TestHandler(t *testing.T) { wantStatusCode: http.StatusOK, wantBody: "Welcome to netflux.io", }, + { + name: "page not found", + method: http.MethodGet, + path: "/foo", + wantStatusCode: http.StatusNotFound, + wantBody: "404 page not found", + }, + { + name: "wrong method", + method: http.MethodPost, + path: "/", + wantStatusCode: http.StatusMethodNotAllowed, + wantBody: "405 method not allowed", + }, } for _, tc := range testCases { @@ -61,9 +75,12 @@ func TestHandler(t *testing.T) { resp := rec.Result() defer resp.Body.Close() - assert.Equal(t, tc.wantContentType, resp.Header.Get("content-type")) assert.Equal(t, tc.wantStatusCode, resp.StatusCode) + if tc.wantContentType != "" { + assert.Equal(t, tc.wantContentType, resp.Header.Get("content-type")) + } + if tc.wantBody != "" { respBody, err := ioutil.ReadAll(resp.Body) require.NoError(t, err)