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"
|
|
|
|
"net/http/httptest"
|
|
|
|
"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
|
|
|
|
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>"
|
2015-07-11 21:20:59 +02:00
|
|
|
|
2015-11-08 01:59:08 +02:00
|
|
|
var nonMarshallableChannel chan bool
|
|
|
|
|
2015-07-11 21:20:59 +02:00
|
|
|
req, _ := http.NewRequest(POST, "/", strings.NewReader(userJSON))
|
2015-05-30 19:54:55 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := NewContext(req, NewResponse(rec), New())
|
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
|
2015-04-26 07:32:20 +02:00
|
|
|
c.pnames = []string{"id"}
|
|
|
|
c.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
|
2015-07-16 17:05:16 +02:00
|
|
|
testBind(t, c, "application/json")
|
2015-05-30 19:54:55 +02:00
|
|
|
|
2015-07-11 21:20:59 +02:00
|
|
|
// XML
|
|
|
|
c.request, _ = http.NewRequest(POST, "/", strings.NewReader(userXML))
|
|
|
|
testBind(t, c, ApplicationXML)
|
2015-05-30 19:54:55 +02:00
|
|
|
|
|
|
|
// Unsupported
|
2015-07-11 21:20:59 +02:00
|
|
|
testBind(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
|
|
|
}
|
2015-05-31 00:20:36 +02:00
|
|
|
c.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) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "Hello, Joe!", rec.Body.String())
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
2015-05-30 19:54:55 +02:00
|
|
|
|
2015-04-11 06:48:26 +02:00
|
|
|
c.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
|
2015-05-30 19:54:55 +02:00
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
|
|
|
err = c.JSON(http.StatusOK, user{"1", "Joe"})
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
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)
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
|
|
|
val := make(chan bool)
|
|
|
|
err = c.JSON(http.StatusOK, val)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2015-11-08 01:39:09 +02:00
|
|
|
// JSONIndent
|
2015-11-01 20:48:55 +02:00
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
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) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
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)
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
|
|
|
err = c.JSONIndent(http.StatusOK, nonMarshallableChannel, "_", "?")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2015-07-24 21:28:35 +02:00
|
|
|
// JSONP
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
|
|
|
callback := "callback"
|
|
|
|
err = c.JSONP(http.StatusOK, callback, user{"1", "Joe"})
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
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
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
|
|
|
err = c.XML(http.StatusOK, user{"1", "Joe"})
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
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)
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
|
|
|
err = c.XML(http.StatusOK, nonMarshallableChannel)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2015-11-08 01:39:09 +02:00
|
|
|
// XMLIndent
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
|
|
|
err = c.XMLIndent(http.StatusOK, user{"1", "Joe"}, "_", "?")
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
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)
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
|
|
|
err = c.XMLIndent(http.StatusOK, nonMarshallableChannel, "_", "?")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// String
|
2015-05-30 19:54:55 +02:00
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
|
|
|
err = c.String(http.StatusOK, "Hello, World!")
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, TextPlain, rec.Header().Get(ContentType))
|
|
|
|
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
|
2015-05-30 19:54:55 +02:00
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
|
|
|
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
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
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
2015-10-13 16:09:53 +02:00
|
|
|
err = c.File("test/fixture/walle.png", "", false)
|
2015-08-01 04:25:03 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, 219885, rec.Body.Len())
|
|
|
|
}
|
|
|
|
|
2015-08-25 07:01:25 +02:00
|
|
|
// File as attachment
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
2015-10-13 16:09:53 +02:00
|
|
|
err = c.File("test/fixture/walle.png", "WALLE.PNG", true)
|
2015-08-25 07:01:25 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
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
|
2015-05-30 19:54:55 +02:00
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
2015-05-30 02:20:13 +02:00
|
|
|
c.NoContent(http.StatusOK)
|
|
|
|
assert.Equal(t, http.StatusOK, c.response.status)
|
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// Redirect
|
2015-05-30 19:54:55 +02:00
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
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
|
2015-05-30 19:54:55 +02:00
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = NewContext(req, NewResponse(rec), New())
|
2015-05-30 02:20:13 +02:00
|
|
|
c.Error(errors.New("error"))
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, c.response.status)
|
|
|
|
|
|
|
|
// reset
|
2015-05-30 19:54:55 +02:00
|
|
|
c.reset(req, NewResponse(httptest.NewRecorder()), New())
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
c := NewContext(nil, nil, e)
|
|
|
|
r.Find(GET, "/users/1", c)
|
|
|
|
assert.Equal(t, c.Path(), "/users/:id")
|
|
|
|
|
|
|
|
r.Add(GET, "/users/:uid/files/:fid", nil, e)
|
|
|
|
c = NewContext(nil, nil, e)
|
|
|
|
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")
|
|
|
|
|
|
|
|
req, err := http.NewRequest(GET, "/", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
req, err := http.NewRequest(POST, "/", strings.NewReader(f.Encode()))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.Header.Add(ContentType, ApplicationForm)
|
|
|
|
|
|
|
|
c := NewContext(req, nil, New())
|
|
|
|
assert.Equal(t, "joe", c.Form("name"))
|
|
|
|
assert.Equal(t, "joe@labstack.com", c.Form("email"))
|
|
|
|
}
|
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
func testBind(t *testing.T, c *Context, ct string) {
|
|
|
|
c.request.Header.Set(ContentType, ct)
|
|
|
|
u := new(user)
|
|
|
|
err := c.Bind(u)
|
2015-07-11 21:20:59 +02:00
|
|
|
if ct == "" {
|
|
|
|
assert.Error(t, UnsupportedMediaType)
|
|
|
|
} else 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
|
|
|
}
|