Przeglądaj źródła

add Server.Any for common HTTP methods

tags/v0.9.0
jimzhan 10 lat temu
rodzic
commit
52fe696cc2
2 zmienionych plików z 66 dodań i 12 usunięć
  1. +15
    -9
      server.go
  2. +51
    -3
      server_test.go

+ 15
- 9
server.go Wyświetl plik

} }


// register adds the http.Handler/http.HandleFunc into Gorilla mux. // 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. // finds the full function name (with package) as its mappings.
var name = runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name() var name = runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name()


switch H := handler.(type) { switch H := handler.(type) {
case http.Handler: 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): 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: default:
Fatalf("Unsupported handler (%s) passed in.", name) 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"), // 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. // it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) Get(pattern string, handler interface{}) { 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") // 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. // it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) Head(pattern string, handler interface{}) { 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") // 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. // 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. // NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do.
func (self *Server) Options(pattern string, handler interface{}) { 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") // 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. // it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) Post(pattern string, handler interface{}) { 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") // 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. // it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) Put(pattern string, handler interface{}) { 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") // 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. // it also fetch the full function name of the handler (with package) to name the route.
func (self *Server) Delete(pattern string, handler interface{}) { 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. // Group creates a new application group under the given path prefix.

+ 51
- 3
server_test.go Wyświetl plik

. "github.com/smartystreets/goconvey/convey" . "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) { func TestGet(t *testing.T) {
app := New() app := New()
app.Get("/", func(w http.ResponseWriter, r *http.Request) { app.Get("/", func(w http.ResponseWriter, r *http.Request) {
}) })


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

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


}) })


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

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


}) })


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

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



Ładowanie…
Anuluj
Zapisz