Parcourir la source

comply the captital HTTP method

tags/v0.9.0
jimzhan il y a 9 ans
Parent
révision
0e911fd740
3 fichiers modifiés avec 70 ajouts et 58 suppressions
  1. +5
    -5
      example/main.go
  2. +49
    -37
      server.go
  3. +16
    -16
      server_test.go

+ 5
- 5
example/main.go Voir le fichier

@@ -87,14 +87,14 @@ func remove(w http.ResponseWriter, r *http.Request) {
func main() {
app := rex.New()
app.Use(livereload.Middleware)
app.Get("/", Index)
app.GET("/", Index)

api := app.Group("/v1/")
api.Use(JSON)
api.Get("/", fetch)
api.Post("/", create)
api.Put("/", update)
api.Delete("/", remove)
api.GET("/", fetch)
api.POST("/", create)
api.PUT("/", update)
api.DELETE("/", remove)

app.Run()
}

+ 49
- 37
server.go Voir le fichier

@@ -90,43 +90,6 @@ func (self *Server) Any(pattern string, handler interface{}) {
self.register(pattern, handler, "GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD")
}

// Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"),
// it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) Get(pattern string, handler interface{}) {
self.register(pattern, handler, "GET")
}

// Head is a shortcut for mux.HandleFunc(pattern, handler).Methods("HEAD")
// it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) Head(pattern string, handler interface{}) {
self.register(pattern, handler, "HEAD")
}

// Options is a shortcut for mux.HandleFunc(pattern, handler).Methods("OPTIONS")
// it also fetch the full function name of the handler (with package) to name the route.
// NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do.
func (self *Server) Options(pattern string, handler interface{}) {
self.register(pattern, handler, "OPTIONS")
}

// Post is a shortcut for mux.HandleFunc(pattern, handler).Methods("POST")
// it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) Post(pattern string, handler interface{}) {
self.register(pattern, handler, "POST")
}

// Put is a shortcut for mux.HandleFunc(pattern, handler).Methods("PUT")
// it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) Put(pattern string, handler interface{}) {
self.register(pattern, handler, "PUT")
}

// Delete is a shortcut for mux.HandleFunc(pattern, handler).Methods("DELETE")
// it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) Delete(pattern string, handler interface{}) {
self.register(pattern, handler, "DELETE")
}

// Group creates a new application group under the given path prefix.
func (self *Server) Group(prefix string) *Server {
var middleware = new(middleware)
@@ -163,6 +126,55 @@ func (self *Server) Use(module func(http.Handler) http.Handler) {
self.middleware.stack = append(self.middleware.stack, module)
}

// GET is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"),
// it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) GET(pattern string, handler interface{}) {
self.register(pattern, handler, "GET")
}

// HEAD is a shortcut for mux.HandleFunc(pattern, handler).Methods("HEAD")
// it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) HEAD(pattern string, handler interface{}) {
self.register(pattern, handler, "HEAD")
}

// OPTIONS is a shortcut for mux.HandleFunc(pattern, handler).Methods("OPTIONS")
// it also fetch the full function name of the handler (with package) to name the route.
// NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do.
func (self *Server) OPTIONS(pattern string, handler interface{}) {
self.register(pattern, handler, "OPTIONS")
}

// POST is a shortcut for mux.HandleFunc(pattern, handler).Methods("POST")
// it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) POST(pattern string, handler interface{}) {
self.register(pattern, handler, "POST")
}

// PUT is a shortcut for mux.HandleFunc(pattern, handler).Methods("PUT")
// it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) PUT(pattern string, handler interface{}) {
self.register(pattern, handler, "PUT")
}

// DELETE is a shortcut for mux.HandleFunc(pattern, handler).Methods("DELETE")
// it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) DELETE(pattern string, handler interface{}) {
self.register(pattern, handler, "DELETE")
}

// TRACE is a shortcut for mux.HandleFunc(pattern, handler).Methods("TRACE")
// it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) TRACE(pattern string, handler interface{}) {
self.register(pattern, handler, "TRACE")
}

// CONNECT is a shortcut for mux.HandleFunc(pattern, handler).Methods("CONNECT")
// it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) CONNECT(pattern string, handler interface{}) {
self.register(pattern, handler, "CONNECT")
}

// ServeHTTP dispatches the request to the handler whose
// pattern most closely matches the request URL.
func (self *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {

+ 16
- 16
server_test.go Voir le fichier

@@ -62,14 +62,14 @@ func TestAny(t *testing.T) {
})
}

func TestGet(t *testing.T) {
func TestGET(t *testing.T) {
app := New()
app.Get("/", func(w http.ResponseWriter, r *http.Request) {
app.GET("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Powered-By", "rex")
w.Header().Set("Content-Type", "application/json")
})

Convey("rex.Get", t, func() {
Convey("rex.GET", t, func() {
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()

@@ -80,14 +80,14 @@ func TestGet(t *testing.T) {
})
}

func TestPost(t *testing.T) {
func TestPOST(t *testing.T) {
app := New()
app.Post("/", func(w http.ResponseWriter, r *http.Request) {
app.POST("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Powered-By", "rex")
w.Header().Set("Content-Type", "application/json")
})

Convey("rex.Post", t, func() {
Convey("rex.POST", t, func() {
request, _ := http.NewRequest("POST", "/", nil)
response := httptest.NewRecorder()

@@ -98,14 +98,14 @@ func TestPost(t *testing.T) {
})
}

func TestPut(t *testing.T) {
func TestPUT(t *testing.T) {
app := New()
app.Put("/", func(w http.ResponseWriter, r *http.Request) {
app.PUT("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Powered-By", "rex")
w.Header().Set("Content-Type", "application/json")
})

Convey("rex.Put", t, func() {
Convey("rex.PUT", t, func() {
request, _ := http.NewRequest("PUT", "/", nil)
response := httptest.NewRecorder()

@@ -116,14 +116,14 @@ func TestPut(t *testing.T) {
})
}

func TestDelete(t *testing.T) {
func TestDELETE(t *testing.T) {
app := New()
app.Delete("/", func(w http.ResponseWriter, r *http.Request) {
app.DELETE("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Powered-By", "rex")
w.Header().Set("Content-Type", "application/json")
})

Convey("rex.Delete", t, func() {
Convey("rex.DELETE", t, func() {
request, _ := http.NewRequest("DELETE", "/", nil)
response := httptest.NewRecorder()

@@ -136,11 +136,11 @@ func TestDelete(t *testing.T) {

func TestGroup(t *testing.T) {
app := New()
app.Get("/", func(w http.ResponseWriter, r *http.Request) {
app.GET("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "index")
})
user := app.Group("/users")
user.Get("/", func(w http.ResponseWriter, r *http.Request) {
user.GET("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Powered-By", "rex")
})

@@ -183,7 +183,7 @@ func TestFileServer(t *testing.T) {

func TestUse(t *testing.T) {
app := New()
app.Get("/", func(w http.ResponseWriter, r *http.Request) {
app.GET("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "index")
})
app.Use(func(next http.Handler) http.Handler {
@@ -203,7 +203,7 @@ func TestUse(t *testing.T) {
func TestVars(t *testing.T) {
Convey("rex.Vars", t, func() {
app := New()
app.Get("/users/{id}", func(w http.ResponseWriter, r *http.Request) {
app.GET("/users/{id}", func(w http.ResponseWriter, r *http.Request) {
vars := app.Vars(r)
So(vars["id"], ShouldEqual, "123")
})

Chargement…
Annuler
Enregistrer