2015-03-27 23:21:30 +02:00
|
|
|
package echo
|
2015-03-21 23:59:09 +02:00
|
|
|
|
2015-03-31 05:54:38 +02:00
|
|
|
import (
|
2016-04-12 01:49:20 +02:00
|
|
|
"bytes"
|
2015-05-30 02:20:13 +02:00
|
|
|
"errors"
|
2015-04-11 06:48:26 +02:00
|
|
|
"io"
|
2016-06-06 07:25:34 +02:00
|
|
|
"mime/multipart"
|
2015-03-31 05:54:38 +02:00
|
|
|
"net/http"
|
2016-03-12 21:49:45 +02:00
|
|
|
"os"
|
2015-03-31 05:54:38 +02:00
|
|
|
"testing"
|
2015-04-11 06:48:26 +02:00
|
|
|
"text/template"
|
2016-05-03 01:19:35 +02:00
|
|
|
"time"
|
2015-05-30 02:20:13 +02:00
|
|
|
|
2016-06-06 07:25:34 +02:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
"strings"
|
|
|
|
|
2015-07-05 20:08:17 +02:00
|
|
|
"net/url"
|
2015-07-24 21:28:35 +02:00
|
|
|
|
2015-11-08 01:39:09 +02:00
|
|
|
"encoding/xml"
|
2015-11-13 06:23:14 +02:00
|
|
|
|
2016-01-09 19:44:18 +02:00
|
|
|
"github.com/labstack/echo/test"
|
2015-07-24 21:28:35 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-03-31 05:54:38 +02:00
|
|
|
)
|
2015-03-21 23:59:09 +02:00
|
|
|
|
2015-04-11 06:48:26 +02:00
|
|
|
type (
|
|
|
|
Template struct {
|
|
|
|
templates *template.Template
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-03-06 06:03:11 +02:00
|
|
|
func (t *Template) Render(w io.Writer, name string, data interface{}, c Context) error {
|
2015-05-20 23:38:51 +02:00
|
|
|
return t.templates.ExecuteTemplate(w, name, data)
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
|
|
|
|
2015-03-31 05:54:38 +02:00
|
|
|
func TestContext(t *testing.T) {
|
2015-12-01 21:22:45 +02:00
|
|
|
e := New()
|
2016-04-24 19:21:23 +02:00
|
|
|
req := test.NewRequest(POST, "/", strings.NewReader(userJSON))
|
|
|
|
rec := test.NewResponseRecorder()
|
2016-06-05 22:47:59 +02:00
|
|
|
c := e.NewContext(req, rec).(*echoContext)
|
2015-04-05 23:21:03 +02:00
|
|
|
|
2016-06-06 07:25:34 +02:00
|
|
|
// Echo
|
|
|
|
assert.Equal(t, e, c.Echo())
|
|
|
|
|
2015-05-30 02:20:13 +02:00
|
|
|
// Request
|
2016-06-06 07:25:34 +02:00
|
|
|
assert.Equal(t, req, c.Request())
|
2015-05-30 02:20:13 +02:00
|
|
|
|
|
|
|
// Response
|
2016-06-06 07:25:34 +02:00
|
|
|
assert.Equal(t, rec, c.Response())
|
2015-04-05 23:21:03 +02:00
|
|
|
|
2016-06-06 07:25:34 +02:00
|
|
|
// Logger
|
|
|
|
assert.Equal(t, e.logger, c.Logger())
|
2015-05-30 19:54:55 +02:00
|
|
|
|
|
|
|
//--------
|
2015-04-11 06:48:26 +02:00
|
|
|
// Render
|
2015-05-30 19:54:55 +02:00
|
|
|
//--------
|
|
|
|
|
2015-04-11 06:48:26 +02:00
|
|
|
tpl := &Template{
|
2015-05-30 19:54:55 +02:00
|
|
|
templates: template.Must(template.New("hello").Parse("Hello, {{.}}!")),
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
2016-04-17 00:53:27 +02:00
|
|
|
c.echo.SetRenderer(tpl)
|
2016-05-01 21:38:51 +02:00
|
|
|
err := c.Render(http.StatusOK, "hello", "Jon Snow")
|
2015-05-30 19:54:55 +02:00
|
|
|
if assert.NoError(t, err) {
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
2016-05-01 21:38:51 +02:00
|
|
|
assert.Equal(t, "Hello, Jon Snow!", rec.Body.String())
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
2015-05-30 19:54:55 +02:00
|
|
|
|
2016-04-17 00:53:27 +02:00
|
|
|
c.echo.renderer = nil
|
2016-05-01 21:38:51 +02:00
|
|
|
err = c.Render(http.StatusOK, "hello", "Jon Snow")
|
2015-05-30 19:54:55 +02:00
|
|
|
assert.Error(t, err)
|
2015-04-11 06:48:26 +02:00
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// JSON
|
2016-04-24 19:21:23 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
2016-06-05 22:47:59 +02:00
|
|
|
c = e.NewContext(req, rec).(*echoContext)
|
2016-05-01 21:38:51 +02:00
|
|
|
err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
|
2015-05-30 19:54:55 +02:00
|
|
|
if assert.NoError(t, err) {
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
|
|
|
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, userJSON, rec.Body.String())
|
2015-07-24 21:28:35 +02:00
|
|
|
}
|
|
|
|
|
2015-11-08 01:59:08 +02:00
|
|
|
// JSON (error)
|
2016-04-24 19:21:23 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
2016-06-05 22:47:59 +02:00
|
|
|
c = e.NewContext(req, rec).(*echoContext)
|
2016-02-11 02:51:43 +02:00
|
|
|
err = c.JSON(http.StatusOK, make(chan bool))
|
2015-11-08 01:59:08 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
2015-07-24 21:28:35 +02:00
|
|
|
// JSONP
|
2016-04-24 19:21:23 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
2016-06-05 22:47:59 +02:00
|
|
|
c = e.NewContext(req, rec).(*echoContext)
|
2015-07-24 21:28:35 +02:00
|
|
|
callback := "callback"
|
2016-05-01 21:38:51 +02:00
|
|
|
err = c.JSONP(http.StatusOK, callback, user{1, "Jon Snow"})
|
2015-07-24 21:28:35 +02:00
|
|
|
if assert.NoError(t, err) {
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
|
|
|
assert.Equal(t, MIMEApplicationJavaScriptCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, callback+"("+userJSON+");", rec.Body.String())
|
2015-07-11 21:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// XML
|
2016-04-24 19:21:23 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
2016-06-05 22:47:59 +02:00
|
|
|
c = e.NewContext(req, rec).(*echoContext)
|
2016-05-01 21:38:51 +02:00
|
|
|
err = c.XML(http.StatusOK, user{1, "Jon Snow"})
|
2015-07-11 21:20:59 +02:00
|
|
|
if assert.NoError(t, err) {
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
|
|
|
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, xml.Header+userXML, rec.Body.String())
|
2015-11-08 01:39:09 +02:00
|
|
|
}
|
|
|
|
|
2015-11-08 01:59:08 +02:00
|
|
|
// XML (error)
|
2016-04-24 19:21:23 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
2016-06-05 22:47:59 +02:00
|
|
|
c = e.NewContext(req, rec).(*echoContext)
|
2016-02-11 02:51:43 +02:00
|
|
|
err = c.XML(http.StatusOK, make(chan bool))
|
2015-11-08 01:59:08 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// String
|
2016-04-24 19:21:23 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
2016-06-05 22:47:59 +02:00
|
|
|
c = e.NewContext(req, rec).(*echoContext)
|
2015-05-30 19:54:55 +02:00
|
|
|
err = c.String(http.StatusOK, "Hello, World!")
|
|
|
|
if assert.NoError(t, err) {
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
|
|
|
assert.Equal(t, MIMETextPlainCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, "Hello, World!", rec.Body.String())
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 22:02:23 +02:00
|
|
|
// HTML
|
2016-04-24 19:21:23 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
2016-06-05 22:47:59 +02:00
|
|
|
c = e.NewContext(req, rec).(*echoContext)
|
2015-05-30 19:54:55 +02:00
|
|
|
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
|
|
|
|
if assert.NoError(t, err) {
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
|
|
|
assert.Equal(t, MIMETextHTMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
// Attachment
|
2016-04-24 19:21:23 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
2016-06-05 22:47:59 +02:00
|
|
|
c = e.NewContext(req, rec).(*echoContext)
|
2016-03-12 21:49:45 +02:00
|
|
|
file, err := os.Open("_fixture/images/walle.png")
|
2015-08-01 04:25:03 +02:00
|
|
|
if assert.NoError(t, err) {
|
2016-03-12 21:49:45 +02:00
|
|
|
err = c.Attachment(file, "walle.png")
|
|
|
|
if assert.NoError(t, err) {
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
|
|
|
assert.Equal(t, "attachment; filename=walle.png", rec.Header().Get(HeaderContentDisposition))
|
|
|
|
assert.Equal(t, 219885, rec.Body.Len())
|
2016-03-12 21:49:45 +02:00
|
|
|
}
|
2015-08-25 07:01:25 +02:00
|
|
|
}
|
|
|
|
|
2015-05-30 02:20:13 +02:00
|
|
|
// NoContent
|
2016-04-24 19:21:23 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
2016-06-05 22:47:59 +02:00
|
|
|
c = e.NewContext(req, rec).(*echoContext)
|
2015-05-30 02:20:13 +02:00
|
|
|
c.NoContent(http.StatusOK)
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
2015-05-30 02:20:13 +02:00
|
|
|
|
|
|
|
// Error
|
2016-04-24 19:21:23 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
2016-06-05 22:47:59 +02:00
|
|
|
c = e.NewContext(req, rec).(*echoContext)
|
2015-05-30 02:20:13 +02:00
|
|
|
c.Error(errors.New("error"))
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, rec.Status())
|
2015-05-30 02:20:13 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Reset
|
2016-04-24 19:21:23 +02:00
|
|
|
c.Reset(req, test.NewResponseRecorder())
|
2015-05-30 19:54:55 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:19:35 +02:00
|
|
|
func TestContextCookie(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
req := test.NewRequest(GET, "/", nil)
|
|
|
|
theme := "theme=light"
|
|
|
|
user := "user=Jon Snow"
|
|
|
|
req.Header().Add(HeaderCookie, theme)
|
|
|
|
req.Header().Add(HeaderCookie, user)
|
|
|
|
rec := test.NewResponseRecorder()
|
2016-06-05 22:47:59 +02:00
|
|
|
c := e.NewContext(req, rec).(*echoContext)
|
2016-05-03 01:19:35 +02:00
|
|
|
|
|
|
|
// Read single
|
2016-05-03 07:41:07 +02:00
|
|
|
cookie, err := c.Cookie("theme")
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "theme", cookie.Name())
|
|
|
|
assert.Equal(t, "light", cookie.Value())
|
|
|
|
}
|
2016-05-03 01:19:35 +02:00
|
|
|
|
|
|
|
// Read multiple
|
|
|
|
for _, cookie := range c.Cookies() {
|
|
|
|
switch cookie.Name() {
|
|
|
|
case "theme":
|
|
|
|
assert.Equal(t, "light", cookie.Value())
|
|
|
|
case "user":
|
|
|
|
assert.Equal(t, "Jon Snow", cookie.Value())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write
|
2016-06-06 07:25:34 +02:00
|
|
|
cookie = &test.Cookie{Cookie: &http.Cookie{
|
2016-05-03 01:19:35 +02:00
|
|
|
Name: "SSID",
|
|
|
|
Value: "Ap4PGTEq",
|
|
|
|
Domain: "labstack.com",
|
|
|
|
Path: "/",
|
|
|
|
Expires: time.Now(),
|
|
|
|
Secure: true,
|
|
|
|
HttpOnly: true,
|
|
|
|
}}
|
|
|
|
c.SetCookie(cookie)
|
|
|
|
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "SSID")
|
|
|
|
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "Ap4PGTEq")
|
|
|
|
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "labstack.com")
|
|
|
|
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "Secure")
|
|
|
|
assert.Contains(t, rec.Header().Get(HeaderSetCookie), "HttpOnly")
|
|
|
|
}
|
|
|
|
|
2015-11-13 06:23:14 +02:00
|
|
|
func TestContextPath(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
r := e.Router()
|
|
|
|
|
|
|
|
r.Add(GET, "/users/:id", nil, e)
|
2016-04-17 00:53:27 +02:00
|
|
|
c := e.NewContext(nil, nil)
|
2015-11-13 06:23:14 +02:00
|
|
|
r.Find(GET, "/users/1", c)
|
2016-03-12 21:49:45 +02:00
|
|
|
assert.Equal(t, "/users/:id", c.Path())
|
2015-11-13 06:23:14 +02:00
|
|
|
|
|
|
|
r.Add(GET, "/users/:uid/files/:fid", nil, e)
|
2016-04-17 00:53:27 +02:00
|
|
|
c = e.NewContext(nil, nil)
|
2015-11-13 06:23:14 +02:00
|
|
|
r.Find(GET, "/users/1/files/1", c)
|
2016-03-12 21:49:45 +02:00
|
|
|
assert.Equal(t, "/users/:uid/files/:fid", c.Path())
|
2015-11-13 06:23:14 +02:00
|
|
|
}
|
|
|
|
|
2016-06-06 07:25:34 +02:00
|
|
|
func TestContextPathParam(t *testing.T) {
|
2016-04-17 00:53:27 +02:00
|
|
|
e := New()
|
2016-06-06 07:25:34 +02:00
|
|
|
req := test.NewRequest(GET, "/", nil)
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, nil)
|
2016-06-06 07:25:34 +02:00
|
|
|
|
|
|
|
// ParamNames
|
|
|
|
c.SetParamNames("uid", "fid")
|
|
|
|
assert.EqualValues(t, []string{"uid", "fid"}, c.ParamNames())
|
|
|
|
|
|
|
|
// ParamValues
|
|
|
|
c.SetParamValues("101", "501")
|
|
|
|
assert.EqualValues(t, []string{"101", "501"}, c.ParamValues())
|
|
|
|
|
|
|
|
// P
|
|
|
|
assert.Equal(t, "101", c.P(0))
|
|
|
|
|
|
|
|
// Param
|
|
|
|
assert.Equal(t, "501", c.Param("fid"))
|
2015-07-05 20:08:17 +02:00
|
|
|
}
|
|
|
|
|
2016-03-21 22:10:20 +02:00
|
|
|
func TestContextFormValue(t *testing.T) {
|
2015-07-05 20:08:17 +02:00
|
|
|
f := make(url.Values)
|
2016-06-06 07:25:34 +02:00
|
|
|
f.Set("name", "Jon Snow")
|
|
|
|
f.Set("email", "jon@labstack.com")
|
2015-07-05 20:08:17 +02:00
|
|
|
|
2016-04-17 00:53:27 +02:00
|
|
|
e := New()
|
2016-04-24 19:21:23 +02:00
|
|
|
req := test.NewRequest(POST, "/", strings.NewReader(f.Encode()))
|
|
|
|
req.Header().Add(HeaderContentType, MIMEApplicationForm)
|
2016-06-06 07:25:34 +02:00
|
|
|
c := e.NewContext(req, nil)
|
2015-07-05 20:08:17 +02:00
|
|
|
|
2016-06-06 07:25:34 +02:00
|
|
|
// FormValue
|
|
|
|
assert.Equal(t, "Jon Snow", c.FormValue("name"))
|
|
|
|
assert.Equal(t, "jon@labstack.com", c.FormValue("email"))
|
|
|
|
|
|
|
|
// FormParams
|
|
|
|
assert.Equal(t, map[string][]string{
|
|
|
|
"name": []string{"Jon Snow"},
|
|
|
|
"email": []string{"jon@labstack.com"},
|
|
|
|
}, c.FormParams())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextQueryParam(t *testing.T) {
|
|
|
|
q := make(url.Values)
|
|
|
|
q.Set("name", "Jon Snow")
|
|
|
|
q.Set("email", "jon@labstack.com")
|
|
|
|
req := test.NewRequest(GET, "/?"+q.Encode(), nil)
|
|
|
|
e := New()
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, nil)
|
2016-06-06 07:25:34 +02:00
|
|
|
|
|
|
|
// QueryParam
|
|
|
|
assert.Equal(t, "Jon Snow", c.QueryParam("name"))
|
|
|
|
assert.Equal(t, "jon@labstack.com", c.QueryParam("email"))
|
|
|
|
|
|
|
|
// QueryParams
|
|
|
|
assert.Equal(t, map[string][]string{
|
|
|
|
"name": []string{"Jon Snow"},
|
|
|
|
"email": []string{"jon@labstack.com"},
|
|
|
|
}, c.QueryParams())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextFormFile(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
mr := multipart.NewWriter(buf)
|
|
|
|
w, err := mr.CreateFormFile("file", "test")
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
w.Write([]byte("test"))
|
|
|
|
}
|
|
|
|
mr.Close()
|
|
|
|
req := test.NewRequest(POST, "/", buf)
|
|
|
|
req.Header().Set(HeaderContentType, mr.FormDataContentType())
|
|
|
|
rec := test.NewResponseRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
f, err := c.FormFile("file")
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "test", f.Filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextMultipartForm(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
mw := multipart.NewWriter(buf)
|
|
|
|
mw.WriteField("name", "Jon Snow")
|
|
|
|
mw.Close()
|
|
|
|
req := test.NewRequest(POST, "/", buf)
|
|
|
|
req.Header().Set(HeaderContentType, mw.FormDataContentType())
|
|
|
|
rec := test.NewResponseRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
f, err := c.MultipartForm()
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.NotNil(t, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextRedirect(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
req := test.NewRequest(GET, "/", nil)
|
|
|
|
rec := test.NewResponseRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
|
|
|
|
assert.Equal(t, http.StatusMovedPermanently, rec.Status())
|
|
|
|
assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(HeaderLocation))
|
|
|
|
assert.Error(t, c.Redirect(310, "http://labstack.github.io/echo"))
|
|
|
|
}
|
|
|
|
|
2016-06-06 18:47:22 +02:00
|
|
|
func TestContextEmbedded(t *testing.T) {
|
2016-08-22 13:41:07 +02:00
|
|
|
var c Context
|
|
|
|
c = new(echoContext)
|
2016-06-06 18:47:22 +02:00
|
|
|
c.SetContext(context.WithValue(c, "key", "val"))
|
2016-06-06 07:25:34 +02:00
|
|
|
assert.Equal(t, "val", c.Value("key"))
|
2016-06-06 18:47:22 +02:00
|
|
|
now := time.Now()
|
|
|
|
ctx, _ := context.WithDeadline(context.Background(), now)
|
|
|
|
c.SetContext(ctx)
|
|
|
|
n, _ := ctx.Deadline()
|
|
|
|
assert.Equal(t, now, n)
|
2016-06-06 07:25:34 +02:00
|
|
|
assert.Equal(t, context.DeadlineExceeded, c.Err())
|
|
|
|
assert.NotNil(t, c.Done())
|
2015-07-05 20:08:17 +02:00
|
|
|
}
|
|
|
|
|
2016-06-06 07:25:34 +02:00
|
|
|
func TestContextStore(t *testing.T) {
|
2016-08-22 17:30:46 +02:00
|
|
|
var c Context
|
|
|
|
c = new(echoContext)
|
2016-06-06 07:25:34 +02:00
|
|
|
c.Set("name", "Jon Snow")
|
|
|
|
assert.Equal(t, "Jon Snow", c.Get("name"))
|
2015-11-22 20:38:02 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 01:29:41 +02:00
|
|
|
func TestContextServeContent(t *testing.T) {
|
|
|
|
e := New()
|
2016-04-24 19:21:23 +02:00
|
|
|
req := test.NewRequest(GET, "/", nil)
|
|
|
|
rec := test.NewResponseRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
2016-03-28 01:29:41 +02:00
|
|
|
|
|
|
|
fs := http.Dir("_fixture/images")
|
|
|
|
f, err := fs.Open("walle.png")
|
|
|
|
if assert.NoError(t, err) {
|
2016-03-28 06:03:35 +02:00
|
|
|
fi, err := f.Stat()
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
// Not cached
|
|
|
|
if assert.NoError(t, c.ServeContent(f, fi.Name(), fi.ModTime())) {
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
2016-03-28 06:03:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cached
|
2016-04-24 19:21:23 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = e.NewContext(req, rec)
|
|
|
|
req.Header().Set(HeaderIfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat))
|
2016-03-28 06:03:35 +02:00
|
|
|
if assert.NoError(t, c.ServeContent(f, fi.Name(), fi.ModTime())) {
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, http.StatusNotModified, rec.Status())
|
2016-03-28 06:03:35 +02:00
|
|
|
}
|
2016-03-28 01:29:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-12 01:49:20 +02:00
|
|
|
func TestContextHandler(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
r := e.Router()
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
|
|
|
|
r.Add(GET, "/handler", func(Context) error {
|
|
|
|
_, err := b.Write([]byte("handler"))
|
|
|
|
return err
|
|
|
|
}, e)
|
2016-04-17 00:53:27 +02:00
|
|
|
c := e.NewContext(nil, nil)
|
2016-04-12 01:49:20 +02:00
|
|
|
r.Find(GET, "/handler", c)
|
|
|
|
c.Handler()(c)
|
|
|
|
assert.Equal(t, "handler", b.String())
|
|
|
|
}
|