mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
Improved test cases
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
e08b3f78e8
commit
8b800e12a2
@ -47,14 +47,14 @@ func (c *Context) Bind(i interface{}) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// String writes status and string to the response.
|
||||
// String sends a text/plain response with status code.
|
||||
func (c *Context) String(n int, s string) {
|
||||
c.Response.Header().Set(HeaderContentType, MIMEText+"; charset=utf-8")
|
||||
c.Response.WriteHeader(n)
|
||||
c.Response.Write([]byte(s))
|
||||
}
|
||||
|
||||
// JSON writes status and JSON to the response.
|
||||
// JSON sends an application/json response with status code.
|
||||
func (c *Context) JSON(n int, i interface{}) {
|
||||
enc := json.NewEncoder(c.Response)
|
||||
c.Response.Header().Set(HeaderContentType, MIMEJSON+"; charset=utf-8")
|
||||
@ -77,7 +77,7 @@ func (c *Context) Set(key string, val interface{}) {
|
||||
c.store[key] = val
|
||||
}
|
||||
|
||||
// Redirect redirects the request using http.Redirect.
|
||||
// Redirect redirects the request using http.Redirect with status code.
|
||||
func (c *Context) Redirect(n int, url string) {
|
||||
http.Redirect(c.Response, c.Request, url, n)
|
||||
}
|
||||
|
52
echo_test.go
52
echo_test.go
@ -1,6 +1,7 @@
|
||||
package echo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"io"
|
||||
@ -54,70 +55,57 @@ func TestEchoStatic(t *testing.T) {
|
||||
|
||||
func TestEchoMiddleware(t *testing.T) {
|
||||
e := New()
|
||||
b := new(bytes.Buffer)
|
||||
|
||||
// func(*echo.Context)
|
||||
e.Use(func(c *Context) {
|
||||
c.Request.Header.Set("e", "5")
|
||||
b.WriteString("a")
|
||||
})
|
||||
|
||||
// func(echo.HandlerFunc) echo.HandlerFunc
|
||||
e.Use(func(h HandlerFunc) HandlerFunc {
|
||||
return HandlerFunc(func(c *Context) {
|
||||
c.Request.Header.Set("a", "1")
|
||||
b.WriteString("b")
|
||||
h(c)
|
||||
})
|
||||
})
|
||||
|
||||
// http.HandlerFunc
|
||||
e.Use(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Header.Set("b", "2")
|
||||
b.WriteString("c")
|
||||
}))
|
||||
|
||||
// http.Handler
|
||||
e.Use(http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Header.Set("c", "3")
|
||||
b.WriteString("d")
|
||||
})))
|
||||
|
||||
// func(http.Handler) http.Handler
|
||||
e.Use(func(http.Handler) http.Handler {
|
||||
e.Use(func(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Header.Set("d", "4")
|
||||
b.WriteString("e")
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
})
|
||||
|
||||
// func(http.ResponseWriter, *http.Request)
|
||||
e.Use(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Header.Set("f", "6")
|
||||
b.WriteString("f")
|
||||
})
|
||||
|
||||
// Route
|
||||
e.Get("/hello", func(c *Context) {
|
||||
if c.Request.Header.Get("a") != "1" {
|
||||
t.Error("header a should be 1")
|
||||
}
|
||||
if c.Request.Header.Get("b") != "2" {
|
||||
t.Error("header b should be 2")
|
||||
}
|
||||
if c.Request.Header.Get("c") != "3" {
|
||||
t.Error("header c should be 3")
|
||||
}
|
||||
if c.Request.Header.Get("d") != "4" {
|
||||
t.Error("header d should be 4")
|
||||
}
|
||||
if c.Request.Header.Get("e") != "5" {
|
||||
t.Error("header e should be 5")
|
||||
}
|
||||
if c.Request.Header.Get("f") != "6" {
|
||||
t.Error("header f should be 6")
|
||||
}
|
||||
c.String(200, "world")
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r, _ := http.NewRequest("GET", "/hello", nil)
|
||||
e.ServeHTTP(w, r)
|
||||
if b.String() != "abcdef" {
|
||||
t.Errorf("buffer should be abcdef, found %s", b.String())
|
||||
}
|
||||
if w.Body.String() != "world" {
|
||||
t.Errorf("body should be world")
|
||||
t.Error("body should be world")
|
||||
}
|
||||
}
|
||||
|
||||
@ -132,10 +120,10 @@ func TestEchoHandler(t *testing.T) {
|
||||
r, _ := http.NewRequest("GET", "/1", nil)
|
||||
e.ServeHTTP(w, r)
|
||||
if w.Body.String() != "1" {
|
||||
t.Errorf("body should be 1")
|
||||
t.Error("body should be 1")
|
||||
}
|
||||
|
||||
// http.Handler / http.HandlerFunc
|
||||
// http.Handler/http.HandlerFunc
|
||||
e.Get("/2", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("2"))
|
||||
}))
|
||||
@ -143,7 +131,7 @@ func TestEchoHandler(t *testing.T) {
|
||||
r, _ = http.NewRequest("GET", "/2", nil)
|
||||
e.ServeHTTP(w, r)
|
||||
if w.Body.String() != "2" {
|
||||
t.Errorf("body should be 2")
|
||||
t.Error("body should be 2")
|
||||
}
|
||||
|
||||
// func(http.ResponseWriter, *http.Request)
|
||||
@ -154,7 +142,7 @@ func TestEchoHandler(t *testing.T) {
|
||||
r, _ = http.NewRequest("GET", "/3", nil)
|
||||
e.ServeHTTP(w, r)
|
||||
if w.Body.String() != "3" {
|
||||
t.Errorf("body should be 3")
|
||||
t.Error("body should be 3")
|
||||
}
|
||||
}
|
||||
|
||||
@ -162,14 +150,14 @@ func verifyUser(rd io.Reader, t *testing.T) {
|
||||
var l int64
|
||||
err := binary.Read(rd, binary.BigEndian, &l) // Body length
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Error(err)
|
||||
}
|
||||
bd := io.LimitReader(rd, l) // Body
|
||||
u2 := new(user)
|
||||
dec := json.NewDecoder(bd)
|
||||
err = dec.Decode(u2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Error(err)
|
||||
}
|
||||
if u2.ID != u.ID {
|
||||
t.Errorf("user id should be %s, found %s", u.ID, u2.ID)
|
||||
|
Loading…
Reference in New Issue
Block a user