1
0
mirror of https://github.com/labstack/echo.git synced 2025-03-19 21:17:58 +02:00

More test coverage

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-05-30 15:20:36 -07:00
parent a9e49e2430
commit b9eec15c01
8 changed files with 220 additions and 352 deletions

View File

@ -80,7 +80,7 @@ func TestContext(t *testing.T) {
tpl := &Template{
templates: template.Must(template.New("hello").Parse("Hello, {{.}}!")),
}
c.echo.renderer = tpl
c.echo.SetRenderer(tpl)
err = c.Render(http.StatusOK, "hello", "Joe")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)

View File

@ -293,9 +293,9 @@ func (e *Echo) WebSocket(path string, h HandlerFunc) {
}
func (e *Echo) add(method, path string, h Handler) {
e.router.Add(method, e.prefix+path, wrapHandler(h), e)
key := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
e.uris[key] = path
e.router.Add(method, e.prefix+path, wrapHandler(h), e)
}
// Index serves index file.
@ -335,10 +335,7 @@ func serveFile(dir, file string, c *Context) error {
return NewHTTPError(http.StatusNotFound)
}
fi, err := f.Stat()
if err != nil {
return NewHTTPError(http.StatusNotFound)
}
fi, _ := f.Stat()
if fi.IsDir() {
return NewHTTPError(http.StatusForbidden)
}
@ -538,7 +535,7 @@ func wrapHandler(h Handler) HandlerFunc {
return nil
}
default:
panic("echo unknown handler")
panic("echo => unknown handler")
}
}

View File

@ -10,6 +10,8 @@ import (
"reflect"
"strings"
"errors"
"github.com/stretchr/testify/assert"
"golang.org/x/net/websocket"
)
@ -21,56 +23,75 @@ type (
}
)
func TestEcho(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "/", nil)
rec := httptest.NewRecorder()
c := NewContext(req, NewResponse(rec), e)
// Router
assert.NotNil(t, e.Router())
// Debug
e.SetDebug(true)
assert.True(t, e.Debug())
// DefaultHTTPErrorHandler
e.DefaultHTTPErrorHandler(errors.New("error"), c)
assert.Equal(t, http.StatusInternalServerError, rec.Code)
}
// TODO: Improve me!
func TestEchoMaxParam(t *testing.T) {
e := New()
e.SetMaxParam(8)
if e.maxParam != 8 {
t.Errorf("max param should be 8, found %d", e.maxParam)
}
assert.EqualValues(t, 8, e.maxParam)
}
func TestEchoIndex(t *testing.T) {
e := New()
e.Index("examples/website/public/index.html")
w := httptest.NewRecorder()
r, _ := http.NewRequest(GET, "/", nil)
e.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("status code should be 200, found %d", w.Code)
}
c, b := request(GET, "/", e)
assert.Equal(t, http.StatusOK, c)
assert.NotEmpty(t, b)
}
func TestEchoFavicon(t *testing.T) {
e := New()
e.Favicon("examples/website/public/favicon.ico")
w := httptest.NewRecorder()
r, _ := http.NewRequest(GET, "/favicon.ico", nil)
e.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("status code should be 200, found %d", w.Code)
}
c, b := request(GET, "/favicon.ico", e)
assert.Equal(t, http.StatusOK, c)
assert.NotEmpty(t, b)
}
func TestEchoStatic(t *testing.T) {
e := New()
// OK
e.Static("/scripts", "examples/website/public/scripts")
w := httptest.NewRecorder()
r, _ := http.NewRequest(GET, "/scripts/main.js", nil)
e.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("status code should be 200, found %d", w.Code)
}
c, b := request(GET, "/scripts/main.js", e)
assert.Equal(t, http.StatusOK, c)
assert.NotEmpty(t, b)
// No file
e.Static("/scripts", "examples/website/public/scripts")
c, _ = request(GET, "/scripts/index.js", e)
assert.Equal(t, http.StatusNotFound, c)
// Directory
e.Static("/scripts", "examples/website/public/scripts")
c, _ = request(GET, "/scripts", e)
assert.Equal(t, http.StatusForbidden, c)
}
func TestEchoMiddleware(t *testing.T) {
e := New()
b := new(bytes.Buffer)
buf := new(bytes.Buffer)
// echo.MiddlewareFunc
e.Use(MiddlewareFunc(func(h HandlerFunc) HandlerFunc {
return func(c *Context) error {
b.WriteString("a")
buf.WriteString("a")
return h(c)
}
}))
@ -78,44 +99,44 @@ func TestEchoMiddleware(t *testing.T) {
// func(echo.HandlerFunc) echo.HandlerFunc
e.Use(func(h HandlerFunc) HandlerFunc {
return func(c *Context) error {
b.WriteString("b")
buf.WriteString("b")
return h(c)
}
})
// echo.HandlerFunc
e.Use(HandlerFunc(func(c *Context) error {
b.WriteString("c")
buf.WriteString("c")
return nil
}))
// func(*echo.Context) error
e.Use(func(c *Context) error {
b.WriteString("d")
buf.WriteString("d")
return nil
})
// func(http.Handler) http.Handler
e.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b.WriteString("e")
buf.WriteString("e")
h.ServeHTTP(w, r)
})
})
// http.Handler
e.Use(http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b.WriteString("f")
buf.WriteString("f")
})))
// http.HandlerFunc
e.Use(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b.WriteString("g")
buf.WriteString("g")
}))
// func(http.ResponseWriter, *http.Request)
e.Use(func(w http.ResponseWriter, r *http.Request) {
b.WriteString("h")
buf.WriteString("h")
})
// Unknown
@ -124,19 +145,21 @@ func TestEchoMiddleware(t *testing.T) {
})
// Route
e.Get("/hello", func(c *Context) error {
return c.String(http.StatusOK, "world")
e.Get("/", func(c *Context) error {
return c.String(http.StatusOK, "Hello!")
})
w := httptest.NewRecorder()
r, _ := http.NewRequest(GET, "/hello", nil)
e.ServeHTTP(w, r)
if b.String() != "abcdefgh" {
t.Errorf("buffer should be abcdefgh, found %s", b.String())
}
if w.Body.String() != "world" {
t.Error("body should be world")
}
c, b := request(GET, "/", e)
assert.Equal(t, "abcdefgh", buf.String())
assert.Equal(t, http.StatusOK, c)
assert.Equal(t, "Hello!", b)
// Error
e.Use(func(*Context) error {
return errors.New("error")
})
c, b = request(GET, "/", e)
assert.Equal(t, http.StatusInternalServerError, c)
}
func TestEchoHandler(t *testing.T) {
@ -146,44 +169,26 @@ func TestEchoHandler(t *testing.T) {
e.Get("/1", HandlerFunc(func(c *Context) error {
return c.String(http.StatusOK, "1")
}))
w := httptest.NewRecorder()
r, _ := http.NewRequest(GET, "/1", nil)
e.ServeHTTP(w, r)
if w.Body.String() != "1" {
t.Error("body should be 1")
}
// func(*echo.Context) error
e.Get("/2", func(c *Context) error {
return c.String(http.StatusOK, "2")
})
w = httptest.NewRecorder()
r, _ = http.NewRequest(GET, "/2", nil)
e.ServeHTTP(w, r)
if w.Body.String() != "2" {
t.Error("body should be 2")
}
// http.Handler/http.HandlerFunc
e.Get("/3", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("3"))
}))
w = httptest.NewRecorder()
r, _ = http.NewRequest(GET, "/3", nil)
e.ServeHTTP(w, r)
if w.Body.String() != "3" {
t.Error("body should be 3")
}
// func(http.ResponseWriter, *http.Request)
e.Get("/4", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("4"))
})
w = httptest.NewRecorder()
r, _ = http.NewRequest(GET, "/4", nil)
e.ServeHTTP(w, r)
if w.Body.String() != "4" {
t.Error("body should be 4")
for _, p := range []string{"1", "2", "3", "4"} {
c, b := request(GET, "/"+p, e)
assert.Equal(t, http.StatusOK, c)
assert.Equal(t, p, b)
}
// Unknown
@ -193,135 +198,104 @@ func TestEchoHandler(t *testing.T) {
}
func TestEchoGroup(t *testing.T) {
b := new(bytes.Buffer)
e := New()
buf := new(bytes.Buffer)
e.Use(func(*Context) error {
b.WriteString("1")
buf.WriteString("0")
return nil
})
e.Get("/users", func(*Context) error { return nil })
w := httptest.NewRecorder()
r, _ := http.NewRequest(GET, "/users", nil)
e.ServeHTTP(w, r)
if b.String() != "1" {
t.Errorf("should only execute middleware 1, executed %s", b.String())
}
h := func(*Context) error { return nil }
//--------
// Routes
//--------
e.Get("/users", h)
// Group
g1 := e.Group("/group1")
g1.Use(func(*Context) error {
b.WriteString("2")
buf.WriteString("1")
return nil
})
g1.Get("/home", func(*Context) error { return nil })
b.Reset()
w = httptest.NewRecorder()
r, _ = http.NewRequest(GET, "/group1/home", nil)
e.ServeHTTP(w, r)
if b.String() != "12" {
t.Errorf("should execute middleware 1 & 2, executed %s", b.String())
}
g1.Get("/", h)
// Group with no parent middleware
g2 := e.Group("/group2", func(*Context) error {
b.WriteString("3")
buf.WriteString("2")
return nil
})
g2.Get("/home", func(*Context) error { return nil })
b.Reset()
w = httptest.NewRecorder()
r, _ = http.NewRequest(GET, "/group2/home", nil)
e.ServeHTTP(w, r)
if b.String() != "3" {
t.Errorf("should execute middleware 3, executed %s", b.String())
}
g2.Get("/", h)
// Nested group
// Nested groups
g3 := e.Group("/group3")
g4 := g3.Group("/group4")
g4.Get("/home", func(c *Context) error {
g4.Get("/", func(c *Context) error {
return c.NoContent(http.StatusOK)
})
w = httptest.NewRecorder()
r, _ = http.NewRequest(GET, "/group3/group4/home", nil)
e.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("status code should be 200, found %d", w.Code)
}
request(GET, "/users", e)
assert.Equal(t, "0", buf.String())
buf.Reset()
request(GET, "/group1/", e)
assert.Equal(t, "01", buf.String())
buf.Reset()
request(GET, "/group2/", e)
assert.Equal(t, "2", buf.String())
buf.Reset()
c, _ := request(GET, "/group3/group4/", e)
assert.Equal(t, http.StatusOK, c)
}
func TestEchoConnect(t *testing.T) {
e := New()
testMethod(t, e, nil, CONNECT, "/")
testMethod(t, CONNECT, "/", e)
}
func TestEchoDelete(t *testing.T) {
e := New()
testMethod(t, e, nil, DELETE, "/")
testMethod(t, DELETE, "/", e)
}
func TestEchoGet(t *testing.T) {
e := New()
testMethod(t, e, nil, GET, "/")
testMethod(t, GET, "/", e)
}
func TestEchoHead(t *testing.T) {
e := New()
testMethod(t, e, nil, HEAD, "/")
testMethod(t, HEAD, "/", e)
}
func TestEchoOptions(t *testing.T) {
e := New()
testMethod(t, e, nil, OPTIONS, "/")
testMethod(t, OPTIONS, "/", e)
}
func TestEchoPatch(t *testing.T) {
e := New()
testMethod(t, e, nil, PATCH, "/")
testMethod(t, PATCH, "/", e)
}
func TestEchoPost(t *testing.T) {
e := New()
testMethod(t, e, nil, POST, "/")
testMethod(t, POST, "/", e)
}
func TestEchoPut(t *testing.T) {
e := New()
testMethod(t, e, nil, PUT, "/")
testMethod(t, PUT, "/", e)
}
func TestEchoTrace(t *testing.T) {
e := New()
testMethod(t, e, nil, TRACE, "/")
testMethod(t, TRACE, "/", e)
}
func testMethod(t *testing.T, e *Echo, g *Group, method, path string) {
m := fmt.Sprintf("%c%s", method[0], strings.ToLower(method[1:]))
p := reflect.ValueOf(path)
h := reflect.ValueOf(func(c *Context) error {
c.String(http.StatusOK, method)
return nil
})
i := interface{}(e)
if g != nil {
path = e.prefix + path
i = g
}
reflect.ValueOf(i).MethodByName(m).Call([]reflect.Value{p, h})
_, body := request(method, path, e)
if body != method {
t.Errorf("expected body `%s`, got %s.", method, body)
}
}
func request(method, path string, e *Echo) (int, string) {
r, _ := http.NewRequest(method, path, nil)
w := httptest.NewRecorder()
e.ServeHTTP(w, r)
return w.Code, w.Body.String()
}
func TestWebSocket(t *testing.T) {
func TestEchoWebSocket(t *testing.T) {
e := New()
e.WebSocket("/ws", func(c *Context) error {
c.socket.Write([]byte("test"))
@ -387,11 +361,31 @@ func TestEchoNotFound(t *testing.T) {
}
}
//func verifyUser(u2 *user, t *testing.T) {
// if u2.ID != u1.ID {
// t.Errorf("user id should be %s, found %s", u1.ID, u2.ID)
// }
// if u2.Name != u1.Name {
// t.Errorf("user name should be %s, found %s", u1.Name, u2.Name)
// }
//}
func TestEchoHTTPError(t *testing.T) {
m := http.StatusText(http.StatusBadRequest)
he := NewHTTPError(http.StatusBadRequest, m)
assert.Equal(t, http.StatusBadRequest, he.Code())
assert.Equal(t, m, he.Error())
}
func testMethod(t *testing.T, method, path string, e *Echo) {
m := fmt.Sprintf("%c%s", method[0], strings.ToLower(method[1:]))
p := reflect.ValueOf(path)
h := reflect.ValueOf(func(c *Context) error {
c.String(http.StatusOK, method)
return nil
})
i := interface{}(e)
reflect.ValueOf(i).MethodByName(m).Call([]reflect.Value{p, h})
_, body := request(method, path, e)
if body != method {
t.Errorf("expected body `%s`, got %s.", method, body)
}
}
func request(method, path string, e *Echo) (int, string) {
r, _ := http.NewRequest(method, path, nil)
w := httptest.NewRecorder()
e.ServeHTTP(w, r)
return w.Code, w.Body.String()
}

View File

@ -2,47 +2,20 @@ package echo
import "testing"
func TestGroupConnect(t *testing.T) {
func TestGroup(t *testing.T) {
g := New().Group("/group")
testMethod(t, &g.echo, g, CONNECT, "/")
}
func TestGroupDelete(t *testing.T) {
g := New().Group("/group")
testMethod(t, &g.echo, g, DELETE, "/")
}
func TestGroupGet(t *testing.T) {
g := New().Group("/group")
testMethod(t, &g.echo, g, GET, "/")
}
func TestGroupHead(t *testing.T) {
g := New().Group("/group")
testMethod(t, &g.echo, g, HEAD, "/")
}
func TestGroupOptions(t *testing.T) {
g := New().Group("/group")
testMethod(t, &g.echo, g, OPTIONS, "/")
}
func TestGroupPatch(t *testing.T) {
g := New().Group("/group")
testMethod(t, &g.echo, g, PATCH, "/")
}
func TestGroupPost(t *testing.T) {
g := New().Group("/group")
testMethod(t, &g.echo, g, POST, "/")
}
func TestGroupPut(t *testing.T) {
g := New().Group("/group")
testMethod(t, &g.echo, g, PUT, "/")
}
func TestGroupTrace(t *testing.T) {
g := New().Group("/group")
testMethod(t, &g.echo, g, TRACE, "/")
h := func(*Context) error { return nil }
g.Connect("/", h)
g.Delete("/", h)
g.Get("/", h)
g.Head("/", h)
g.Options("/", h)
g.Patch("/", h)
g.Post("/", h)
g.Put("/", h)
g.Trace("/", h)
g.WebSocket("/ws", h)
g.Static("/scripts", "scripts")
g.ServeDir("/scripts", "scripts")
g.ServeFile("/scripts/main.js", "scripts/main.js")
}

View File

@ -6,8 +6,8 @@ import (
"testing"
"github.com/labstack/echo"
"net/http/httptest"
"github.com/stretchr/testify/assert"
"net/http/httptest"
)
func TestBasicAuth(t *testing.T) {

View File

@ -1,11 +1,11 @@
package middleware
import (
"errors"
"github.com/labstack/echo"
"net/http"
"net/http/httptest"
"testing"
"errors"
)
func TestLogger(t *testing.T) {

View File

@ -1,10 +1,10 @@
package echo
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestResponse(t *testing.T) {
@ -44,7 +44,7 @@ func TestResponse(t *testing.T) {
r.Flush()
// Size
assert.Len(t, s, int(r.Size()))
assert.EqualValues(t, len(s), r.Size())
// Hijack
assert.Panics(t, func() {

View File

@ -1,11 +1,12 @@
package echo
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
type route struct {
@ -281,20 +282,15 @@ var (
func TestRouterStatic(t *testing.T) {
r := New().router
b := new(bytes.Buffer)
path := "/folders/a/files/echo.gif"
r.Add(GET, path, func(*Context) error {
b.WriteString(path)
r.Add(GET, path, func(c *Context) error {
c.Set("path", path)
return nil
}, nil)
h, _ := r.Find(GET, path, context)
if h == nil {
t.Error("handler not found")
} else {
h(nil)
if b.String() != path {
t.Errorf("buffer should %s", path)
}
if assert.NotNil(t, h) {
h(context)
assert.Equal(t, path, context.Get("path"))
}
}
@ -304,12 +300,8 @@ func TestRouterParam(t *testing.T) {
return nil
}, nil)
h, _ := r.Find(GET, "/users/1", context)
if h == nil {
t.Error("handler not found")
} else {
if context.P(0) != "1" {
t.Error("param id should be 1")
}
if assert.NotNil(t, h) {
assert.Equal(t, "1", context.P(0))
}
}
@ -320,21 +312,13 @@ func TestRouterTwoParam(t *testing.T) {
}, nil)
h, _ := r.Find(GET, "/users/1/files/1", context)
if h == nil {
t.Error("handler not found")
} else {
if context.P(0) != "1" {
t.Error("param uid should be 1")
}
if context.P(1) != "1" {
t.Error("param fid should be 1")
}
if assert.NotNil(t, h) {
assert.Equal(t, "1", context.P(0))
assert.Equal(t, "1", context.P(1))
}
h, _ = r.Find(GET, "/users/1", context)
if h != nil {
t.Error("should not found handler")
}
assert.Nil(t, h)
}
func TestRouterMatchAny(t *testing.T) {
@ -344,21 +328,13 @@ func TestRouterMatchAny(t *testing.T) {
}, nil)
h, _ := r.Find(GET, "/users/", context)
if h == nil {
t.Error("should match empty value")
} else {
if context.P(0) != "" {
t.Error("value should be empty")
}
if assert.NotNil(t, h) {
assert.Equal(t, "", context.P(0))
}
h, _ = r.Find(GET, "/users/joe", context)
if h == nil {
t.Error("should match non-empty value")
} else {
if context.P(0) != "joe" {
t.Error("value should be joe")
}
h, _ = r.Find(GET, "/users/1", context)
if assert.NotNil(t, h) {
assert.Equal(t, "1", context.P(0))
}
}
@ -368,28 +344,19 @@ func TestRouterMicroParam(t *testing.T) {
return nil
}, nil)
h, _ := r.Find(GET, "/1/2/3", context)
if h == nil {
t.Error("handler not found")
} else {
if context.P(0) != "1" {
t.Error("param a should be 1")
}
if context.P(1) != "2" {
t.Error("param b should be 2")
}
if context.P(2) != "3" {
t.Error("param c should be 3")
}
if assert.NotNil(t, h) {
assert.Equal(t, "1", context.P(0))
assert.Equal(t, "2", context.P(1))
assert.Equal(t, "3", context.P(2))
}
}
func TestRouterMultiRoute(t *testing.T) {
r := New().router
b := new(bytes.Buffer)
// Routes
r.Add(GET, "/users", func(*Context) error {
b.WriteString("/users")
r.Add(GET, "/users", func(c *Context) error {
c.Set("path", "/users")
return nil
}, nil)
r.Add(GET, "/users/:id", func(c *Context) error {
@ -398,30 +365,20 @@ func TestRouterMultiRoute(t *testing.T) {
// Route > /users
h, _ := r.Find(GET, "/users", context)
if h == nil {
t.Error("handler not found")
} else {
h(nil)
if b.String() != "/users" {
t.Errorf("buffer should be /users")
}
if assert.NotNil(t, h) {
h(context)
assert.Equal(t, "/users", context.Get("path"))
}
// Route > /users/:id
h, _ = r.Find(GET, "/users/1", context)
if h == nil {
t.Error("handler not found")
} else {
if context.P(0) != "1" {
t.Error("param id should be 1")
}
if assert.NotNil(t, h) {
assert.Equal(t, "1", context.P(0))
}
// Route > /user
h, _ = r.Find(GET, "/user", context)
if h != nil {
t.Error("handler should be nil")
}
assert.Nil(t, h)
}
func TestRouterPriority(t *testing.T) {
@ -459,89 +416,60 @@ func TestRouterPriority(t *testing.T) {
// Route > /users
h, _ := r.Find(GET, "/users", context)
if h == nil {
t.Error("handler not found")
} else {
if assert.NotNil(t, h) {
h(context)
if context.Get("a") != 1 {
t.Error("a should map to 1")
}
assert.Equal(t, 1, context.Get("a"))
}
// Route > /users/new
h, _ = r.Find(GET, "/users/new", context)
if h == nil {
t.Error("handler not found")
} else {
if assert.NotNil(t, h) {
h(context)
if context.Get("b") != 2 {
t.Error("b should map to 2")
}
assert.Equal(t, 2, context.Get("b"))
}
// Route > /users/:id
h, _ = r.Find(GET, "/users/1", context)
if h == nil {
t.Error("handler not found")
} else {
if assert.NotNil(t, h) {
h(context)
if context.Get("c") != 3 {
t.Error("c should map to 3")
}
assert.Equal(t, 3, context.Get("c"))
}
// Route > /users/dew
h, _ = r.Find(GET, "/users/dew", context)
if h == nil {
t.Error("handler not found")
} else {
if assert.NotNil(t, h) {
h(context)
if context.Get("d") != 4 {
t.Error("d should map to 4")
}
assert.Equal(t, 4, context.Get("d"))
}
// Route > /users/:id/files
h, _ = r.Find(GET, "/users/1/files", context)
if h == nil {
t.Error("handler not found")
} else {
if assert.NotNil(t, h) {
h(context)
if context.Get("e") != 5 {
t.Error("e should map to 5")
}
assert.Equal(t, 5, context.Get("e"))
}
// Route > /users/:id
h, _ = r.Find(GET, "/users/news", context)
if h == nil {
t.Error("handler not found")
} else {
if assert.NotNil(t, h) {
h(context)
if context.Get("c") != 3 {
t.Error("c should map to 3")
}
assert.Equal(t, 3, context.Get("c"))
}
// Route > /users/*
h, _ = r.Find(GET, "/users/joe/books", context)
if h == nil {
t.Error("handler not found")
} else {
if assert.NotNil(t, h) {
h(context)
if context.Get("g") != 7 {
t.Error("g should map to 7")
}
assert.Equal(t, 7, context.Get("g"))
}
}
func TestRouterParamNames(t *testing.T) {
r := New().router
b := new(bytes.Buffer)
// Routes
r.Add(GET, "/users", func(*Context) error {
b.WriteString("/users")
r.Add(GET, "/users", func(c *Context) error {
c.Set("path", "/users")
return nil
}, nil)
r.Add(GET, "/users/:id", func(c *Context) error {
@ -553,45 +481,25 @@ func TestRouterParamNames(t *testing.T) {
// Route > /users
h, _ := r.Find(GET, "/users", context)
if h == nil {
t.Error("handler not found")
} else {
if assert.NotNil(t, h) {
h(context)
if b.String() != "/users" {
t.Errorf("buffer should be /users")
}
assert.Equal(t, "/users", context.Get("path"))
}
// Route > /users/:id
h, _ = r.Find(GET, "/users/1", context)
if h == nil {
t.Error("handler not found")
} else {
if context.pnames[0] != "id" {
t.Error("param name should be id")
}
if context.P(0) != "1" {
t.Error("param id should be 1")
}
if assert.NotNil(t, h) {
assert.Equal(t, "id", context.pnames[0])
assert.Equal(t, "1", context.P(0))
}
// Route > /users/:uid/files/:fid
h, _ = r.Find(GET, "/users/1/files/1", context)
if h == nil {
t.Error("handler not found")
} else {
if context.pnames[0] != "uid" {
t.Error("param name should be id")
}
if context.P(0) != "1" {
t.Error("param id should be 1")
}
if context.pnames[1] != "fid" {
t.Error("param name should be id")
}
if context.P(1) != "1" {
t.Error("param id should be 1")
}
if assert.NotNil(t, h) {
assert.Equal(t, "uid", context.pnames[0])
assert.Equal(t, "1", context.P(0))
assert.Equal(t, "fid", context.pnames[1])
assert.Equal(t, "1", context.P(1))
}
}
@ -600,18 +508,14 @@ func TestRouterAPI(t *testing.T) {
for _, route := range api {
r.Add(route.method, route.path, func(c *Context) error {
for i, n := range c.pnames {
if n != "" {
if ":"+n != c.P(uint8(i)) {
t.Errorf("param not found, method=%s, path=%s", route.method, route.path)
}
if assert.NotEmpty(t, n) {
assert.Equal(t, ":"+n, c.P(uint8(i)))
}
}
return nil
}, nil)
h, _ := r.Find(route.method, route.path, context)
if h == nil {
t.Fatalf("handler not found, method=%s, path=%s", route.method, route.path)
} else {
if assert.NotNil(t, h) {
h(context)
}
}
@ -628,7 +532,7 @@ func TestRouterServeHTTP(t *testing.T) {
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
// NotFound handler
// Not found
req, _ = http.NewRequest(GET, "/files", nil)
w = httptest.NewRecorder()
r.ServeHTTP(w, req)