Browse Source

Named router fixed + test coverage

tags/v0.9.0
jimzhan 9 years ago
parent
commit
aa2b4405ef
2 changed files with 17 additions and 2 deletions
  1. +3
    -2
      server.go
  2. +14
    -0
      server_test.go

+ 3
- 2
server.go View File

"log" "log"
"net/http" "net/http"
"path/filepath" "path/filepath"
"reflect"
"runtime" "runtime"
"strings"
"sync" "sync"
"time" "time"




// register adds the http.Handler/http.HandleFunc into Gorilla mux. // register adds the http.Handler/http.HandleFunc into Gorilla mux.
func (self *server) register(pattern string, handler interface{}, methods ...string) { func (self *server) register(pattern string, handler interface{}, methods ...string) {
var name = strings.Join(methods, "|") + ":" + pattern
// 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:

+ 14
- 0
server_test.go View File

}) })
} }


func TestName(t *testing.T) {
Convey("rex.Name", t, func() {
app := New()
app.GET("/login", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})

request, _ := http.NewRequest("GET", "/login", nil)
response := httptest.NewRecorder()
app.ServeHTTP(response, request)
So(app.Name(request), ShouldEqual, "GET:/login")
})
}

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) {

Loading…
Cancel
Save