mirror of
https://github.com/labstack/echo.git
synced 2026-06-20 01:18:42 +02:00
+7
-2
@@ -26,8 +26,13 @@ func (c *Context) P(i uint8) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Param returns path parameter by name.
|
// Param returns path parameter by name.
|
||||||
func (c *Context) Param(n string) string {
|
func (c *Context) Param(name string) (value string) {
|
||||||
return c.params.Get(n)
|
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.
|
// Bind decodes the body into provided type based on Content-Type header.
|
||||||
|
|||||||
+5
-4
@@ -50,13 +50,14 @@ func TestContext(t *testing.T) {
|
|||||||
// Param //
|
// Param //
|
||||||
//***********//
|
//***********//
|
||||||
// By id
|
// By id
|
||||||
if c.P(0) != "" {
|
c.params = Params{{"id", "1"}}
|
||||||
t.Error("param id should be nil")
|
if c.P(0) != "1" {
|
||||||
|
t.Error("param id should be 1")
|
||||||
}
|
}
|
||||||
|
|
||||||
// By name
|
// By name
|
||||||
if c.Param("id") != "" {
|
if c.Param("id") != "1" {
|
||||||
t.Error("param id should be nil")
|
t.Error("param id should be 1")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store
|
// Store
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ var (
|
|||||||
ErrUnsupportedMediaType = errors.New("echo: unsupported media type")
|
ErrUnsupportedMediaType = errors.New("echo: unsupported media type")
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates a echo instance.
|
// New creates an Echo instance.
|
||||||
func New() (e *Echo) {
|
func New() (e *Echo) {
|
||||||
e = &Echo{
|
e = &Echo{
|
||||||
maxParam: 5,
|
maxParam: 5,
|
||||||
|
|||||||
+14
-14
@@ -19,7 +19,7 @@ var u1 = user{
|
|||||||
Name: "Joe",
|
Name: "Joe",
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Fix me
|
// TODO: Improve me!
|
||||||
func TestEchoMaxParam(t *testing.T) {
|
func TestEchoMaxParam(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
e.MaxParam(8)
|
e.MaxParam(8)
|
||||||
@@ -201,26 +201,26 @@ func TestEchoMethod(t *testing.T) {
|
|||||||
e.Trace("/", func(*Context) {})
|
e.Trace("/", func(*Context) {})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEchoServeHTTP(t *testing.T) {
|
func TestEchoNotFound(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
|
|
||||||
// OK
|
// Default NotFound handler
|
||||||
e.Get("/users", func(*Context) {
|
r, _ := http.NewRequest(MethodGET, "/files", nil)
|
||||||
})
|
|
||||||
w := httptest.NewRecorder()
|
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)
|
e.ServeHTTP(w, r)
|
||||||
if w.Code != http.StatusNotFound {
|
if w.Code != http.StatusNotFound {
|
||||||
t.Errorf("status code should be 404, found %d", w.Code)
|
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) {
|
func verifyUser(u2 *user, t *testing.T) {
|
||||||
|
|||||||
@@ -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) {
|
func (r *router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
h, c, _ := r.Find(req.Method, req.URL.Path)
|
h, c, _ := r.Find(req.Method, req.URL.Path)
|
||||||
defer r.echo.pool.Put(c)
|
defer r.echo.pool.Put(c)
|
||||||
|
|||||||
@@ -2,9 +2,20 @@ package echo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type route struct {
|
||||||
|
method string
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
|
var api = []route{
|
||||||
|
{"GET", "/authorizations"},
|
||||||
|
}
|
||||||
|
|
||||||
func TestRouterStatic(t *testing.T) {
|
func TestRouterStatic(t *testing.T) {
|
||||||
r := New().Router
|
r := New().Router
|
||||||
r.Add(MethodGET, "/folders/files/echo.gif", func(c *Context) {}, nil)
|
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) {
|
func (n *node) printTree(pfx string, tail bool) {
|
||||||
p := prefix(tail, pfx, "└── ", "├── ")
|
p := prefix(tail, pfx, "└── ", "├── ")
|
||||||
fmt.Printf("%s%s has=%d, h=%v, echo=%v\n", p, n.prefix, n.has, n.handler, n.echo)
|
fmt.Printf("%s%s has=%d, h=%v, echo=%v\n", p, n.prefix, n.has, n.handler, n.echo)
|
||||||
|
|||||||
Reference in New Issue
Block a user