Skip to content

Commit

Permalink
Merge pull request julienschmidt#67 from philippfranke/master
Browse files Browse the repository at this point in the history
Added convenient shortcut for OPTIONS
  • Loading branch information
julienschmidt committed Mar 21, 2015
2 parents f88e0d3 + 996b173 commit ddb6c34
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ func (r *Router) HEAD(path string, handle Handle) {
r.Handle("HEAD", path, handle)
}

// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
func (r *Router) OPTIONS(path string, handle Handle) {
r.Handle("OPTIONS", path, handle)
}

// POST is a shortcut for router.Handle("POST", path, handle)
func (r *Router) POST(path string, handle Handle) {
r.Handle("POST", path, handle)
Expand Down
11 changes: 10 additions & 1 deletion router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (h handlerStruct) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func TestRouterAPI(t *testing.T) {
var get, head, post, put, patch, delete, handler, handlerFunc bool
var get, head, options, post, put, patch, delete, handler, handlerFunc bool

httpHandler := handlerStruct{&handler}

Expand All @@ -87,6 +87,9 @@ func TestRouterAPI(t *testing.T) {
router.HEAD("/GET", func(w http.ResponseWriter, r *http.Request, _ Params) {
head = true
})
router.OPTIONS("/GET", func(w http.ResponseWriter, r *http.Request, _ Params) {
options = true
})
router.POST("/POST", func(w http.ResponseWriter, r *http.Request, _ Params) {
post = true
})
Expand Down Expand Up @@ -118,6 +121,12 @@ func TestRouterAPI(t *testing.T) {
t.Error("routing HEAD failed")
}

r, _ = http.NewRequest("OPTIONS", "/GET", nil)
router.ServeHTTP(w, r)
if !head {
t.Error("routing OPTIONS failed")
}

r, _ = http.NewRequest("POST", "/POST", nil)
router.ServeHTTP(w, r)
if !post {
Expand Down

0 comments on commit ddb6c34

Please sign in to comment.