Sfoglia il codice sorgente

add Server.Any for common HTTP methods

tags/v0.9.0
jimzhan 9 anni fa
parent
commit
52fe696cc2
2 ha cambiato i file con 66 aggiunte e 12 eliminazioni
  1. +15
    -9
      server.go
  2. +51
    -3
      server_test.go

+ 15
- 9
server.go Vedi File

@@ -44,57 +44,63 @@ func (self *Server) build() http.Handler {
}

// register adds the http.Handler/http.HandleFunc into Gorilla mux.
func (self *Server) register(method string, pattern string, handler interface{}) {
func (self *Server) register(pattern string, handler interface{}, methods ...string) {
// finds the full function name (with package) as its mappings.
var name = runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name()

switch H := handler.(type) {
case http.Handler:
self.mux.Handle(pattern, H).Methods(method).Name(name)
self.mux.Handle(pattern, H).Methods(methods...).Name(name)

case func(http.ResponseWriter, *http.Request):
self.mux.HandleFunc(pattern, H).Methods(method).Name(name)
self.mux.HandleFunc(pattern, H).Methods(methods...).Name(name)

default:
Fatalf("Unsupported handler (%s) passed in.", name)
}
}

// Any maps most common HTTP methods request to the given `http.Handler`.
// Supports: GET | POST | PUT | DELETE | OPTIONS | HEAD
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("GET", pattern, handler)
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("HEAD", pattern, handler)
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("OPTIONS", pattern, handler)
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("POST", pattern, handler)
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("PUT", pattern, handler)
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("Delete", pattern, handler)
self.register(pattern, handler, "DELETE")
}

// Group creates a new application group under the given path prefix.

+ 51
- 3
server_test.go Vedi File

@@ -9,6 +9,57 @@ import (
. "github.com/smartystreets/goconvey/convey"
)

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

switch r.Method {
case "GET":
w.WriteHeader(http.StatusOK)

case "POST":
w.WriteHeader(http.StatusCreated)

case "PUT":
w.WriteHeader(http.StatusAccepted)

case "DELETE":
w.WriteHeader(http.StatusGone)

default:
w.Header().Set("X-HTTP-Method", r.Method)
}
})

Convey("rex.Any", t, func() {
var (
request *http.Request
response *httptest.ResponseRecorder
)
request, _ = http.NewRequest("GET", "/", nil)
response = httptest.NewRecorder()
app.ServeHTTP(response, request)
So(response.Code, ShouldEqual, http.StatusOK)

request, _ = http.NewRequest("POST", "/", nil)
response = httptest.NewRecorder()
app.ServeHTTP(response, request)
So(response.Code, ShouldEqual, http.StatusCreated)

request, _ = http.NewRequest("PUT", "/", nil)
response = httptest.NewRecorder()
app.ServeHTTP(response, request)
So(response.Code, ShouldEqual, http.StatusAccepted)

request, _ = http.NewRequest("DELETE", "/", nil)
response = httptest.NewRecorder()
app.ServeHTTP(response, request)
So(response.Code, ShouldEqual, http.StatusGone)
})
}

func TestGet(t *testing.T) {
app := New()
app.Get("/", func(w http.ResponseWriter, r *http.Request) {
@@ -17,7 +68,6 @@ func TestGet(t *testing.T) {
})

Convey("rex.Get", t, func() {

request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()

@@ -36,7 +86,6 @@ func TestPost(t *testing.T) {
})

Convey("rex.Post", t, func() {

request, _ := http.NewRequest("POST", "/", nil)
response := httptest.NewRecorder()

@@ -55,7 +104,6 @@ func TestPut(t *testing.T) {
})

Convey("rex.Put", t, func() {

request, _ := http.NewRequest("PUT", "/", nil)
response := httptest.NewRecorder()


Loading…
Annulla
Salva