diff options
-rw-r--r-- | response.go | 9 | ||||
-rw-r--r-- | router.go | 16 | ||||
-rw-r--r-- | router_test.go | 11 |
3 files changed, 33 insertions, 3 deletions
diff --git a/response.go b/response.go index d7069d7..e71b546 100644 --- a/response.go +++ b/response.go @@ -1,7 +1,10 @@ package mooh type Response struct { - Location string - Content string - Status int + Headers map[string][]string + Content string + Status int +} + +func (self *Response) AddHeader(headerName string, headerValue string) { } diff --git a/router.go b/router.go index d4c9058..23b8214 100644 --- a/router.go +++ b/router.go @@ -19,6 +19,22 @@ func BuildRouter() *Router { return &router } +func (self *Router) AddOptions() *Router { + self.notAllowed = true + routes := self.GetRouteList() + for _, r := range routes { + m := self.GetMethodsForPath(r) + allowed := strings.Join(m, ", ") + defaultFn := func(req *Request) (Response, error) { + r := Response{Status: 204} + r.AddHeader("Allow", allowed) + return r, nil + } + self.AddRoute(&Route{Method: "OPTIONS", Path: r, Code: defaultFn}) + } + return self +} + func (self *Router) routeIsKnown(route *Route) bool { if self.knownPaths[route.Path] == nil { self.knownPaths[route.Path] = map[string]bool{} diff --git a/router_test.go b/router_test.go index de13225..07cf3f6 100644 --- a/router_test.go +++ b/router_test.go @@ -123,3 +123,14 @@ func TestGetMethodsForPath(t *testing.T) { t.Fatal() } } + +func TestAddNotAllowed(t *testing.T) { + r := BuildRouter() + r.AddRoute(&Route{Method: "GET", Path: "/foo", Code: testRoute}) + r.AddRoute(&Route{Method: "PUT", Path: "/foo", Code: testRoute}) + r = r.AddOptions() + m := r.GetMethodsForPath("/foo") + if len(m) != 3 { + t.Fatal() + } +} |