diff --git a/context.go b/context.go index 8c11372d..f2646db7 100644 --- a/context.go +++ b/context.go @@ -26,8 +26,13 @@ func (c *Context) P(i uint8) string { } // Param returns path parameter by name. -func (c *Context) Param(n string) string { - return c.params.Get(n) +func (c *Context) Param(name string) (value string) { + for _, p := range c.params { + if p.Name == name { + value = p.Value + } + } + return } // Bind decodes the body into provided type based on Content-Type header. diff --git a/context_test.go b/context_test.go index 9c9def66..29190d57 100644 --- a/context_test.go +++ b/context_test.go @@ -50,13 +50,14 @@ func TestContext(t *testing.T) { // Param // //***********// // By id - if c.P(0) != "" { - t.Error("param id should be nil") + c.params = Params{{"id", "1"}} + if c.P(0) != "1" { + t.Error("param id should be 1") } // By name - if c.Param("id") != "" { - t.Error("param id should be nil") + if c.Param("id") != "1" { + t.Error("param id should be 1") } // Store diff --git a/echo.go b/echo.go index b1909908..afc2a83d 100644 --- a/echo.go +++ b/echo.go @@ -62,7 +62,7 @@ var ( ErrUnsupportedMediaType = errors.New("echo: unsupported media type") ) -// New creates a echo instance. +// New creates an Echo instance. func New() (e *Echo) { e = &Echo{ maxParam: 5, diff --git a/echo_test.go b/echo_test.go index de056ad4..09c27f62 100644 --- a/echo_test.go +++ b/echo_test.go @@ -19,7 +19,7 @@ var u1 = user{ Name: "Joe", } -// TODO: Fix me +// TODO: Improve me! func TestEchoMaxParam(t *testing.T) { e := New() e.MaxParam(8) @@ -201,26 +201,26 @@ func TestEchoMethod(t *testing.T) { e.Trace("/", func(*Context) {}) } -func TestEchoServeHTTP(t *testing.T) { +func TestEchoNotFound(t *testing.T) { e := New() - // OK - e.Get("/users", func(*Context) { - }) + // Default NotFound handler + r, _ := http.NewRequest(MethodGET, "/files", nil) w := httptest.NewRecorder() - r, _ := http.NewRequest(MethodGET, "/users", nil) - e.ServeHTTP(w, r) - if w.Code != http.StatusOK { - t.Errorf("status code should be 200, found %d", w.Code) - } - - // NotFound - r, _ = http.NewRequest(MethodGET, "/user", nil) - w = httptest.NewRecorder() e.ServeHTTP(w, r) if w.Code != http.StatusNotFound { t.Errorf("status code should be 404, found %d", w.Code) } + + // Customized NotFound handler + e.NotFoundHandler(func(c *Context) { + c.Text(404, "not found") + }) + w = httptest.NewRecorder() + e.ServeHTTP(w, r) + if w.Body.String() != "not found" { + t.Errorf("body should be `not found`") + } } func verifyUser(u2 *user, t *testing.T) { diff --git a/router.go b/router.go index fc8e3dc1..15cc2858 100644 --- a/router.go +++ b/router.go @@ -214,16 +214,6 @@ func (r *router) Find(method, path string) (h HandlerFunc, c *Context, echo *Ech } } -// Get returns path parameter by name. -func (ps Params) Get(n string) (v string) { - for _, p := range ps { - if p.Name == n { - v = p.Value - } - } - return -} - func (r *router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { h, c, _ := r.Find(req.Method, req.URL.Path) defer r.echo.pool.Put(c) diff --git a/router_test.go b/router_test.go index 865a4ecc..fa7190f6 100644 --- a/router_test.go +++ b/router_test.go @@ -2,9 +2,20 @@ package echo import ( "fmt" + "net/http" + "net/http/httptest" "testing" ) +type route struct { + method string + path string +} + +var api = []route{ + {"GET", "/authorizations"}, +} + func TestRouterStatic(t *testing.T) { r := New().Router r.Add(MethodGET, "/folders/files/echo.gif", func(c *Context) {}, nil) @@ -68,6 +79,25 @@ func TestRouterMicroParam(t *testing.T) { } } +func TestRouterAPI(t *testing.T) { + // r := New().Router +} + +func TestRouterServeHTTP(t *testing.T) { + r := New().Router + r.Add(MethodGET, "/users", func(c *Context) {}, nil) + + // OK + req, _ := http.NewRequest(MethodGET, "/users", nil) + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + + // NotFound handler + req, _ = http.NewRequest(MethodGET, "/files", nil) + w = httptest.NewRecorder() + r.ServeHTTP(w, req) +} + func (n *node) printTree(pfx string, tail bool) { p := prefix(tail, pfx, "└── ", "├── ") fmt.Printf("%s%s has=%d, h=%v, echo=%v\n", p, n.prefix, n.has, n.handler, n.echo)