1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-28 08:38:39 +02:00

More coverage and cleanup

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-04-05 20:08:52 -07:00
parent c410efd290
commit 47fb44deb5
6 changed files with 57 additions and 31 deletions

View File

@ -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.

View File

@ -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

View File

@ -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,

View File

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

View File

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

View File

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