Преглед на файлове

Testing new Any routing.

Manu Mtz-Almeida преди 10 години
родител
ревизия
53329e4646
променени са 2 файла, в които са добавени 24 реда и са изтрити 1 реда
  1. 16 0
      routergroup.go
  2. 8 1
      routes_test.go

+ 16 - 0
routergroup.go

@@ -94,6 +94,22 @@ func (group *RouterGroup) UNLINK(relativePath string, handlers ...HandlerFunc) {
 	group.Handle("UNLINK", relativePath, handlers)
 	group.Handle("UNLINK", relativePath, handlers)
 }
 }
 
 
+func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) {
+	// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, WS, LINK, UNLINK, TRACE
+	group.Handle("GET", relativePath, handlers)
+	group.Handle("POST", relativePath, handlers)
+	group.Handle("PUT", relativePath, handlers)
+	group.Handle("PATCH", relativePath, handlers)
+	group.Handle("HEAD", relativePath, handlers)
+	group.Handle("OPTIONS", relativePath, handlers)
+	group.Handle("DELETE", relativePath, handlers)
+	group.Handle("CONNECT", relativePath, handlers)
+	group.Handle("WS", relativePath, handlers)
+	group.Handle("LINK", relativePath, handlers)
+	group.Handle("UNLINK", relativePath, handlers)
+	group.Handle("TRACE", relativePath, handlers)
+}
+
 // Static serves files from the given file system root.
 // Static serves files from the given file system root.
 // Internally a http.FileServer is used, therefore http.NotFound is used instead
 // Internally a http.FileServer is used, therefore http.NotFound is used instead
 // of the Router's NotFound handler.
 // of the Router's NotFound handler.

+ 8 - 1
routes_test.go

@@ -25,15 +25,22 @@ func performRequest(r http.Handler, method, path string) *httptest.ResponseRecor
 
 
 func testRouteOK(method string, t *testing.T) {
 func testRouteOK(method string, t *testing.T) {
 	passed := false
 	passed := false
+	passedAny := false
 	r := New()
 	r := New()
+	r.Any("/test2", func(c *Context) {
+		passedAny = true
+	})
 	r.Handle(method, "/test", HandlersChain{func(c *Context) {
 	r.Handle(method, "/test", HandlersChain{func(c *Context) {
 		passed = true
 		passed = true
 	}})
 	}})
 
 
 	w := performRequest(r, method, "/test")
 	w := performRequest(r, method, "/test")
-
 	assert.True(t, passed)
 	assert.True(t, passed)
 	assert.Equal(t, w.Code, http.StatusOK)
 	assert.Equal(t, w.Code, http.StatusOK)
+
+	performRequest(r, method, "/test2")
+	assert.True(t, passedAny)
+
 }
 }
 
 
 // TestSingleRouteOK tests that POST route is correctly invoked.
 // TestSingleRouteOK tests that POST route is correctly invoked.