Skip to content

Commit

Permalink
Separated recipes from the main repo
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Jan 9, 2016
1 parent 33d8813 commit dbd1e8e
Show file tree
Hide file tree
Showing 52 changed files with 424 additions and 877 deletions.
131 changes: 63 additions & 68 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
"text/template"

Expand All @@ -14,6 +13,7 @@ import (

"encoding/xml"

"github.com/labstack/echo/test"
"github.com/stretchr/testify/assert"
)

Expand All @@ -36,9 +36,9 @@ func TestContext(t *testing.T) {
var nonMarshallableChannel chan bool

e := New()
req, _ := http.NewRequest(POST, "/", strings.NewReader(userJSON))
rec := httptest.NewRecorder()
c := NewContext(req, NewResponse(rec, e), e).X()
req := test.NewRequest(POST, "/", strings.NewReader(userJSON))
rec := test.NewResponseRecorder()
c := NewContext(req, rec, e)

// Request
assert.NotNil(t, c.Request())
Expand All @@ -50,8 +50,8 @@ func TestContext(t *testing.T) {
assert.Nil(t, c.Socket())

// Param by id
c.pnames = []string{"id"}
c.pvalues = []string{"1"}
c.X().pnames = []string{"id"}
c.X().pvalues = []string{"1"}
assert.Equal(t, "1", c.P(0))

// Param by name
Expand All @@ -69,7 +69,7 @@ func TestContext(t *testing.T) {
testBind(t, c, "application/json")

// XML
c.request, _ = http.NewRequest(POST, "/", strings.NewReader(userXML))
c.X().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
testBind(t, c, ApplicationXML)

// Unsupported
Expand All @@ -82,164 +82,164 @@ func TestContext(t *testing.T) {
tpl := &Template{
templates: template.Must(template.New("hello").Parse("Hello, {{.}}!")),
}
c.echo.SetRenderer(tpl)
c.X().echo.SetRenderer(tpl)
err := c.Render(http.StatusOK, "hello", "Joe")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, "Hello, Joe!", rec.Body.String())
}

c.echo.renderer = nil
c.X().echo.renderer = nil
err = c.Render(http.StatusOK, "hello", "Joe")
assert.Error(t, err)

// JSON
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
err = c.JSON(http.StatusOK, user{"1", "Joe"})
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType))
assert.Equal(t, userJSON, rec.Body.String())
}

// JSON (error)
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
val := make(chan bool)
err = c.JSON(http.StatusOK, val)
assert.Error(t, err)

// JSONIndent
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
err = c.JSONIndent(http.StatusOK, user{"1", "Joe"}, "_", "?")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType))
assert.Equal(t, userJSONIndent, rec.Body.String())
}

// JSONIndent (error)
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
err = c.JSONIndent(http.StatusOK, nonMarshallableChannel, "_", "?")
assert.Error(t, err)

// JSONP
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
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, http.StatusOK, rec.Status())
assert.Equal(t, ApplicationJavaScriptCharsetUTF8, rec.Header().Get(ContentType))
assert.Equal(t, callback+"("+userJSON+");", rec.Body.String())
}

// XML
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
err = c.XML(http.StatusOK, user{"1", "Joe"})
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, ApplicationXMLCharsetUTF8, rec.Header().Get(ContentType))
assert.Equal(t, xml.Header+userXML, rec.Body.String())
}

// XML (error)
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
err = c.XML(http.StatusOK, nonMarshallableChannel)
assert.Error(t, err)

// XMLIndent
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
err = c.XMLIndent(http.StatusOK, user{"1", "Joe"}, "_", "?")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, ApplicationXMLCharsetUTF8, rec.Header().Get(ContentType))
assert.Equal(t, xml.Header+userXMLIndent, rec.Body.String())
}

// XMLIndent (error)
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
err = c.XMLIndent(http.StatusOK, nonMarshallableChannel, "_", "?")
assert.Error(t, err)

// String
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
err = c.String(http.StatusOK, "Hello, World!")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, TextPlainCharsetUTF8, rec.Header().Get(ContentType))
assert.Equal(t, "Hello, World!", rec.Body.String())
}

// HTML
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, TextHTMLCharsetUTF8, rec.Header().Get(ContentType))
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
}

// File
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
err = c.File("test/fixture/walle.png", "", false)
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
err = c.File("testing/fixture/walle.png", "", false)
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, 219885, rec.Body.Len())
}

// File as attachment
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
err = c.File("test/fixture/walle.png", "WALLE.PNG", true)
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
err = c.File("testing/fixture/walle.png", "WALLE.PNG", true)
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, rec.Header().Get(ContentDisposition), "attachment; filename=WALLE.PNG")
assert.Equal(t, 219885, rec.Body.Len())
}

// NoContent
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
c.NoContent(http.StatusOK)
assert.Equal(t, http.StatusOK, c.response.status)
assert.Equal(t, http.StatusOK, c.Response().Status())

// Redirect
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))

// Error
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec, e), e).X()
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e).X()
c.Error(errors.New("error"))
assert.Equal(t, http.StatusInternalServerError, c.response.status)
assert.Equal(t, http.StatusInternalServerError, c.Response().Status())

// reset
c.reset(req, NewResponse(httptest.NewRecorder(), e), e)
c.X().reset(req, test.NewResponseRecorder(), e)
}

func TestContextPath(t *testing.T) {
e := New()
r := e.Router()

r.Add(GET, "/users/:id", nil, e)
c := NewContext(nil, nil, e).X()
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).X()
c = NewContext(nil, nil, e)
r.Find(GET, "/users/1/files/1", c)
assert.Equal(t, c.Path(), "/users/:uid/files/:fid")
}
Expand All @@ -248,11 +248,7 @@ func TestContextQuery(t *testing.T) {
q := make(url.Values)
q.Set("name", "joe")
q.Set("email", "[email protected]")

req, err := http.NewRequest(GET, "/", nil)
assert.NoError(t, err)
req.URL.RawQuery = q.Encode()

req := test.NewRequest(GET, "/?"+q.Encode(), nil)
c := NewContext(req, nil, New())
assert.Equal(t, "joe", c.Query("name"))
assert.Equal(t, "[email protected]", c.Query("email"))
Expand All @@ -263,9 +259,8 @@ func TestContextForm(t *testing.T) {
f.Set("name", "joe")
f.Set("email", "[email protected]")

req, err := http.NewRequest(POST, "/", strings.NewReader(f.Encode()))
assert.NoError(t, err)
req.Header.Add(ContentType, ApplicationForm)
req := test.NewRequest(POST, "/", strings.NewReader(f.Encode()))
req.Header().Add(ContentType, ApplicationForm)

c := NewContext(req, nil, New())
assert.Equal(t, "joe", c.Form("name"))
Expand All @@ -278,8 +273,8 @@ func TestContextNetContext(t *testing.T) {
// assert.Equal(t, "val", c.Value("key"))
}

func testBind(t *testing.T, c *context, ct string) {
c.request.Header.Set(ContentType, ct)
func testBind(t *testing.T, c Context, ct string) {
c.Request().Header().Set(ContentType, ct)
u := new(user)
err := c.Bind(u)
if ct == "" {
Expand Down
36 changes: 16 additions & 20 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,19 @@ func New() (e *Echo) {

e.HTTP2(true)
e.defaultHTTPErrorHandler = func(err error, c Context) {
// TODO: v2
// x := c.X()
// code := http.StatusInternalServerError
// msg := http.StatusText(code)
// if he, ok := err.(*HTTPError); ok {
// code = he.code
// msg = he.message
// }
// if e.debug {
// msg = err.Error()
// }
// if !x.response.Committed() {
// http.Error(x.response, msg, code)
// }
// e.logger.Error(err)
code := http.StatusInternalServerError
msg := http.StatusText(code)
if he, ok := err.(*HTTPError); ok {
code = he.code
msg = he.message
}
if e.debug {
msg = err.Error()
}
if !c.Response().Committed() {
c.String(code, msg)
}
e.logger.Error(err)
}
e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
e.SetBinder(&binder{})
Expand Down Expand Up @@ -410,23 +408,21 @@ func (e *Echo) ServeFile(path, file string) {
}

func (e *Echo) serveFile(dir, file string, c Context) (err error) {
// TODO: v2
// x := c.X()
// fs := http.Dir(dir)
// f, err := fs.Open(file)
// if err != nil {
// return NewHTTPError(http.StatusNotFound)
// }
// defer f.Close()
//

// fi, _ := f.Stat()
// if fi.IsDir() {
// /* NOTE:
// Not checking the Last-Modified header as it caches the response `304` when
// changing differnt directories for the same path.
// */
// d := f
//

// // Index file
// file = filepath.Join(file, indexPage)
// f, err = fs.Open(file)
Expand All @@ -439,7 +435,7 @@ func (e *Echo) serveFile(dir, file string, c Context) (err error) {
// }
// fi, _ = f.Stat() // Index file stat
// }
// http.ServeContent(x.response, x.request, fi.Name(), fi.ModTime(), f)
// http.ServeContent(c.Response(), c.Request(), fi.Name(), fi.ModTime(), f)
return
}

Expand Down
Loading

0 comments on commit dbd1e8e

Please sign in to comment.