Improve 404 and 405 handling
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
d0fd4d1461
commit
6db924f7d5
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue