Add OPTIONS to list in Allow header Fixes #132
diff --git a/router.go b/router.go index f18a678..bb17330 100644 --- a/router.go +++ b/router.go
@@ -294,14 +294,12 @@ } func (r *Router) allowed(path, reqMethod string) (allow string) { - for method := range r.trees { - // Skip the requested method - we already tried this one - if method == reqMethod || method == "OPTIONS" { - continue - } + if path == "*" { // server-wide + for method := range r.trees { + if method == "OPTIONS" { + continue + } - handle, _, _ := r.trees[method].getValue(path) - if handle != nil { // add request method to list of allowed methods if len(allow) == 0 { allow = method @@ -309,6 +307,26 @@ allow += ", " + method } } + } else { // specific path + for method := range r.trees { + // Skip the requested method - we already tried this one + if method == reqMethod || method == "OPTIONS" { + continue + } + + handle, _, _ := r.trees[method].getValue(path) + if handle != nil { + // add request method to list of allowed methods + if len(allow) == 0 { + allow = method + } else { + allow += ", " + method + } + } + } + } + if len(allow) > 0 { + allow += ", OPTIONS" } return } @@ -361,24 +379,7 @@ if req.Method == "OPTIONS" { // Handle OPTIONS requests if r.HandleOPTIONS { - var allow string - if path == "*" { // server OPTIONS - for method := range r.trees { - if method == "OPTIONS" { - continue - } - - // add request method to list of allowed methods - if len(allow) == 0 { - allow = method - } else { - allow += ", " + method - } - } - } else { // path OPTIONS - allow = r.allowed(path, req.Method) - } - if len(allow) > 0 { + if allow := r.allowed(path, req.Method); len(allow) > 0 { w.Header().Set("Allow", allow) return }
diff --git a/router_test.go b/router_test.go index db57740..88895a3 100644 --- a/router_test.go +++ b/router_test.go
@@ -229,7 +229,7 @@ router.ServeHTTP(w, r) if !(w.Code == http.StatusOK) { t.Errorf("OPTIONS handling failed: Code=%d, Header=%v", w.Code, w.Header()) - } else if allow := w.Header().Get("Allow"); allow != "POST" { + } else if allow := w.Header().Get("Allow"); allow != "POST, OPTIONS" { t.Error("unexpected Allow header value: " + allow) } @@ -239,7 +239,7 @@ router.ServeHTTP(w, r) if !(w.Code == http.StatusOK) { t.Errorf("OPTIONS handling failed: Code=%d, Header=%v", w.Code, w.Header()) - } else if allow := w.Header().Get("Allow"); allow != "POST" { + } else if allow := w.Header().Get("Allow"); allow != "POST, OPTIONS" { t.Error("unexpected Allow header value: " + allow) } @@ -260,7 +260,7 @@ router.ServeHTTP(w, r) if !(w.Code == http.StatusOK) { t.Errorf("OPTIONS handling failed: Code=%d, Header=%v", w.Code, w.Header()) - } else if allow := w.Header().Get("Allow"); allow != "POST, GET" && allow != "GET, POST" { + } else if allow := w.Header().Get("Allow"); allow != "POST, GET, OPTIONS" && allow != "GET, POST, OPTIONS" { t.Error("unexpected Allow header value: " + allow) } @@ -270,7 +270,7 @@ router.ServeHTTP(w, r) if !(w.Code == http.StatusOK) { t.Errorf("OPTIONS handling failed: Code=%d, Header=%v", w.Code, w.Header()) - } else if allow := w.Header().Get("Allow"); allow != "POST, GET" && allow != "GET, POST" { + } else if allow := w.Header().Get("Allow"); allow != "POST, GET, OPTIONS" && allow != "GET, POST, OPTIONS" { t.Error("unexpected Allow header value: " + allow) } @@ -287,7 +287,7 @@ router.ServeHTTP(w, r) if !(w.Code == http.StatusOK) { t.Errorf("OPTIONS handling failed: Code=%d, Header=%v", w.Code, w.Header()) - } else if allow := w.Header().Get("Allow"); allow != "POST, GET" && allow != "GET, POST" { + } else if allow := w.Header().Get("Allow"); allow != "POST, GET, OPTIONS" && allow != "GET, POST, OPTIONS" { t.Error("unexpected Allow header value: " + allow) } if custom { @@ -318,7 +318,7 @@ router.ServeHTTP(w, r) if !(w.Code == http.StatusMethodNotAllowed) { t.Errorf("NotAllowed handling failed: Code=%d, Header=%v", w.Code, w.Header()) - } else if allow := w.Header().Get("Allow"); allow != "POST" { + } else if allow := w.Header().Get("Allow"); allow != "POST, OPTIONS" { t.Error("unexpected Allow header value: " + allow) } @@ -332,7 +332,7 @@ router.ServeHTTP(w, r) if !(w.Code == http.StatusMethodNotAllowed) { t.Errorf("NotAllowed handling failed: Code=%d, Header=%v", w.Code, w.Header()) - } else if allow := w.Header().Get("Allow"); allow != "POST, DELETE" && allow != "DELETE, POST" { + } else if allow := w.Header().Get("Allow"); allow != "POST, DELETE, OPTIONS" && allow != "DELETE, POST, OPTIONS" { t.Error("unexpected Allow header value: " + allow) } @@ -350,7 +350,7 @@ if w.Code != http.StatusTeapot { t.Errorf("unexpected response code %d want %d", w.Code, http.StatusTeapot) } - if allow := w.Header().Get("Allow"); allow != "POST, DELETE" && allow != "DELETE, POST" { + if allow := w.Header().Get("Allow"); allow != "POST, DELETE, OPTIONS" && allow != "DELETE, POST, OPTIONS" { t.Error("unexpected Allow header value: " + allow) } }