Handle HEAD request
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Rob Watson 2022-06-07 05:04:33 +02:00
parent 45c5b2ac2a
commit bfb0c305b8
2 changed files with 15 additions and 7 deletions

View File

@ -10,7 +10,7 @@ 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 && r.Method != http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("405 method not allowed\n")) w.Write([]byte("405 method not allowed\n"))
return return
@ -28,7 +28,9 @@ func New(matrixHostname, matrixBaseURL string) http.HandlerFunc {
case "/": case "/":
w.Header().Add("content-type", "text/html") w.Header().Add("content-type", "text/html")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
if r.Method == http.MethodGet {
w.Write([]byte(indexPage)) w.Write([]byte(indexPage))
}
default: default:
http.NotFound(w, r) http.NotFound(w, r)
} }

View File

@ -49,6 +49,14 @@ func TestHandler(t *testing.T) {
wantStatusCode: http.StatusOK, wantStatusCode: http.StatusOK,
wantBody: "Welcome to netflux.io", wantBody: "Welcome to netflux.io",
}, },
{
name: "HEAD /",
method: http.MethodHead,
path: "/",
wantContentType: "text/html",
wantStatusCode: http.StatusOK,
wantBody: "",
},
{ {
name: "page not found", name: "page not found",
method: http.MethodGet, method: http.MethodGet,
@ -81,11 +89,9 @@ func TestHandler(t *testing.T) {
assert.Equal(t, tc.wantContentType, resp.Header.Get("content-type")) assert.Equal(t, tc.wantContentType, resp.Header.Get("content-type"))
} }
if tc.wantBody != "" {
respBody, err := ioutil.ReadAll(resp.Body) respBody, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err) require.NoError(t, err)
assert.Contains(t, string(respBody), tc.wantBody) assert.Contains(t, string(respBody), tc.wantBody)
}
}) })
} }
} }