1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-22 20:06:21 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-10-07 19:25:01 -07:00 committed by GitHub
parent 9c0e9d689f
commit 1924528614
5 changed files with 119 additions and 137 deletions

View File

@ -17,20 +17,18 @@ import (
"bytes" "bytes"
"github.com/labstack/echo/context" gcontext "github.com/labstack/echo/context"
) )
type ( type (
// Context represents the context of the current HTTP request. It holds request and // Context represents the context of the current HTTP request. It holds request and
// response objects, path, path parameters, data and registered handler. // response objects, path, path parameters, data and registered handler.
Context interface { Context interface {
context.Context
// StdContext returns `context.Context`. // StdContext returns `context.Context`.
StdContext() context.Context StdContext() gcontext.Context
// SetStdContext sets `context.Context`. // SetStdContext sets `context.Context`.
SetStdContext(context.Context) SetStdContext(gcontext.Context)
// Request returns `engine.Request` interface. // Request returns `engine.Request` interface.
Request() engine.Request Request() engine.Request
@ -187,63 +185,50 @@ type (
Reset(engine.Request, engine.Response) Reset(engine.Request, engine.Response)
} }
echoContext struct { context struct {
context context.Context stdContext gcontext.Context
request engine.Request request engine.Request
response engine.Response response engine.Response
path string path string
pnames []string pnames []string
pvalues []string pvalues []string
handler HandlerFunc handler HandlerFunc
store store
echo *Echo echo *Echo
} }
store map[string]interface{}
) )
const ( const (
indexPage = "index.html" indexPage = "index.html"
) )
func (c *echoContext) StdContext() context.Context { func (c *context) StdContext() gcontext.Context {
return c.context return c.stdContext
} }
func (c *echoContext) SetStdContext(ctx context.Context) { func (c *context) SetStdContext(ctx gcontext.Context) {
c.context = ctx c.stdContext = ctx
} }
func (c *echoContext) Deadline() (deadline time.Time, ok bool) { func (c *context) Request() engine.Request {
return c.context.Deadline()
}
func (c *echoContext) Done() <-chan struct{} {
return c.context.Done()
}
func (c *echoContext) Err() error {
return c.context.Err()
}
func (c *echoContext) Value(key interface{}) interface{} {
return c.context.Value(key)
}
func (c *echoContext) Request() engine.Request {
return c.request return c.request
} }
func (c *echoContext) Response() engine.Response { func (c *context) Response() engine.Response {
return c.response return c.response
} }
func (c *echoContext) Path() string { func (c *context) Path() string {
return c.path return c.path
} }
func (c *echoContext) SetPath(p string) { func (c *context) SetPath(p string) {
c.path = p c.path = p
} }
func (c *echoContext) P(i int) (value string) { func (c *context) P(i int) (value string) {
l := len(c.pnames) l := len(c.pnames)
if i < l { if i < l {
value = c.pvalues[i] value = c.pvalues[i]
@ -251,7 +236,7 @@ func (c *echoContext) P(i int) (value string) {
return return
} }
func (c *echoContext) Param(name string) (value string) { func (c *context) Param(name string) (value string) {
l := len(c.pnames) l := len(c.pnames)
for i, n := range c.pnames { for i, n := range c.pnames {
if n == name && i < l { if n == name && i < l {
@ -262,71 +247,74 @@ func (c *echoContext) Param(name string) (value string) {
return return
} }
func (c *echoContext) ParamNames() []string { func (c *context) ParamNames() []string {
return c.pnames return c.pnames
} }
func (c *echoContext) SetParamNames(names ...string) { func (c *context) SetParamNames(names ...string) {
c.pnames = names c.pnames = names
} }
func (c *echoContext) ParamValues() []string { func (c *context) ParamValues() []string {
return c.pvalues return c.pvalues
} }
func (c *echoContext) SetParamValues(values ...string) { func (c *context) SetParamValues(values ...string) {
c.pvalues = values c.pvalues = values
} }
func (c *echoContext) QueryParam(name string) string { func (c *context) QueryParam(name string) string {
return c.request.URL().QueryParam(name) return c.request.URL().QueryParam(name)
} }
func (c *echoContext) QueryParams() map[string][]string { func (c *context) QueryParams() map[string][]string {
return c.request.URL().QueryParams() return c.request.URL().QueryParams()
} }
func (c *echoContext) FormValue(name string) string { func (c *context) FormValue(name string) string {
return c.request.FormValue(name) return c.request.FormValue(name)
} }
func (c *echoContext) FormParams() map[string][]string { func (c *context) FormParams() map[string][]string {
return c.request.FormParams() return c.request.FormParams()
} }
func (c *echoContext) FormFile(name string) (*multipart.FileHeader, error) { func (c *context) FormFile(name string) (*multipart.FileHeader, error) {
return c.request.FormFile(name) return c.request.FormFile(name)
} }
func (c *echoContext) MultipartForm() (*multipart.Form, error) { func (c *context) MultipartForm() (*multipart.Form, error) {
return c.request.MultipartForm() return c.request.MultipartForm()
} }
func (c *echoContext) Cookie(name string) (engine.Cookie, error) { func (c *context) Cookie(name string) (engine.Cookie, error) {
return c.request.Cookie(name) return c.request.Cookie(name)
} }
func (c *echoContext) SetCookie(cookie engine.Cookie) { func (c *context) SetCookie(cookie engine.Cookie) {
c.response.SetCookie(cookie) c.response.SetCookie(cookie)
} }
func (c *echoContext) Cookies() []engine.Cookie { func (c *context) Cookies() []engine.Cookie {
return c.request.Cookies() return c.request.Cookies()
} }
func (c *echoContext) Set(key string, val interface{}) { func (c *context) Set(key string, val interface{}) {
c.context = context.WithValue(c.context, key, val) if c.store == nil {
c.store = make(store)
}
c.store[key] = val
} }
func (c *echoContext) Get(key string) interface{} { func (c *context) Get(key string) interface{} {
return c.context.Value(key) return c.store[key]
} }
func (c *echoContext) Bind(i interface{}) error { func (c *context) Bind(i interface{}) error {
return c.echo.binder.Bind(i, c) return c.echo.binder.Bind(i, c)
} }
func (c *echoContext) Render(code int, name string, data interface{}) (err error) { func (c *context) Render(code int, name string, data interface{}) (err error) {
if c.echo.renderer == nil { if c.echo.renderer == nil {
return ErrRendererNotRegistered return ErrRendererNotRegistered
} }
@ -340,21 +328,21 @@ func (c *echoContext) Render(code int, name string, data interface{}) (err error
return return
} }
func (c *echoContext) HTML(code int, html string) (err error) { func (c *context) HTML(code int, html string) (err error) {
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8) c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
c.response.WriteHeader(code) c.response.WriteHeader(code)
_, err = c.response.Write([]byte(html)) _, err = c.response.Write([]byte(html))
return return
} }
func (c *echoContext) String(code int, s string) (err error) { func (c *context) String(code int, s string) (err error) {
c.response.Header().Set(HeaderContentType, MIMETextPlainCharsetUTF8) c.response.Header().Set(HeaderContentType, MIMETextPlainCharsetUTF8)
c.response.WriteHeader(code) c.response.WriteHeader(code)
_, err = c.response.Write([]byte(s)) _, err = c.response.Write([]byte(s))
return return
} }
func (c *echoContext) JSON(code int, i interface{}) (err error) { func (c *context) JSON(code int, i interface{}) (err error) {
b, err := json.Marshal(i) b, err := json.Marshal(i)
if c.echo.Debug() { if c.echo.Debug() {
b, err = json.MarshalIndent(i, "", " ") b, err = json.MarshalIndent(i, "", " ")
@ -365,11 +353,11 @@ func (c *echoContext) JSON(code int, i interface{}) (err error) {
return c.JSONBlob(code, b) return c.JSONBlob(code, b)
} }
func (c *echoContext) JSONBlob(code int, b []byte) (err error) { func (c *context) JSONBlob(code int, b []byte) (err error) {
return c.Blob(code, MIMEApplicationJSONCharsetUTF8, b) return c.Blob(code, MIMEApplicationJSONCharsetUTF8, b)
} }
func (c *echoContext) JSONP(code int, callback string, i interface{}) (err error) { func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
b, err := json.Marshal(i) b, err := json.Marshal(i)
if err != nil { if err != nil {
return err return err
@ -377,7 +365,7 @@ func (c *echoContext) JSONP(code int, callback string, i interface{}) (err error
return c.JSONPBlob(code, callback, b) return c.JSONPBlob(code, callback, b)
} }
func (c *echoContext) JSONPBlob(code int, callback string, b []byte) (err error) { func (c *context) JSONPBlob(code int, callback string, b []byte) (err error) {
c.response.Header().Set(HeaderContentType, MIMEApplicationJavaScriptCharsetUTF8) c.response.Header().Set(HeaderContentType, MIMEApplicationJavaScriptCharsetUTF8)
c.response.WriteHeader(code) c.response.WriteHeader(code)
if _, err = c.response.Write([]byte(callback + "(")); err != nil { if _, err = c.response.Write([]byte(callback + "(")); err != nil {
@ -390,7 +378,7 @@ func (c *echoContext) JSONPBlob(code int, callback string, b []byte) (err error)
return return
} }
func (c *echoContext) XML(code int, i interface{}) (err error) { func (c *context) XML(code int, i interface{}) (err error) {
b, err := xml.Marshal(i) b, err := xml.Marshal(i)
if c.echo.Debug() { if c.echo.Debug() {
b, err = xml.MarshalIndent(i, "", " ") b, err = xml.MarshalIndent(i, "", " ")
@ -401,28 +389,28 @@ func (c *echoContext) XML(code int, i interface{}) (err error) {
return c.XMLBlob(code, b) return c.XMLBlob(code, b)
} }
func (c *echoContext) XMLBlob(code int, b []byte) (err error) { func (c *context) XMLBlob(code int, b []byte) (err error) {
if _, err = c.response.Write([]byte(xml.Header)); err != nil { if _, err = c.response.Write([]byte(xml.Header)); err != nil {
return return
} }
return c.Blob(code, MIMEApplicationXMLCharsetUTF8, b) return c.Blob(code, MIMEApplicationXMLCharsetUTF8, b)
} }
func (c *echoContext) Blob(code int, contentType string, b []byte) (err error) { func (c *context) Blob(code int, contentType string, b []byte) (err error) {
c.response.Header().Set(HeaderContentType, contentType) c.response.Header().Set(HeaderContentType, contentType)
c.response.WriteHeader(code) c.response.WriteHeader(code)
_, err = c.response.Write(b) _, err = c.response.Write(b)
return return
} }
func (c *echoContext) Stream(code int, contentType string, r io.Reader) (err error) { func (c *context) Stream(code int, contentType string, r io.Reader) (err error) {
c.response.Header().Set(HeaderContentType, contentType) c.response.Header().Set(HeaderContentType, contentType)
c.response.WriteHeader(code) c.response.WriteHeader(code)
_, err = io.Copy(c.response, r) _, err = io.Copy(c.response, r)
return return
} }
func (c *echoContext) File(file string) error { func (c *context) File(file string) error {
f, err := os.Open(file) f, err := os.Open(file)
if err != nil { if err != nil {
return ErrNotFound return ErrNotFound
@ -443,15 +431,15 @@ func (c *echoContext) File(file string) error {
return c.ServeContent(f, fi.Name(), fi.ModTime()) return c.ServeContent(f, fi.Name(), fi.ModTime())
} }
func (c *echoContext) Attachment(r io.ReadSeeker, name string) (err error) { func (c *context) Attachment(r io.ReadSeeker, name string) (err error) {
return c.contentDisposition(r, name, "attachment") return c.contentDisposition(r, name, "attachment")
} }
func (c *echoContext) Inline(r io.ReadSeeker, name string) (err error) { func (c *context) Inline(r io.ReadSeeker, name string) (err error) {
return c.contentDisposition(r, name, "inline") return c.contentDisposition(r, name, "inline")
} }
func (c *echoContext) contentDisposition(r io.ReadSeeker, name, dispositionType string) (err error) { func (c *context) contentDisposition(r io.ReadSeeker, name, dispositionType string) (err error) {
c.response.Header().Set(HeaderContentType, ContentTypeByExtension(name)) c.response.Header().Set(HeaderContentType, ContentTypeByExtension(name))
c.response.Header().Set(HeaderContentDisposition, fmt.Sprintf("%s; filename=%s", dispositionType, name)) c.response.Header().Set(HeaderContentDisposition, fmt.Sprintf("%s; filename=%s", dispositionType, name))
c.response.WriteHeader(http.StatusOK) c.response.WriteHeader(http.StatusOK)
@ -459,12 +447,12 @@ func (c *echoContext) contentDisposition(r io.ReadSeeker, name, dispositionType
return return
} }
func (c *echoContext) NoContent(code int) error { func (c *context) NoContent(code int) error {
c.response.WriteHeader(code) c.response.WriteHeader(code)
return nil return nil
} }
func (c *echoContext) Redirect(code int, url string) error { func (c *context) Redirect(code int, url string) error {
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect { if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
return ErrInvalidRedirectCode return ErrInvalidRedirectCode
} }
@ -473,27 +461,27 @@ func (c *echoContext) Redirect(code int, url string) error {
return nil return nil
} }
func (c *echoContext) Error(err error) { func (c *context) Error(err error) {
c.echo.httpErrorHandler(err, c) c.echo.httpErrorHandler(err, c)
} }
func (c *echoContext) Echo() *Echo { func (c *context) Echo() *Echo {
return c.echo return c.echo
} }
func (c *echoContext) Handler() HandlerFunc { func (c *context) Handler() HandlerFunc {
return c.handler return c.handler
} }
func (c *echoContext) SetHandler(h HandlerFunc) { func (c *context) SetHandler(h HandlerFunc) {
c.handler = h c.handler = h
} }
func (c *echoContext) Logger() log.Logger { func (c *context) Logger() log.Logger {
return c.echo.logger return c.echo.logger
} }
func (c *echoContext) ServeContent(content io.ReadSeeker, name string, modtime time.Time) error { func (c *context) ServeContent(content io.ReadSeeker, name string, modtime time.Time) error {
req := c.Request() req := c.Request()
res := c.Response() res := c.Response()
@ -520,9 +508,10 @@ func ContentTypeByExtension(name string) (t string) {
return return
} }
func (c *echoContext) Reset(req engine.Request, res engine.Response) { func (c *context) Reset(req engine.Request, res engine.Response) {
c.context = context.Background() c.stdContext = gcontext.Background()
c.request = req c.request = req
c.response = res c.response = res
c.store = nil
c.handler = NotFoundHandler c.handler = NotFoundHandler
} }

View File

@ -11,10 +11,10 @@ import (
"text/template" "text/template"
"time" "time"
"golang.org/x/net/context"
"strings" "strings"
gcontext "github.com/labstack/echo/context"
"net/url" "net/url"
"encoding/xml" "encoding/xml"
@ -37,7 +37,7 @@ func TestContext(t *testing.T) {
e := New() e := New()
req := test.NewRequest(POST, "/", strings.NewReader(userJSON)) req := test.NewRequest(POST, "/", strings.NewReader(userJSON))
rec := test.NewResponseRecorder() rec := test.NewResponseRecorder()
c := e.NewContext(req, rec).(*echoContext) c := e.NewContext(req, rec).(*context)
// Echo // Echo
assert.Equal(t, e, c.Echo()) assert.Equal(t, e, c.Echo())
@ -71,7 +71,7 @@ func TestContext(t *testing.T) {
// JSON // JSON
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext) c = e.NewContext(req, rec).(*context)
err = c.JSON(http.StatusOK, user{1, "Jon Snow"}) err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
if assert.NoError(t, err) { if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Status()) assert.Equal(t, http.StatusOK, rec.Status())
@ -81,13 +81,13 @@ func TestContext(t *testing.T) {
// JSON (error) // JSON (error)
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext) c = e.NewContext(req, rec).(*context)
err = c.JSON(http.StatusOK, make(chan bool)) err = c.JSON(http.StatusOK, make(chan bool))
assert.Error(t, err) assert.Error(t, err)
// JSONP // JSONP
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext) c = e.NewContext(req, rec).(*context)
callback := "callback" callback := "callback"
err = c.JSONP(http.StatusOK, callback, user{1, "Jon Snow"}) err = c.JSONP(http.StatusOK, callback, user{1, "Jon Snow"})
if assert.NoError(t, err) { if assert.NoError(t, err) {
@ -98,7 +98,7 @@ func TestContext(t *testing.T) {
// XML // XML
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext) c = e.NewContext(req, rec).(*context)
err = c.XML(http.StatusOK, user{1, "Jon Snow"}) err = c.XML(http.StatusOK, user{1, "Jon Snow"})
if assert.NoError(t, err) { if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Status()) assert.Equal(t, http.StatusOK, rec.Status())
@ -108,13 +108,13 @@ func TestContext(t *testing.T) {
// XML (error) // XML (error)
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext) c = e.NewContext(req, rec).(*context)
err = c.XML(http.StatusOK, make(chan bool)) err = c.XML(http.StatusOK, make(chan bool))
assert.Error(t, err) assert.Error(t, err)
// String // String
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext) c = e.NewContext(req, rec).(*context)
err = c.String(http.StatusOK, "Hello, World!") err = c.String(http.StatusOK, "Hello, World!")
if assert.NoError(t, err) { if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Status()) assert.Equal(t, http.StatusOK, rec.Status())
@ -124,7 +124,7 @@ func TestContext(t *testing.T) {
// HTML // HTML
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext) c = e.NewContext(req, rec).(*context)
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>") err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
if assert.NoError(t, err) { if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Status()) assert.Equal(t, http.StatusOK, rec.Status())
@ -134,7 +134,7 @@ func TestContext(t *testing.T) {
// Stream // Stream
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext) c = e.NewContext(req, rec).(*context)
r := strings.NewReader("response from a stream") r := strings.NewReader("response from a stream")
err = c.Stream(http.StatusOK, "application/octet-stream", r) err = c.Stream(http.StatusOK, "application/octet-stream", r)
if assert.NoError(t, err) { if assert.NoError(t, err) {
@ -145,7 +145,7 @@ func TestContext(t *testing.T) {
// Attachment // Attachment
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext) c = e.NewContext(req, rec).(*context)
file, err := os.Open("_fixture/images/walle.png") file, err := os.Open("_fixture/images/walle.png")
if assert.NoError(t, err) { if assert.NoError(t, err) {
err = c.Attachment(file, "walle.png") err = c.Attachment(file, "walle.png")
@ -158,7 +158,7 @@ func TestContext(t *testing.T) {
// Inline // Inline
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext) c = e.NewContext(req, rec).(*context)
file, err = os.Open("_fixture/images/walle.png") file, err = os.Open("_fixture/images/walle.png")
if assert.NoError(t, err) { if assert.NoError(t, err) {
err = c.Inline(file, "walle.png") err = c.Inline(file, "walle.png")
@ -171,13 +171,13 @@ func TestContext(t *testing.T) {
// NoContent // NoContent
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext) c = e.NewContext(req, rec).(*context)
c.NoContent(http.StatusOK) c.NoContent(http.StatusOK)
assert.Equal(t, http.StatusOK, rec.Status()) assert.Equal(t, http.StatusOK, rec.Status())
// Error // Error
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext) c = e.NewContext(req, rec).(*context)
c.Error(errors.New("error")) c.Error(errors.New("error"))
assert.Equal(t, http.StatusInternalServerError, rec.Status()) assert.Equal(t, http.StatusInternalServerError, rec.Status())
@ -193,7 +193,7 @@ func TestContextCookie(t *testing.T) {
req.Header().Add(HeaderCookie, theme) req.Header().Add(HeaderCookie, theme)
req.Header().Add(HeaderCookie, user) req.Header().Add(HeaderCookie, user)
rec := test.NewResponseRecorder() rec := test.NewResponseRecorder()
c := e.NewContext(req, rec).(*echoContext) c := e.NewContext(req, rec).(*context)
// Read single // Read single
cookie, err := c.Cookie("theme") cookie, err := c.Cookie("theme")
@ -351,23 +351,16 @@ func TestContextRedirect(t *testing.T) {
assert.Error(t, c.Redirect(310, "http://labstack.github.io/echo")) assert.Error(t, c.Redirect(310, "http://labstack.github.io/echo"))
} }
func TestContextEmbedded(t *testing.T) { func TestStdContextEmbedded(t *testing.T) {
var c Context c := new(context)
c = new(echoContext) sc := gcontext.WithValue(nil, "key", "val")
c.SetStdContext(context.WithValue(c, "key", "val")) c.SetStdContext(sc)
assert.Equal(t, "val", c.Value("key")) assert.NotEqual(t, c, c.StdContext())
now := time.Now()
ctx, _ := context.WithDeadline(context.Background(), now)
c.SetStdContext(ctx)
n, _ := ctx.Deadline()
assert.Equal(t, now, n)
assert.Equal(t, context.DeadlineExceeded, c.Err())
assert.NotNil(t, c.Done())
} }
func TestContextStore(t *testing.T) { func TestContextStore(t *testing.T) {
var c Context var c Context
c = new(echoContext) c = new(context)
c.Set("name", "Jon Snow") c.Set("name", "Jon Snow")
assert.Equal(t, "Jon Snow", c.Get("name")) assert.Equal(t, "Jon Snow", c.Get("name"))
} }

10
echo.go
View File

@ -48,8 +48,7 @@ import (
"runtime" "runtime"
"sync" "sync"
"golang.org/x/net/context" gcontext "github.com/labstack/echo/context"
"github.com/labstack/echo/engine" "github.com/labstack/echo/engine"
"github.com/labstack/echo/log" "github.com/labstack/echo/log"
glog "github.com/labstack/gommon/log" glog "github.com/labstack/gommon/log"
@ -238,10 +237,11 @@ func New() (e *Echo) {
// NewContext returns a Context instance. // NewContext returns a Context instance.
func (e *Echo) NewContext(req engine.Request, res engine.Response) Context { func (e *Echo) NewContext(req engine.Request, res engine.Response) Context {
return &echoContext{ return &context{
context: context.Background(), stdContext: gcontext.Background(),
request: req, request: req,
response: res, response: res,
store: make(store),
echo: e, echo: e,
pvalues: make([]string, *e.maxParam), pvalues: make([]string, *e.maxParam),
handler: NotFoundHandler, handler: NotFoundHandler,
@ -541,7 +541,7 @@ func (e *Echo) ReleaseContext(c Context) {
} }
func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) { func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
c := e.pool.Get().(*echoContext) c := e.pool.Get().(*context)
c.Reset(req, res) c.Reset(req, res)
// Middleware // Middleware

View File

@ -333,7 +333,7 @@ func TestEchoHTTPError(t *testing.T) {
func TestEchoContext(t *testing.T) { func TestEchoContext(t *testing.T) {
e := New() e := New()
c := e.AcquireContext() c := e.AcquireContext()
assert.IsType(t, new(echoContext), c) assert.IsType(t, new(context), c)
e.ReleaseContext(c) e.ReleaseContext(c)
} }

View File

@ -281,7 +281,7 @@ func TestRouterStatic(t *testing.T) {
c.Set("path", path) c.Set("path", path)
return nil return nil
}, e) }, e)
c := e.NewContext(nil, nil).(*echoContext) c := e.NewContext(nil, nil).(*context)
r.Find(GET, path, c) r.Find(GET, path, c)
c.handler(c) c.handler(c)
assert.Equal(t, path, c.Get("path")) assert.Equal(t, path, c.Get("path"))
@ -293,7 +293,7 @@ func TestRouterParam(t *testing.T) {
r.Add(GET, "/users/:id", func(c Context) error { r.Add(GET, "/users/:id", func(c Context) error {
return nil return nil
}, e) }, e)
c := e.NewContext(nil, nil).(*echoContext) c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/users/1", c) r.Find(GET, "/users/1", c)
assert.Equal(t, "1", c.P(0)) assert.Equal(t, "1", c.P(0))
} }
@ -304,7 +304,7 @@ func TestRouterTwoParam(t *testing.T) {
r.Add(GET, "/users/:uid/files/:fid", func(Context) error { r.Add(GET, "/users/:uid/files/:fid", func(Context) error {
return nil return nil
}, e) }, e)
c := e.NewContext(nil, nil).(*echoContext) c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/users/1/files/1", c) r.Find(GET, "/users/1/files/1", c)
assert.Equal(t, "1", c.P(0)) assert.Equal(t, "1", c.P(0))
@ -324,7 +324,7 @@ func TestRouterParamWithSlash(t *testing.T) {
return nil return nil
}, e) }, e)
c := e.NewContext(nil, nil).(*echoContext) c := e.NewContext(nil, nil).(*context)
assert.NotPanics(t, func() { assert.NotPanics(t, func() {
r.Find(GET, "/a/1/c/d/2/3", c) r.Find(GET, "/a/1/c/d/2/3", c)
}) })
@ -344,7 +344,7 @@ func TestRouterMatchAny(t *testing.T) {
r.Add(GET, "/users/*", func(Context) error { r.Add(GET, "/users/*", func(Context) error {
return nil return nil
}, e) }, e)
c := e.NewContext(nil, nil).(*echoContext) c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/", c) r.Find(GET, "/", c)
assert.Equal(t, "", c.P(0)) assert.Equal(t, "", c.P(0))
@ -362,7 +362,7 @@ func TestRouterMicroParam(t *testing.T) {
r.Add(GET, "/:a/:b/:c", func(c Context) error { r.Add(GET, "/:a/:b/:c", func(c Context) error {
return nil return nil
}, e) }, e)
c := e.NewContext(nil, nil).(*echoContext) c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/1/2/3", c) r.Find(GET, "/1/2/3", c)
assert.Equal(t, "1", c.P(0)) assert.Equal(t, "1", c.P(0))
assert.Equal(t, "2", c.P(1)) assert.Equal(t, "2", c.P(1))
@ -377,7 +377,7 @@ func TestRouterMixParamMatchAny(t *testing.T) {
r.Add(GET, "/users/:id/*", func(c Context) error { r.Add(GET, "/users/:id/*", func(c Context) error {
return nil return nil
}, e) }, e)
c := e.NewContext(nil, nil).(*echoContext) c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/users/joe/comments", c) r.Find(GET, "/users/joe/comments", c)
c.handler(c) c.handler(c)
@ -396,7 +396,7 @@ func TestRouterMultiRoute(t *testing.T) {
r.Add(GET, "/users/:id", func(c Context) error { r.Add(GET, "/users/:id", func(c Context) error {
return nil return nil
}, e) }, e)
c := e.NewContext(nil, nil).(*echoContext) c := e.NewContext(nil, nil).(*context)
// Route > /users // Route > /users
r.Find(GET, "/users", c) r.Find(GET, "/users", c)
@ -408,7 +408,7 @@ func TestRouterMultiRoute(t *testing.T) {
assert.Equal(t, "1", c.P(0)) assert.Equal(t, "1", c.P(0))
// Route > /user // Route > /user
c = e.NewContext(nil, nil).(*echoContext) c = e.NewContext(nil, nil).(*context)
r.Find(GET, "/user", c) r.Find(GET, "/user", c)
he := c.handler(c).(*HTTPError) he := c.handler(c).(*HTTPError)
assert.Equal(t, http.StatusNotFound, he.Code) assert.Equal(t, http.StatusNotFound, he.Code)
@ -447,7 +447,7 @@ func TestRouterPriority(t *testing.T) {
c.Set("g", 7) c.Set("g", 7)
return nil return nil
}, e) }, e)
c := e.NewContext(nil, nil).(*echoContext) c := e.NewContext(nil, nil).(*context)
// Route > /users // Route > /users
r.Find(GET, "/users", c) r.Find(GET, "/users", c)
@ -490,7 +490,7 @@ func TestRouterPriority(t *testing.T) {
func TestRouterPriorityNotFound(t *testing.T) { func TestRouterPriorityNotFound(t *testing.T) {
e := New() e := New()
r := e.router r := e.router
c := e.NewContext(nil, nil).(*echoContext) c := e.NewContext(nil, nil).(*context)
// Add // Add
r.Add(GET, "/a/foo", func(c Context) error { r.Add(GET, "/a/foo", func(c Context) error {
@ -511,7 +511,7 @@ func TestRouterPriorityNotFound(t *testing.T) {
c.handler(c) c.handler(c)
assert.Equal(t, 2, c.Get("b")) assert.Equal(t, 2, c.Get("b"))
c = e.NewContext(nil, nil).(*echoContext) c = e.NewContext(nil, nil).(*context)
r.Find(GET, "/abc/def", c) r.Find(GET, "/abc/def", c)
he := c.handler(c).(*HTTPError) he := c.handler(c).(*HTTPError)
assert.Equal(t, http.StatusNotFound, he.Code) assert.Equal(t, http.StatusNotFound, he.Code)
@ -532,7 +532,7 @@ func TestRouterParamNames(t *testing.T) {
r.Add(GET, "/users/:uid/files/:fid", func(c Context) error { r.Add(GET, "/users/:uid/files/:fid", func(c Context) error {
return nil return nil
}, e) }, e)
c := e.NewContext(nil, nil).(*echoContext) c := e.NewContext(nil, nil).(*context)
// Route > /users // Route > /users
r.Find(GET, "/users", c) r.Find(GET, "/users", c)
@ -561,7 +561,7 @@ func TestRouterAPI(t *testing.T) {
return nil return nil
}, e) }, e)
} }
c := e.NewContext(nil, nil).(*echoContext) c := e.NewContext(nil, nil).(*context)
for _, route := range api { for _, route := range api {
r.Find(route.Method, route.Path, c) r.Find(route.Method, route.Path, c)
for i, n := range c.pnames { for i, n := range c.pnames {