Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions method_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (m *MethodHandler) Handle(method string, handler http.Handler) *MethodHandl
method = strings.ToUpper(strings.TrimSpace(method))

if m.methodsAllowedStr == "" {
m.methodsAllowedStr = method
m.methodsAllowedStr = "OPTIONS, " + method
} else {
m.methodsAllowedStr += ", " + method
}
Expand Down Expand Up @@ -98,5 +98,7 @@ func (m *MethodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Allow#Examples
w.Header().Set("Allow", m.methodsAllowedStr)
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
if r.Method != http.MethodOptions {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
}
4 changes: 3 additions & 1 deletion method_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ func TestMethodHandler(t *testing.T) {
expect(t, http.MethodDelete, srv.URL+"/user/42").statusCode(http.StatusOK).
bodyEq("DELETE: remove user with ID: 42\n")
expect(t, http.MethodPut, srv.URL+"/user/42").statusCode(http.StatusMethodNotAllowed).
bodyEq("Method Not Allowed\n").headerEq("Allow", "GET, POST, DELETE")
bodyEq("Method Not Allowed\n").headerEq("Allow", "OPTIONS, GET, POST, DELETE")
expect(t, http.MethodOptions, srv.URL+"/user/42").statusCode(http.StatusOK).
headerEq("Allow", "OPTIONS, GET, POST, DELETE")
}