Skip to content

Commit

Permalink
Add OPTIONS to list in Allow header
Browse files Browse the repository at this point in the history
  • Loading branch information
julienschmidt committed Feb 15, 2016
1 parent 2c34ec8 commit 153d9c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
51 changes: 26 additions & 25 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,21 +294,39 @@ func (r *Router) Lookup(method, path string) (Handle, Params, bool) {
}

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
} else {
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
}
Expand Down Expand Up @@ -361,24 +379,7 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
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
}
Expand Down
16 changes: 8 additions & 8 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func TestRouterOPTIONS(t *testing.T) {
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)
}

Expand All @@ -239,7 +239,7 @@ func TestRouterOPTIONS(t *testing.T) {
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)
}

Expand All @@ -260,7 +260,7 @@ func TestRouterOPTIONS(t *testing.T) {
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)
}

Expand All @@ -270,7 +270,7 @@ func TestRouterOPTIONS(t *testing.T) {
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)
}

Expand All @@ -287,7 +287,7 @@ func TestRouterOPTIONS(t *testing.T) {
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 {
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestRouterNotAllowed(t *testing.T) {
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)
}

Expand All @@ -332,7 +332,7 @@ func TestRouterNotAllowed(t *testing.T) {
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)
}

Expand All @@ -350,7 +350,7 @@ func TestRouterNotAllowed(t *testing.T) {
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)
}
}
Expand Down

0 comments on commit 153d9c9

Please sign in to comment.