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
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
|
|
|
|
return t.templates.ExecuteTemplate(w, name, data)
|
|
|
|
}
|
|
|
|
|
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-04-05 23:21:03 +02:00
|
|
|
c := &Context{
|
2015-04-09 23:59:31 +02:00
|
|
|
Response: &response{Writer: httptest.NewRecorder()},
|
2015-04-05 23:21:03 +02:00
|
|
|
Request: r,
|
2015-04-26 07:32:20 +02:00
|
|
|
pvalues: make([]string, 5),
|
2015-04-05 23:21:03 +02:00
|
|
|
store: make(store),
|
2015-04-15 19:10:05 +02:00
|
|
|
echo: 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
|
|
|
|
r.Header.Set(HeaderContentType, MIMEJSON)
|
2015-04-05 23:21:03 +02:00
|
|
|
u2 := new(user)
|
|
|
|
if err := c.Bind(u2); err != nil {
|
|
|
|
t.Error(err)
|
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
|
|
|
|
r.Header.Set(HeaderContentType, MIMEForm)
|
|
|
|
u2 = new(user)
|
|
|
|
if err := c.Bind(u2); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
// TODO: add verification
|
|
|
|
|
|
|
|
// Unsupported
|
|
|
|
r.Header.Set(HeaderContentType, "")
|
|
|
|
u2 = new(user)
|
|
|
|
if err := c.Bind(u2); err == nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
// 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-04-19 01:47:48 +02:00
|
|
|
if err := c.Render(http.StatusOK, "hello", "Joe"); err != nil {
|
2015-04-11 06:48:26 +02:00
|
|
|
t.Errorf("render %v", err)
|
|
|
|
}
|
|
|
|
c.echo.renderer = nil
|
2015-04-19 01:47:48 +02:00
|
|
|
if err := c.Render(http.StatusOK, "hello", "Joe"); err == nil {
|
2015-04-11 06:48:26 +02:00
|
|
|
t.Error("render should error out")
|
|
|
|
}
|
|
|
|
|
2015-04-05 23:21:03 +02:00
|
|
|
// JSON
|
|
|
|
r.Header.Set(HeaderAccept, MIMEJSON)
|
2015-04-11 06:48:26 +02:00
|
|
|
c.Response.committed = false
|
2015-04-07 22:02:23 +02:00
|
|
|
if err := c.JSON(http.StatusOK, u1); err != nil {
|
2015-04-11 06:48:26 +02:00
|
|
|
t.Errorf("json %v", err)
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// String
|
|
|
|
r.Header.Set(HeaderAccept, MIMEText)
|
|
|
|
c.Response.committed = false
|
2015-04-07 22:02:23 +02:00
|
|
|
if err := c.String(http.StatusOK, "Hello, World!"); err != nil {
|
2015-04-11 06:48:26 +02:00
|
|
|
t.Errorf("string %v", err)
|
2015-04-05 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 22:02:23 +02:00
|
|
|
// HTML
|
2015-04-05 23:21:03 +02:00
|
|
|
r.Header.Set(HeaderAccept, MIMEHTML)
|
|
|
|
c.Response.committed = false
|
2015-04-07 22:02:23 +02:00
|
|
|
if err := c.HTML(http.StatusOK, "Hello, <strong>World!</strong>"); err != nil {
|
2015-04-11 06:48:26 +02:00
|
|
|
t.Errorf("html %v", err)
|
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
|
|
|
}
|