Skip to content

Commit

Permalink
add options shortcut (#455)
Browse files Browse the repository at this point in the history
* add options shortcut

* add test for options shortcut
  • Loading branch information
RobinTsai authored Nov 10, 2020
1 parent 1f7f1de commit e5d3175
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions web_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,8 @@ func (w *WebService) PATCH(subPath string) *RouteBuilder {
func (w *WebService) DELETE(subPath string) *RouteBuilder {
return new(RouteBuilder).typeNameHandler(w.typeNameHandleFunc).servicePath(w.rootPath).Method("DELETE").Path(subPath)
}

// OPTIONS is a shortcut for .Method("OPTIONS").Path(subPath)
func (w *WebService) OPTIONS(subPath string) *RouteBuilder {
return new(RouteBuilder).typeNameHandler(w.typeNameHandleFunc).servicePath(w.rootPath).Method("OPTIONS").Path(subPath)
}
18 changes: 18 additions & 0 deletions web_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,20 @@ func TestParameterDataTypeCustomization(t *testing.T) {
}
}

func TestOptionsShortcut(t *testing.T) {
tearDown()
ws := new(WebService).Path("")
ws.Route(ws.OPTIONS("/options").To(return200))
Add(ws)

httpRequest, _ := http.NewRequest("OPTIONS", "http://here.com/options", nil)
httpWriter := httptest.NewRecorder()
DefaultContainer.dispatch(httpWriter, httpRequest)
if got, want := httpWriter.Code, 200; got != want {
t.Errorf("got %v, want %v", got, want)
}
}

func newPanicingService() *WebService {
ws := new(WebService).Path("")
ws.Route(ws.GET("/fire").To(doPanic))
Expand Down Expand Up @@ -385,3 +399,7 @@ func doNothing(req *Request, resp *Response) {
func return204(req *Request, resp *Response) {
resp.WriteHeader(204)
}

func return200(req *Request, resp *Response) {
resp.WriteHeader(200)
}

0 comments on commit e5d3175

Please sign in to comment.