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 (
|
2015-05-30 02:20:13 +02:00
|
|
|
"errors"
|
2015-04-11 06:48:26 +02:00
|
|
|
"io"
|
2015-03-31 05:54:38 +02:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
2015-04-11 06:48:26 +02:00
|
|
|
"text/template"
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-05-20 23:38:51 +02:00
|
|
|
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
|
|
|
|
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-07-11 21:20:59 +02:00
|
|
|
userJSON := `{"id":"1","name":"Joe"}`
|
2015-11-08 01:39:09 +02:00
|
|
|
userJSONIndent := "{\n_?\"id\": \"1\",\n_?\"name\": \"Joe\"\n_}"
|
2015-07-11 21:20:59 +02:00
|
|
|
userXML := `<user><id>1</id><name>Joe</name></user>`
|
2015-11-08 01:39:09 +02:00
|
|
|
userXMLIndent := "_<user>\n_?<id>1</id>\n_?<name>Joe</name>\n_</user>"
|
2016-01-29 12:22:18 +02:00
|
|
|
incorrectContent := "this is incorrect content"
|
2015-07-11 21:20:59 +02:00
|
|
|
|
2015-11-08 01:59:08 +02:00
|
|
|
var nonMarshallableChannel chan bool
|
|
|
|
|
2015-12-01 21:22:45 +02:00
|
|
|
e := New()
|
2016-01-09 19:44:18 +02:00
|
|
|
req := test.NewRequest(POST, "/", strings.NewReader(userJSON))
|
|
|
|
rec := test.NewResponseRecorder()
|
|
|
|
c := NewContext(req, rec, e)
|
2015-04-05 23:21:03 +02:00
|
|
|
|
2015-05-30 02:20:13 +02:00
|
|
|
// Request
|
2015-05-30 19:54:55 +02:00
|
|
|
assert.NotNil(t, c.Request())
|
2015-05-30 02:20:13 +02:00
|
|
|
|
|
|
|
// Response
|
2015-05-30 19:54:55 +02:00
|
|
|
assert.NotNil(t, c.Response())
|
2015-05-30 02:20:13 +02:00
|
|
|
|
|
|
|
// Socket
|
|
|
|
assert.Nil(t, c.Socket())
|
|
|
|
|
2015-07-05 20:08:17 +02:00
|
|
|
// Param by id
|
2016-02-09 02:48:03 +02:00
|
|
|
c.Object().pnames = []string{"id"}
|
|
|
|
c.Object().pvalues = []string{"1"}
|
2015-05-30 19:54:55 +02:00
|
|
|
assert.Equal(t, "1", c.P(0))
|
2015-04-05 23:21:03 +02:00
|
|
|
|
2015-07-05 20:08:17 +02:00
|
|
|
// Param by name
|
2015-05-30 19:54:55 +02:00
|
|
|
assert.Equal(t, "1", c.Param("id"))
|
2015-04-05 23:21:03 +02:00
|
|
|
|
|
|
|
// Store
|
2015-05-30 19:54:55 +02:00
|
|
|
c.Set("user", "Joe")
|
|
|
|
assert.Equal(t, "Joe", c.Get("user"))
|
2015-04-05 23:21:03 +02:00
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
//------
|
|
|
|
// Bind
|
|
|
|
//------
|
|
|
|
|
|
|
|
// JSON
|
2016-01-29 12:22:18 +02:00
|
|
|
testBindOk(t, c, ApplicationJSON)
|
2016-02-09 02:48:03 +02:00
|
|
|
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
|
2016-01-29 12:22:18 +02:00
|
|
|
testBindError(t, c, ApplicationJSON)
|
2015-05-30 19:54:55 +02:00
|
|
|
|
2015-07-11 21:20:59 +02:00
|
|
|
// XML
|
2016-02-09 02:48:03 +02:00
|
|
|
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
|
2016-01-29 12:22:18 +02:00
|
|
|
testBindOk(t, c, ApplicationXML)
|
2016-02-09 02:48:03 +02:00
|
|
|
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
|
2016-01-29 12:22:18 +02:00
|
|
|
testBindError(t, c, ApplicationXML)
|
2015-05-30 19:54:55 +02:00
|
|
|
|
|
|
|
// Unsupported
|
2016-01-29 12:22:18 +02:00
|
|
|
testBindError(t, c, "")
|
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-02-09 02:48:03 +02:00
|
|
|
c.Object().echo.SetRenderer(tpl)
|
2015-07-11 21:20:59 +02:00
|
|
|
err := c.Render(http.StatusOK, "hello", "Joe")
|
2015-05-30 19:54:55 +02:00
|
|
|
if assert.NoError(t, err) {
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
2015-05-30 19:54:55 +02:00
|
|
|
assert.Equal(t, "Hello, Joe!", rec.Body.String())
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
2015-05-30 19:54:55 +02:00
|
|
|
|
2016-02-09 02:48:03 +02:00
|
|
|
c.Object().echo.renderer = nil
|
2015-05-30 19:54:55 +02:00
|
|
|
err = c.Render(http.StatusOK, "hello", "Joe")
|
|
|
|
assert.Error(t, err)
|
2015-04-11 06:48:26 +02:00
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// JSON
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2015-05-30 19:54:55 +02:00
|
|
|
err = c.JSON(http.StatusOK, user{"1", "Joe"})
|
|
|
|
if assert.NoError(t, err) {
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
2015-07-22 13:44:29 +02:00
|
|
|
assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType))
|
2015-10-02 20:23:52 +02:00
|
|
|
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-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2015-11-08 01:59:08 +02:00
|
|
|
val := make(chan bool)
|
|
|
|
err = c.JSON(http.StatusOK, val)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2015-11-08 01:39:09 +02:00
|
|
|
// JSONIndent
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2015-11-08 01:39:09 +02:00
|
|
|
err = c.JSONIndent(http.StatusOK, user{"1", "Joe"}, "_", "?")
|
2015-11-01 20:48:55 +02:00
|
|
|
if assert.NoError(t, err) {
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
2015-11-01 20:48:55 +02:00
|
|
|
assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType))
|
2015-11-08 01:39:09 +02:00
|
|
|
assert.Equal(t, userJSONIndent, rec.Body.String())
|
2015-11-01 20:48:55 +02:00
|
|
|
}
|
|
|
|
|
2015-11-08 01:59:08 +02:00
|
|
|
// JSONIndent (error)
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2015-11-08 01:59:08 +02:00
|
|
|
err = c.JSONIndent(http.StatusOK, nonMarshallableChannel, "_", "?")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2015-07-24 21:28:35 +02:00
|
|
|
// JSONP
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2015-07-24 21:28:35 +02:00
|
|
|
callback := "callback"
|
|
|
|
err = c.JSONP(http.StatusOK, callback, user{"1", "Joe"})
|
|
|
|
if assert.NoError(t, err) {
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
2015-07-24 21:28:35 +02:00
|
|
|
assert.Equal(t, ApplicationJavaScriptCharsetUTF8, rec.Header().Get(ContentType))
|
2015-10-02 20:23:52 +02:00
|
|
|
assert.Equal(t, callback+"("+userJSON+");", rec.Body.String())
|
2015-07-11 21:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// XML
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2015-07-11 21:20:59 +02:00
|
|
|
err = c.XML(http.StatusOK, user{"1", "Joe"})
|
|
|
|
if assert.NoError(t, err) {
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
2015-07-22 13:44:29 +02:00
|
|
|
assert.Equal(t, ApplicationXMLCharsetUTF8, rec.Header().Get(ContentType))
|
2015-11-08 01:39:09 +02:00
|
|
|
assert.Equal(t, xml.Header+userXML, rec.Body.String())
|
|
|
|
}
|
|
|
|
|
2015-11-08 01:59:08 +02:00
|
|
|
// XML (error)
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2015-11-08 01:59:08 +02:00
|
|
|
err = c.XML(http.StatusOK, nonMarshallableChannel)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2015-11-08 01:39:09 +02:00
|
|
|
// XMLIndent
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2015-11-08 01:39:09 +02:00
|
|
|
err = c.XMLIndent(http.StatusOK, user{"1", "Joe"}, "_", "?")
|
|
|
|
if assert.NoError(t, err) {
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
2015-11-08 01:39:09 +02:00
|
|
|
assert.Equal(t, ApplicationXMLCharsetUTF8, rec.Header().Get(ContentType))
|
|
|
|
assert.Equal(t, xml.Header+userXMLIndent, rec.Body.String())
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2015-11-08 01:59:08 +02:00
|
|
|
// XMLIndent (error)
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2015-11-08 01:59:08 +02:00
|
|
|
err = c.XMLIndent(http.StatusOK, nonMarshallableChannel, "_", "?")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// String
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2015-05-30 19:54:55 +02:00
|
|
|
err = c.String(http.StatusOK, "Hello, World!")
|
|
|
|
if assert.NoError(t, err) {
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
2015-12-01 21:22:45 +02:00
|
|
|
assert.Equal(t, TextPlainCharsetUTF8, rec.Header().Get(ContentType))
|
2015-05-30 19:54:55 +02:00
|
|
|
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-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2015-05-30 19:54:55 +02:00
|
|
|
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
|
|
|
|
if assert.NoError(t, err) {
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
2015-07-22 13:44:29 +02:00
|
|
|
assert.Equal(t, TextHTMLCharsetUTF8, rec.Header().Get(ContentType))
|
2015-05-30 19:54:55 +02:00
|
|
|
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2015-08-01 04:25:03 +02:00
|
|
|
// File
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2016-01-29 09:46:11 +02:00
|
|
|
err = c.File("_fixture/images/walle.png", "", false)
|
2015-08-01 04:25:03 +02:00
|
|
|
if assert.NoError(t, err) {
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
2015-08-01 04:25:03 +02:00
|
|
|
assert.Equal(t, 219885, rec.Body.Len())
|
|
|
|
}
|
|
|
|
|
2015-08-25 07:01:25 +02:00
|
|
|
// File as attachment
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2016-01-29 09:46:11 +02:00
|
|
|
err = c.File("_fixture/images/walle.png", "WALLE.PNG", true)
|
2015-08-25 07:01:25 +02:00
|
|
|
if assert.NoError(t, err) {
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
2015-10-11 20:43:27 +02:00
|
|
|
assert.Equal(t, rec.Header().Get(ContentDisposition), "attachment; filename=WALLE.PNG")
|
2015-08-25 07:01:25 +02:00
|
|
|
assert.Equal(t, 219885, rec.Body.Len())
|
|
|
|
}
|
|
|
|
|
2015-05-30 02:20:13 +02:00
|
|
|
// NoContent
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2015-05-30 02:20:13 +02:00
|
|
|
c.NoContent(http.StatusOK)
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, http.StatusOK, c.Response().Status())
|
2015-05-30 02:20:13 +02:00
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// Redirect
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = NewContext(req, rec, e)
|
2015-07-15 03:35:28 +02:00
|
|
|
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
|
2015-05-30 02:20:13 +02:00
|
|
|
|
|
|
|
// Error
|
2016-01-09 19:44:18 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
2016-02-09 02:48:03 +02:00
|
|
|
c = NewContext(req, rec, e).Object()
|
2015-05-30 02:20:13 +02:00
|
|
|
c.Error(errors.New("error"))
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, c.Response().Status())
|
2015-05-30 02:20:13 +02:00
|
|
|
|
|
|
|
// reset
|
2016-02-09 02:48:03 +02:00
|
|
|
c.Object().reset(req, test.NewResponseRecorder(), e)
|
2015-05-30 19:54:55 +02:00
|
|
|
}
|
|
|
|
|
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-01-09 19:44:18 +02:00
|
|
|
c := NewContext(nil, nil, e)
|
2015-11-13 06:23:14 +02:00
|
|
|
r.Find(GET, "/users/1", c)
|
|
|
|
assert.Equal(t, c.Path(), "/users/:id")
|
|
|
|
|
|
|
|
r.Add(GET, "/users/:uid/files/:fid", nil, e)
|
2016-01-09 19:44:18 +02:00
|
|
|
c = NewContext(nil, nil, e)
|
2015-11-13 06:23:14 +02:00
|
|
|
r.Find(GET, "/users/1/files/1", c)
|
|
|
|
assert.Equal(t, c.Path(), "/users/:uid/files/:fid")
|
|
|
|
}
|
|
|
|
|
2015-07-05 20:08:17 +02:00
|
|
|
func TestContextQuery(t *testing.T) {
|
|
|
|
q := make(url.Values)
|
|
|
|
q.Set("name", "joe")
|
|
|
|
q.Set("email", "joe@labstack.com")
|
2016-01-09 19:44:18 +02:00
|
|
|
req := test.NewRequest(GET, "/?"+q.Encode(), nil)
|
2015-07-05 20:08:17 +02:00
|
|
|
c := NewContext(req, nil, New())
|
|
|
|
assert.Equal(t, "joe", c.Query("name"))
|
|
|
|
assert.Equal(t, "joe@labstack.com", c.Query("email"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextForm(t *testing.T) {
|
|
|
|
f := make(url.Values)
|
|
|
|
f.Set("name", "joe")
|
|
|
|
f.Set("email", "joe@labstack.com")
|
|
|
|
|
2016-01-09 19:44:18 +02:00
|
|
|
req := test.NewRequest(POST, "/", strings.NewReader(f.Encode()))
|
|
|
|
req.Header().Add(ContentType, ApplicationForm)
|
2015-07-05 20:08:17 +02:00
|
|
|
|
|
|
|
c := NewContext(req, nil, New())
|
|
|
|
assert.Equal(t, "joe", c.Form("name"))
|
|
|
|
assert.Equal(t, "joe@labstack.com", c.Form("email"))
|
|
|
|
}
|
|
|
|
|
2015-11-22 20:38:02 +02:00
|
|
|
func TestContextNetContext(t *testing.T) {
|
2015-12-04 03:23:53 +02:00
|
|
|
// c := new(context)
|
|
|
|
// c.Context = xcontext.WithValue(nil, "key", "val")
|
|
|
|
// assert.Equal(t, "val", c.Value("key"))
|
2015-11-22 20:38:02 +02:00
|
|
|
}
|
|
|
|
|
2016-01-29 12:22:18 +02:00
|
|
|
func testBindOk(t *testing.T, c Context, ct string) {
|
2016-01-09 19:44:18 +02:00
|
|
|
c.Request().Header().Set(ContentType, ct)
|
2015-05-30 19:54:55 +02:00
|
|
|
u := new(user)
|
|
|
|
err := c.Bind(u)
|
2016-01-29 12:22:18 +02:00
|
|
|
if assert.NoError(t, err) {
|
2015-05-30 19:54:55 +02:00
|
|
|
assert.Equal(t, "1", u.ID)
|
|
|
|
assert.Equal(t, "Joe", u.Name)
|
|
|
|
}
|
2015-03-21 23:59:09 +02:00
|
|
|
}
|
2016-01-29 12:22:18 +02:00
|
|
|
|
|
|
|
func testBindError(t *testing.T, c Context, ct string) {
|
|
|
|
c.Request().Header().Set(ContentType, ct)
|
|
|
|
u := new(user)
|
|
|
|
err := c.Bind(u)
|
|
|
|
|
|
|
|
switch ct {
|
|
|
|
case ApplicationJSON, ApplicationXML:
|
|
|
|
if assert.IsType(t, new(HTTPError), err) {
|
|
|
|
assert.Equal(t, http.StatusBadRequest, err.(*HTTPError).code)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if assert.IsType(t, new(HTTPError), err) {
|
|
|
|
assert.Equal(t, UnsupportedMediaType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|