Skip to content

Commit e5d3175

Browse files
authored
add options shortcut (#455)
* add options shortcut * add test for options shortcut
1 parent 1f7f1de commit e5d3175

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

web_service.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,8 @@ func (w *WebService) PATCH(subPath string) *RouteBuilder {
288288
func (w *WebService) DELETE(subPath string) *RouteBuilder {
289289
return new(RouteBuilder).typeNameHandler(w.typeNameHandleFunc).servicePath(w.rootPath).Method("DELETE").Path(subPath)
290290
}
291+
292+
// OPTIONS is a shortcut for .Method("OPTIONS").Path(subPath)
293+
func (w *WebService) OPTIONS(subPath string) *RouteBuilder {
294+
return new(RouteBuilder).typeNameHandler(w.typeNameHandleFunc).servicePath(w.rootPath).Method("OPTIONS").Path(subPath)
295+
}

web_service_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,20 @@ func TestParameterDataTypeCustomization(t *testing.T) {
299299
}
300300
}
301301

302+
func TestOptionsShortcut(t *testing.T) {
303+
tearDown()
304+
ws := new(WebService).Path("")
305+
ws.Route(ws.OPTIONS("/options").To(return200))
306+
Add(ws)
307+
308+
httpRequest, _ := http.NewRequest("OPTIONS", "http://here.com/options", nil)
309+
httpWriter := httptest.NewRecorder()
310+
DefaultContainer.dispatch(httpWriter, httpRequest)
311+
if got, want := httpWriter.Code, 200; got != want {
312+
t.Errorf("got %v, want %v", got, want)
313+
}
314+
}
315+
302316
func newPanicingService() *WebService {
303317
ws := new(WebService).Path("")
304318
ws.Route(ws.GET("/fire").To(doPanic))
@@ -385,3 +399,7 @@ func doNothing(req *Request, resp *Response) {
385399
func return204(req *Request, resp *Response) {
386400
resp.WriteHeader(204)
387401
}
402+
403+
func return200(req *Request, resp *Response) {
404+
resp.WriteHeader(200)
405+
}

0 commit comments

Comments
 (0)