Skip to content

Commit ddb6c34

Browse files
committed
Merge pull request #67 from philippfranke/master
Added convenient shortcut for OPTIONS
2 parents f88e0d3 + 996b173 commit ddb6c34

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

router.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ func (r *Router) HEAD(path string, handle Handle) {
178178
r.Handle("HEAD", path, handle)
179179
}
180180

181+
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
182+
func (r *Router) OPTIONS(path string, handle Handle) {
183+
r.Handle("OPTIONS", path, handle)
184+
}
185+
181186
// POST is a shortcut for router.Handle("POST", path, handle)
182187
func (r *Router) POST(path string, handle Handle) {
183188
r.Handle("POST", path, handle)

router_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (h handlerStruct) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7676
}
7777

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

8181
httpHandler := handlerStruct{&handler}
8282

@@ -87,6 +87,9 @@ func TestRouterAPI(t *testing.T) {
8787
router.HEAD("/GET", func(w http.ResponseWriter, r *http.Request, _ Params) {
8888
head = true
8989
})
90+
router.OPTIONS("/GET", func(w http.ResponseWriter, r *http.Request, _ Params) {
91+
options = true
92+
})
9093
router.POST("/POST", func(w http.ResponseWriter, r *http.Request, _ Params) {
9194
post = true
9295
})
@@ -118,6 +121,12 @@ func TestRouterAPI(t *testing.T) {
118121
t.Error("routing HEAD failed")
119122
}
120123

124+
r, _ = http.NewRequest("OPTIONS", "/GET", nil)
125+
router.ServeHTTP(w, r)
126+
if !head {
127+
t.Error("routing OPTIONS failed")
128+
}
129+
121130
r, _ = http.NewRequest("POST", "/POST", nil)
122131
router.ServeHTTP(w, r)
123132
if !post {

0 commit comments

Comments
 (0)