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-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
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
|
|
|
|
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
|
|
|
|
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()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON))
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c := e.NewContext(req, rec).(*context)
|
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-09-23 07:53:44 +02:00
|
|
|
assert.NotNil(t, c.Request())
|
2015-05-30 02:20:13 +02:00
|
|
|
|
|
|
|
// Response
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.NotNil(t, c.Response())
|
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
|
|
|
//--------
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
tmpl := &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-09-23 07:53:44 +02:00
|
|
|
c.echo.Renderer = tmpl
|
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-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
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-09-23 07:53:44 +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-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
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-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, userJSON, rec.Body.String())
|
2015-07-24 21:28:35 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 06:41:46 +02:00
|
|
|
// JSON with "?pretty"
|
|
|
|
req = httptest.NewRequest(GET, "/?pretty", nil)
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
|
|
|
err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, userJSONPretty, rec.Body.String())
|
|
|
|
}
|
|
|
|
req = httptest.NewRequest(GET, "/", nil) // reset
|
|
|
|
|
2016-12-09 21:13:55 +02:00
|
|
|
// JSONPretty
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
2017-04-28 06:41:46 +02:00
|
|
|
err = c.JSONPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
|
2016-12-09 21:13:55 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, userJSONPretty, rec.Body.String())
|
|
|
|
}
|
|
|
|
|
2015-11-08 01:59:08 +02:00
|
|
|
// JSON (error)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
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-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
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-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2016-04-24 19:21:23 +02:00
|
|
|
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-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
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-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2016-04-24 19:21:23 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-28 06:41:46 +02:00
|
|
|
// XML with "?pretty"
|
|
|
|
req = httptest.NewRequest(GET, "/?pretty", nil)
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
|
|
|
err = c.XML(http.StatusOK, user{1, "Jon Snow"})
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, xml.Header+userXMLPretty, rec.Body.String())
|
|
|
|
}
|
|
|
|
req = httptest.NewRequest(GET, "/", nil)
|
|
|
|
|
2015-11-08 01:59:08 +02:00
|
|
|
// XML (error)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
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)
|
|
|
|
|
2016-12-09 21:13:55 +02:00
|
|
|
// XMLPretty
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec).(*context)
|
2017-04-28 06:41:46 +02:00
|
|
|
err = c.XMLPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
|
2016-12-09 21:13:55 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, xml.Header+userXMLPretty, rec.Body.String())
|
|
|
|
}
|
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// String
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2015-05-30 19:54:55 +02:00
|
|
|
err = c.String(http.StatusOK, "Hello, World!")
|
|
|
|
if assert.NoError(t, err) {
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2016-04-24 19:21:23 +02:00
|
|
|
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-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2015-05-30 19:54:55 +02:00
|
|
|
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
|
|
|
|
if assert.NoError(t, err) {
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2016-04-24 19:21:23 +02:00
|
|
|
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-09-10 00:30:07 +02:00
|
|
|
// Stream
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2016-09-10 00:30:07 +02:00
|
|
|
r := strings.NewReader("response from a stream")
|
|
|
|
err = c.Stream(http.StatusOK, "application/octet-stream", r)
|
|
|
|
if assert.NoError(t, err) {
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2016-09-10 00:30:07 +02:00
|
|
|
assert.Equal(t, "application/octet-stream", rec.Header().Get(HeaderContentType))
|
|
|
|
assert.Equal(t, "response from a stream", rec.Body.String())
|
|
|
|
}
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
// Attachment
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2016-10-25 23:53:17 +02:00
|
|
|
err = c.Attachment("_fixture/images/walle.png", "walle.png")
|
2015-08-01 04:25:03 +02:00
|
|
|
if assert.NoError(t, err) {
|
2016-10-25 23:53:17 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "attachment; filename=walle.png", rec.Header().Get(HeaderContentDisposition))
|
|
|
|
assert.Equal(t, 219885, rec.Body.Len())
|
2015-08-25 07:01:25 +02:00
|
|
|
}
|
|
|
|
|
2016-08-22 23:46:21 +02:00
|
|
|
// Inline
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2016-10-25 23:53:17 +02:00
|
|
|
err = c.Inline("_fixture/images/walle.png", "walle.png")
|
2016-08-22 23:46:21 +02:00
|
|
|
if assert.NoError(t, err) {
|
2016-10-25 23:53:17 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "inline; filename=walle.png", rec.Header().Get(HeaderContentDisposition))
|
|
|
|
assert.Equal(t, 219885, rec.Body.Len())
|
2016-08-22 23:46:21 +02:00
|
|
|
}
|
|
|
|
|
2015-05-30 02:20:13 +02:00
|
|
|
// NoContent
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2015-05-30 02:20:13 +02:00
|
|
|
c.NoContent(http.StatusOK)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2015-05-30 02:20:13 +02:00
|
|
|
|
|
|
|
// Error
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c = e.NewContext(req, rec).(*context)
|
2015-05-30 02:20:13 +02:00
|
|
|
c.Error(errors.New("error"))
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
2015-05-30 02:20:13 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Reset
|
2017-03-30 12:05:24 +02:00
|
|
|
c.SetParamNames("foo")
|
|
|
|
c.SetParamValues("bar")
|
|
|
|
c.Set("foe", "ban")
|
|
|
|
c.query = url.Values(map[string][]string{"fon": []string{"baz"}})
|
2016-09-23 07:53:44 +02:00
|
|
|
c.Reset(req, httptest.NewRecorder())
|
2017-03-30 12:05:24 +02:00
|
|
|
assert.Equal(t, 0, len(c.ParamValues()))
|
|
|
|
assert.Equal(t, 0, len(c.ParamNames()))
|
|
|
|
assert.Equal(t, 0, len(c.store))
|
|
|
|
assert.Equal(t, "", c.Path())
|
|
|
|
assert.Equal(t, 0, len(c.QueryParams()))
|
2015-05-30 19:54:55 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:19:35 +02:00
|
|
|
func TestContextCookie(t *testing.T) {
|
|
|
|
e := New()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(GET, "/", nil)
|
2016-05-03 01:19:35 +02:00
|
|
|
theme := "theme=light"
|
|
|
|
user := "user=Jon Snow"
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Add(HeaderCookie, theme)
|
|
|
|
req.Header.Add(HeaderCookie, user)
|
|
|
|
rec := httptest.NewRecorder()
|
2016-10-11 02:31:26 +02:00
|
|
|
c := e.NewContext(req, rec).(*context)
|
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) {
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, "theme", cookie.Name)
|
|
|
|
assert.Equal(t, "light", cookie.Value)
|
2016-05-03 07:41:07 +02:00
|
|
|
}
|
2016-05-03 01:19:35 +02:00
|
|
|
|
|
|
|
// Read multiple
|
|
|
|
for _, cookie := range c.Cookies() {
|
2016-09-23 07:53:44 +02:00
|
|
|
switch cookie.Name {
|
2016-05-03 01:19:35 +02:00
|
|
|
case "theme":
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, "light", cookie.Value)
|
2016-05-03 01:19:35 +02:00
|
|
|
case "user":
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, "Jon Snow", cookie.Value)
|
2016-05-03 01:19:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write
|
2016-09-23 07:53:44 +02:00
|
|
|
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,
|
2016-09-23 07:53:44 +02:00
|
|
|
}
|
2016-05-03 01:19:35 +02:00
|
|
|
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()
|
|
|
|
|
2016-10-22 05:36:49 +02:00
|
|
|
r.Add(GET, "/users/:id", nil)
|
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
|
|
|
|
2016-10-22 05:36:49 +02:00
|
|
|
r.Add(GET, "/users/:uid/files/:fid", nil)
|
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()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.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())
|
|
|
|
|
|
|
|
// Param
|
|
|
|
assert.Equal(t, "501", c.Param("fid"))
|
2015-07-05 20:08:17 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 19:36:57 +02:00
|
|
|
func TestContextPathParamNamesAlais(t *testing.T) {
|
|
|
|
e := New()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(GET, "/", nil)
|
2016-11-13 19:36:57 +02:00
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
|
|
|
|
c.SetParamNames("id,name")
|
|
|
|
c.SetParamValues("joe")
|
|
|
|
|
|
|
|
assert.Equal(t, "joe", c.Param("id"))
|
|
|
|
assert.Equal(t, "joe", c.Param("name"))
|
|
|
|
}
|
|
|
|
|
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()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(POST, "/", strings.NewReader(f.Encode()))
|
2016-09-23 07:53:44 +02:00
|
|
|
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
|
2016-09-23 07:53:44 +02:00
|
|
|
params, err := c.FormParams()
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, url.Values{
|
|
|
|
"name": []string{"Jon Snow"},
|
|
|
|
"email": []string{"jon@labstack.com"},
|
|
|
|
}, params)
|
|
|
|
}
|
2016-06-06 07:25:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextQueryParam(t *testing.T) {
|
|
|
|
q := make(url.Values)
|
|
|
|
q.Set("name", "Jon Snow")
|
|
|
|
q.Set("email", "jon@labstack.com")
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(GET, "/?"+q.Encode(), nil)
|
2016-06-06 07:25:34 +02:00
|
|
|
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
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, url.Values{
|
2016-06-06 07:25:34 +02:00
|
|
|
"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()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(POST, "/", buf)
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Set(HeaderContentType, mr.FormDataContentType())
|
|
|
|
rec := httptest.NewRecorder()
|
2016-06-06 07:25:34 +02:00
|
|
|
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()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(POST, "/", buf)
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Set(HeaderContentType, mw.FormDataContentType())
|
|
|
|
rec := httptest.NewRecorder()
|
2016-06-06 07:25:34 +02:00
|
|
|
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()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(GET, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2016-06-06 07:25:34 +02:00
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, rec.Code)
|
2016-06-06 07:25:34 +02:00
|
|
|
assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(HeaderLocation))
|
|
|
|
assert.Error(t, c.Redirect(310, "http://labstack.github.io/echo"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextStore(t *testing.T) {
|
2016-08-22 17:30:46 +02:00
|
|
|
var c Context
|
2016-10-11 02:31:26 +02:00
|
|
|
c = new(context)
|
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-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
|
2016-10-22 05:36:49 +02:00
|
|
|
})
|
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())
|
|
|
|
}
|