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-05-30 02:20:13 +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-05-30 19:54:55 +02:00
|
|
|
usr := `{"id":"1","name":"Joe"}`
|
|
|
|
req, _ := http.NewRequest(POST, "/", strings.NewReader(usr))
|
|
|
|
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-04-19 01:47:48 +02:00
|
|
|
//-------
|
|
|
|
// Param
|
|
|
|
//-------
|
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// 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
|
|
|
|
|
|
|
// 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
|
|
|
|
testBind(t, c, ApplicationJSON)
|
|
|
|
|
|
|
|
// TODO: Form
|
|
|
|
c.request.Header.Set(ContentType, ApplicationForm)
|
|
|
|
u := new(user)
|
|
|
|
err := c.Bind(u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Unsupported
|
|
|
|
c.request.Header.Set(ContentType, "")
|
|
|
|
u = new(user)
|
|
|
|
err = c.Bind(u)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
//--------
|
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-05-30 19:54:55 +02:00
|
|
|
err = c.Render(http.StatusOK, "hello", "Joe")
|
|
|
|
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
|
|
|
req.Header.Set(Accept, ApplicationJSON)
|
|
|
|
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)
|
|
|
|
assert.Equal(t, ApplicationJSON, rec.Header().Get(ContentType))
|
|
|
|
assert.Equal(t, usr, strings.TrimSpace(rec.Body.String()))
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// String
|
2015-05-30 19:54:55 +02:00
|
|
|
req.Header.Set(Accept, TextPlain)
|
|
|
|
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
|
|
|
req.Header.Set(Accept, TextHTML)
|
|
|
|
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)
|
|
|
|
assert.Equal(t, TextHTML, rec.Header().Get(ContentType))
|
|
|
|
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
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-04-05 23:21:03 +02:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBind(t *testing.T, c *Context, ct string) {
|
|
|
|
c.request.Header.Set(ContentType, ct)
|
|
|
|
u := new(user)
|
|
|
|
err := c.Bind(u)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "1", u.ID)
|
|
|
|
assert.Equal(t, "Joe", u.Name)
|
|
|
|
}
|
2015-03-21 23:59:09 +02:00
|
|
|
}
|