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 {
|
func New(matrixHostname, matrixBaseURL string) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodGet {
|
if r.Method != http.MethodGet {
|
||||||
http.NotFound(w, r)
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
w.Write([]byte("405 method not allowed\n"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,10 +25,12 @@ func New(matrixHostname, matrixBaseURL string) http.HandlerFunc {
|
||||||
w.Header().Add("content-type", "application/json")
|
w.Header().Add("content-type", "application/json")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte(`{"m.homeserver": {"base_url": "` + matrixBaseURL + `"}}`))
|
w.Write([]byte(`{"m.homeserver": {"base_url": "` + matrixBaseURL + `"}}`))
|
||||||
default:
|
case "/":
|
||||||
w.Header().Add("content-type", "text/html")
|
w.Header().Add("content-type", "text/html")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte(indexPage))
|
w.Write([]byte(indexPage))
|
||||||
|
default:
|
||||||
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,20 @@ func TestHandler(t *testing.T) {
|
||||||
wantStatusCode: http.StatusOK,
|
wantStatusCode: http.StatusOK,
|
||||||
wantBody: "Welcome to netflux.io",
|
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 {
|
for _, tc := range testCases {
|
||||||
|
@ -61,9 +75,12 @@ func TestHandler(t *testing.T) {
|
||||||
resp := rec.Result()
|
resp := rec.Result()
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
assert.Equal(t, tc.wantContentType, resp.Header.Get("content-type"))
|
|
||||||
assert.Equal(t, tc.wantStatusCode, resp.StatusCode)
|
assert.Equal(t, tc.wantStatusCode, resp.StatusCode)
|
||||||
|
|
||||||
|
if tc.wantContentType != "" {
|
||||||
|
assert.Equal(t, tc.wantContentType, resp.Header.Get("content-type"))
|
||||||
|
}
|
||||||
|
|
||||||
if tc.wantBody != "" {
|
if tc.wantBody != "" {
|
||||||
respBody, err := ioutil.ReadAll(resp.Body)
|
respBody, err := ioutil.ReadAll(resp.Body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
Loading…
Reference in New Issue