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 (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
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-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-06 06:55:49 +02:00
|
|
|
func (t *Template) Render(w io.Writer, name string, data interface{}) *HTTPError {
|
|
|
|
if err := t.templates.ExecuteTemplate(w, name, data); err != nil {
|
|
|
|
return &HTTPError{Error: err}
|
|
|
|
}
|
|
|
|
return nil
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
|
|
|
|
2015-03-31 05:54:38 +02:00
|
|
|
func TestContext(t *testing.T) {
|
2015-04-05 23:21:03 +02:00
|
|
|
b, _ := json.Marshal(u1)
|
2015-04-06 06:49:55 +02:00
|
|
|
r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b))
|
2015-05-08 20:52:06 +02:00
|
|
|
c := NewContext(r, &Response{Writer: httptest.NewRecorder()}, New())
|
2015-04-05 23:21:03 +02:00
|
|
|
|
2015-04-19 01:47:48 +02:00
|
|
|
//------
|
|
|
|
// Bind
|
|
|
|
//------
|
|
|
|
|
2015-04-06 00:30:03 +02:00
|
|
|
// JSON
|
2015-05-11 07:34:31 +02:00
|
|
|
r.Header.Set(ContentType, ApplicationJSON)
|
2015-04-05 23:21:03 +02:00
|
|
|
u2 := new(user)
|
2015-05-06 06:55:49 +02:00
|
|
|
if he := c.Bind(u2); he != nil {
|
|
|
|
t.Errorf("bind %#v", he)
|
2015-03-31 05:54:38 +02:00
|
|
|
}
|
2015-04-05 23:21:03 +02:00
|
|
|
verifyUser(u2, t)
|
|
|
|
|
2015-04-06 00:30:03 +02:00
|
|
|
// FORM
|
2015-05-11 07:34:31 +02:00
|
|
|
r.Header.Set(ContentType, ApplicationForm)
|
2015-04-06 00:30:03 +02:00
|
|
|
u2 = new(user)
|
2015-05-06 06:55:49 +02:00
|
|
|
if he := c.Bind(u2); he != nil {
|
|
|
|
t.Errorf("bind %#v", he)
|
2015-04-06 00:30:03 +02:00
|
|
|
}
|
|
|
|
// TODO: add verification
|
|
|
|
|
|
|
|
// Unsupported
|
2015-05-11 07:34:31 +02:00
|
|
|
r.Header.Set(ContentType, "")
|
2015-04-06 00:30:03 +02:00
|
|
|
u2 = new(user)
|
2015-05-06 06:55:49 +02:00
|
|
|
if he := c.Bind(u2); he == nil {
|
|
|
|
t.Errorf("bind %#v", he)
|
2015-04-06 00:30:03 +02:00
|
|
|
}
|
|
|
|
// TODO: add verification
|
|
|
|
|
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-04-06 05:08:52 +02:00
|
|
|
if c.P(0) != "1" {
|
|
|
|
t.Error("param id should be 1")
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// By name
|
2015-04-06 05:08:52 +02:00
|
|
|
if c.Param("id") != "1" {
|
|
|
|
t.Error("param id should be 1")
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store
|
|
|
|
c.Set("user", u1.Name)
|
|
|
|
n := c.Get("user")
|
|
|
|
if n != u1.Name {
|
|
|
|
t.Error("user name should be Joe")
|
|
|
|
}
|
|
|
|
|
2015-04-11 06:48:26 +02:00
|
|
|
// Render
|
|
|
|
tpl := &Template{
|
|
|
|
templates: template.Must(template.New("hello").Parse("{{.}}")),
|
|
|
|
}
|
|
|
|
c.echo.renderer = tpl
|
2015-05-06 06:55:49 +02:00
|
|
|
if he := c.Render(http.StatusOK, "hello", "Joe"); he != nil {
|
|
|
|
t.Errorf("render %#v", he.Error)
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
|
|
|
c.echo.renderer = nil
|
2015-05-06 06:55:49 +02:00
|
|
|
if he := c.Render(http.StatusOK, "hello", "Joe"); he.Error == nil {
|
2015-04-11 06:48:26 +02:00
|
|
|
t.Error("render should error out")
|
|
|
|
}
|
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// JSON
|
2015-05-11 07:34:31 +02:00
|
|
|
r.Header.Set(Accept, ApplicationJSON)
|
2015-04-11 06:48:26 +02:00
|
|
|
c.Response.committed = false
|
2015-05-06 06:55:49 +02:00
|
|
|
if he := c.JSON(http.StatusOK, u1); he != nil {
|
|
|
|
t.Errorf("json %#v", he)
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// String
|
2015-05-11 07:34:31 +02:00
|
|
|
r.Header.Set(Accept, TextPlain)
|
2015-04-05 23:21:03 +02:00
|
|
|
c.Response.committed = false
|
2015-05-06 06:55:49 +02:00
|
|
|
if he := c.String(http.StatusOK, "Hello, World!"); he != nil {
|
|
|
|
t.Errorf("string %#v", he.Error)
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 22:02:23 +02:00
|
|
|
// HTML
|
2015-05-11 07:34:31 +02:00
|
|
|
r.Header.Set(Accept, TextHTML)
|
2015-04-05 23:21:03 +02:00
|
|
|
c.Response.committed = false
|
2015-05-06 06:55:49 +02:00
|
|
|
if he := c.HTML(http.StatusOK, "Hello, <strong>World!</strong>"); he != nil {
|
|
|
|
t.Errorf("html %v", he.Error)
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect
|
2015-04-07 22:02:23 +02:00
|
|
|
c.Response.committed = false
|
2015-04-05 23:21:03 +02:00
|
|
|
c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo")
|
2015-03-21 23:59:09 +02:00
|
|
|
}
|