mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
parent
74c8e254ff
commit
09a2ce60a6
129
context.go
129
context.go
@ -16,20 +16,21 @@ import (
|
|||||||
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
netContext "golang.org/x/net/context"
|
"golang.org/x/net/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 {
|
||||||
netContext.Context
|
context.Context
|
||||||
|
|
||||||
// NetContext returns `http://blog.golang.org/context.Context` interface.
|
// StdContext returns `net/context.Context`. By default it is set the background
|
||||||
NetContext() netContext.Context
|
// context. To change the context, use SetStdContext().
|
||||||
|
StdContext() context.Context
|
||||||
|
|
||||||
// SetNetContext sets `http://blog.golang.org/context.Context` interface.
|
// SetStdContext sets `net/context.Context`.
|
||||||
SetNetContext(netContext.Context)
|
SetStdContext(context.Context)
|
||||||
|
|
||||||
// Request returns `engine.Request` interface.
|
// Request returns `engine.Request` interface.
|
||||||
Request() engine.Request
|
Request() engine.Request
|
||||||
@ -178,8 +179,8 @@ type (
|
|||||||
Reset(engine.Request, engine.Response)
|
Reset(engine.Request, engine.Response)
|
||||||
}
|
}
|
||||||
|
|
||||||
context struct {
|
echoContext struct {
|
||||||
netContext netContext.Context
|
context context.Context
|
||||||
request engine.Request
|
request engine.Request
|
||||||
response engine.Response
|
response engine.Response
|
||||||
path string
|
path string
|
||||||
@ -197,47 +198,47 @@ const (
|
|||||||
indexPage = "index.html"
|
indexPage = "index.html"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *context) NetContext() netContext.Context {
|
func (c *echoContext) StdContext() context.Context {
|
||||||
return c.netContext
|
return c.context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) SetNetContext(ctx netContext.Context) {
|
func (c *echoContext) SetStdContext(ctx context.Context) {
|
||||||
c.netContext = ctx
|
c.context = ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Deadline() (deadline time.Time, ok bool) {
|
func (c *echoContext) Deadline() (deadline time.Time, ok bool) {
|
||||||
return c.netContext.Deadline()
|
return c.context.Deadline()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Done() <-chan struct{} {
|
func (c *echoContext) Done() <-chan struct{} {
|
||||||
return c.netContext.Done()
|
return c.context.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Err() error {
|
func (c *echoContext) Err() error {
|
||||||
return c.netContext.Err()
|
return c.context.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Value(key interface{}) interface{} {
|
func (c *echoContext) Value(key interface{}) interface{} {
|
||||||
return c.netContext.Value(key)
|
return c.context.Value(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Request() engine.Request {
|
func (c *echoContext) Request() engine.Request {
|
||||||
return c.request
|
return c.request
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Response() engine.Response {
|
func (c *echoContext) Response() engine.Response {
|
||||||
return c.response
|
return c.response
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Path() string {
|
func (c *echoContext) Path() string {
|
||||||
return c.path
|
return c.path
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) SetPath(p string) {
|
func (c *echoContext) SetPath(p string) {
|
||||||
c.path = p
|
c.path = p
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) P(i int) (value string) {
|
func (c *echoContext) 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]
|
||||||
@ -245,7 +246,7 @@ func (c *context) P(i int) (value string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Param(name string) (value string) {
|
func (c *echoContext) 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 {
|
||||||
@ -256,83 +257,83 @@ func (c *context) Param(name string) (value string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) ParamNames() []string {
|
func (c *echoContext) ParamNames() []string {
|
||||||
return c.pnames
|
return c.pnames
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) SetParamNames(names ...string) {
|
func (c *echoContext) SetParamNames(names ...string) {
|
||||||
c.pnames = names
|
c.pnames = names
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) ParamValues() []string {
|
func (c *echoContext) ParamValues() []string {
|
||||||
return c.pvalues
|
return c.pvalues
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) SetParamValues(values ...string) {
|
func (c *echoContext) SetParamValues(values ...string) {
|
||||||
c.pvalues = values
|
c.pvalues = values
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) QueryParam(name string) string {
|
func (c *echoContext) QueryParam(name string) string {
|
||||||
return c.request.URL().QueryParam(name)
|
return c.request.URL().QueryParam(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) QueryParams() map[string][]string {
|
func (c *echoContext) QueryParams() map[string][]string {
|
||||||
return c.request.URL().QueryParams()
|
return c.request.URL().QueryParams()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) FormValue(name string) string {
|
func (c *echoContext) FormValue(name string) string {
|
||||||
return c.request.FormValue(name)
|
return c.request.FormValue(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) FormParams() map[string][]string {
|
func (c *echoContext) FormParams() map[string][]string {
|
||||||
return c.request.FormParams()
|
return c.request.FormParams()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) FormFile(name string) (*multipart.FileHeader, error) {
|
func (c *echoContext) FormFile(name string) (*multipart.FileHeader, error) {
|
||||||
return c.request.FormFile(name)
|
return c.request.FormFile(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) MultipartForm() (*multipart.Form, error) {
|
func (c *echoContext) MultipartForm() (*multipart.Form, error) {
|
||||||
return c.request.MultipartForm()
|
return c.request.MultipartForm()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Cookie(name string) (engine.Cookie, error) {
|
func (c *echoContext) Cookie(name string) (engine.Cookie, error) {
|
||||||
return c.request.Cookie(name)
|
return c.request.Cookie(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) SetCookie(cookie engine.Cookie) {
|
func (c *echoContext) SetCookie(cookie engine.Cookie) {
|
||||||
c.response.SetCookie(cookie)
|
c.response.SetCookie(cookie)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Cookies() []engine.Cookie {
|
func (c *echoContext) Cookies() []engine.Cookie {
|
||||||
return c.request.Cookies()
|
return c.request.Cookies()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Set(key string, val interface{}) {
|
func (c *echoContext) Set(key string, val interface{}) {
|
||||||
if c.store == nil {
|
if c.store == nil {
|
||||||
c.store = make(store)
|
c.store = make(store)
|
||||||
}
|
}
|
||||||
c.store[key] = val
|
c.store[key] = val
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Get(key string) interface{} {
|
func (c *echoContext) Get(key string) interface{} {
|
||||||
return c.store[key]
|
return c.store[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Del(key string) {
|
func (c *echoContext) Del(key string) {
|
||||||
delete(c.store, key)
|
delete(c.store, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Contains(key string) bool {
|
func (c *echoContext) Contains(key string) bool {
|
||||||
_, ok := c.store[key]
|
_, ok := c.store[key]
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Bind(i interface{}) error {
|
func (c *echoContext) Bind(i interface{}) error {
|
||||||
return c.echo.binder.Bind(i, c)
|
return c.echo.binder.Bind(i, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Render(code int, name string, data interface{}) (err error) {
|
func (c *echoContext) Render(code int, name string, data interface{}) (err error) {
|
||||||
if c.echo.renderer == nil {
|
if c.echo.renderer == nil {
|
||||||
return ErrRendererNotRegistered
|
return ErrRendererNotRegistered
|
||||||
}
|
}
|
||||||
@ -346,21 +347,21 @@ func (c *context) Render(code int, name string, data interface{}) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) HTML(code int, html string) (err error) {
|
func (c *echoContext) 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 *context) String(code int, s string) (err error) {
|
func (c *echoContext) 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 *context) JSON(code int, i interface{}) (err error) {
|
func (c *echoContext) 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, "", " ")
|
||||||
@ -371,14 +372,14 @@ func (c *context) JSON(code int, i interface{}) (err error) {
|
|||||||
return c.JSONBlob(code, b)
|
return c.JSONBlob(code, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) JSONBlob(code int, b []byte) (err error) {
|
func (c *echoContext) JSONBlob(code int, b []byte) (err error) {
|
||||||
c.response.Header().Set(HeaderContentType, MIMEApplicationJSONCharsetUTF8)
|
c.response.Header().Set(HeaderContentType, MIMEApplicationJSONCharsetUTF8)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
_, err = c.response.Write(b)
|
_, err = c.response.Write(b)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
|
func (c *echoContext) 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
|
||||||
@ -395,7 +396,7 @@ func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) XML(code int, i interface{}) (err error) {
|
func (c *echoContext) 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, "", " ")
|
||||||
@ -406,7 +407,7 @@ func (c *context) XML(code int, i interface{}) (err error) {
|
|||||||
return c.XMLBlob(code, b)
|
return c.XMLBlob(code, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) XMLBlob(code int, b []byte) (err error) {
|
func (c *echoContext) XMLBlob(code int, b []byte) (err error) {
|
||||||
c.response.Header().Set(HeaderContentType, MIMEApplicationXMLCharsetUTF8)
|
c.response.Header().Set(HeaderContentType, MIMEApplicationXMLCharsetUTF8)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
|
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
|
||||||
@ -416,7 +417,7 @@ func (c *context) XMLBlob(code int, b []byte) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) File(file string) error {
|
func (c *echoContext) File(file string) error {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
@ -437,7 +438,7 @@ func (c *context) File(file string) error {
|
|||||||
return c.ServeContent(f, fi.Name(), fi.ModTime())
|
return c.ServeContent(f, fi.Name(), fi.ModTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Attachment(r io.ReadSeeker, name string) (err error) {
|
func (c *echoContext) Attachment(r io.ReadSeeker, name string) (err error) {
|
||||||
c.response.Header().Set(HeaderContentType, ContentTypeByExtension(name))
|
c.response.Header().Set(HeaderContentType, ContentTypeByExtension(name))
|
||||||
c.response.Header().Set(HeaderContentDisposition, "attachment; filename="+name)
|
c.response.Header().Set(HeaderContentDisposition, "attachment; filename="+name)
|
||||||
c.response.WriteHeader(http.StatusOK)
|
c.response.WriteHeader(http.StatusOK)
|
||||||
@ -445,12 +446,12 @@ func (c *context) Attachment(r io.ReadSeeker, name string) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) NoContent(code int) error {
|
func (c *echoContext) NoContent(code int) error {
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Redirect(code int, url string) error {
|
func (c *echoContext) Redirect(code int, url string) error {
|
||||||
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
|
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
|
||||||
return ErrInvalidRedirectCode
|
return ErrInvalidRedirectCode
|
||||||
}
|
}
|
||||||
@ -459,27 +460,27 @@ func (c *context) Redirect(code int, url string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Error(err error) {
|
func (c *echoContext) Error(err error) {
|
||||||
c.echo.httpErrorHandler(err, c)
|
c.echo.httpErrorHandler(err, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Echo() *Echo {
|
func (c *echoContext) Echo() *Echo {
|
||||||
return c.echo
|
return c.echo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Handler() HandlerFunc {
|
func (c *echoContext) Handler() HandlerFunc {
|
||||||
return c.handler
|
return c.handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) SetHandler(h HandlerFunc) {
|
func (c *echoContext) SetHandler(h HandlerFunc) {
|
||||||
c.handler = h
|
c.handler = h
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Logger() log.Logger {
|
func (c *echoContext) Logger() log.Logger {
|
||||||
return c.echo.logger
|
return c.echo.logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) ServeContent(content io.ReadSeeker, name string, modtime time.Time) error {
|
func (c *echoContext) ServeContent(content io.ReadSeeker, name string, modtime time.Time) error {
|
||||||
req := c.Request()
|
req := c.Request()
|
||||||
res := c.Response()
|
res := c.Response()
|
||||||
|
|
||||||
@ -506,8 +507,8 @@ func ContentTypeByExtension(name string) (t string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Reset(req engine.Request, res engine.Response) {
|
func (c *echoContext) Reset(req engine.Request, res engine.Response) {
|
||||||
c.netContext = nil
|
c.context = nil
|
||||||
c.request = req
|
c.request = req
|
||||||
c.response = res
|
c.response = res
|
||||||
c.store = nil
|
c.store = nil
|
||||||
|
@ -34,7 +34,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).(*context)
|
c := e.NewContext(req, rec).(*echoContext)
|
||||||
|
|
||||||
// Request
|
// Request
|
||||||
assert.NotNil(t, c.Request())
|
assert.NotNil(t, c.Request())
|
||||||
@ -78,7 +78,7 @@ func TestContext(t *testing.T) {
|
|||||||
|
|
||||||
// JSON
|
// JSON
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
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())
|
||||||
@ -88,13 +88,13 @@ func TestContext(t *testing.T) {
|
|||||||
|
|
||||||
// JSON (error)
|
// JSON (error)
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
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).(*context)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
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) {
|
||||||
@ -105,7 +105,7 @@ func TestContext(t *testing.T) {
|
|||||||
|
|
||||||
// XML
|
// XML
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
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())
|
||||||
@ -115,13 +115,13 @@ func TestContext(t *testing.T) {
|
|||||||
|
|
||||||
// XML (error)
|
// XML (error)
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
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).(*context)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
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())
|
||||||
@ -131,7 +131,7 @@ func TestContext(t *testing.T) {
|
|||||||
|
|
||||||
// HTML
|
// HTML
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
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())
|
||||||
@ -141,7 +141,7 @@ func TestContext(t *testing.T) {
|
|||||||
|
|
||||||
// Attachment
|
// Attachment
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
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")
|
||||||
@ -154,20 +154,20 @@ func TestContext(t *testing.T) {
|
|||||||
|
|
||||||
// NoContent
|
// NoContent
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
c.NoContent(http.StatusOK)
|
c.NoContent(http.StatusOK)
|
||||||
assert.Equal(t, http.StatusOK, rec.Status())
|
assert.Equal(t, http.StatusOK, rec.Status())
|
||||||
|
|
||||||
// Redirect
|
// Redirect
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
|
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
|
||||||
assert.Equal(t, http.StatusMovedPermanently, rec.Status())
|
assert.Equal(t, http.StatusMovedPermanently, rec.Status())
|
||||||
assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(HeaderLocation))
|
assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(HeaderLocation))
|
||||||
|
|
||||||
// Error
|
// Error
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
c.Error(errors.New("error"))
|
c.Error(errors.New("error"))
|
||||||
assert.Equal(t, http.StatusInternalServerError, rec.Status())
|
assert.Equal(t, http.StatusInternalServerError, rec.Status())
|
||||||
|
|
||||||
@ -183,7 +183,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).(*context)
|
c := e.NewContext(req, rec).(*echoContext)
|
||||||
|
|
||||||
// Read single
|
// Read single
|
||||||
cookie, err := c.Cookie("theme")
|
cookie, err := c.Cookie("theme")
|
||||||
|
7
echo.go
7
echo.go
@ -48,6 +48,8 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
ncontext "golang.org/x/net/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"
|
||||||
@ -235,7 +237,8 @@ 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 &context{
|
return &echoContext{
|
||||||
|
context: ncontext.Background(),
|
||||||
request: req,
|
request: req,
|
||||||
response: res,
|
response: res,
|
||||||
echo: e,
|
echo: e,
|
||||||
@ -546,7 +549,7 @@ func (e *Echo) PutContext(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().(*context)
|
c := e.pool.Get().(*echoContext)
|
||||||
c.Reset(req, res)
|
c.Reset(req, res)
|
||||||
|
|
||||||
// Middleware
|
// Middleware
|
||||||
|
@ -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).(*context)
|
c := e.NewContext(nil, nil).(*echoContext)
|
||||||
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).(*context)
|
c := e.NewContext(nil, nil).(*echoContext)
|
||||||
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).(*context)
|
c := e.NewContext(nil, nil).(*echoContext)
|
||||||
|
|
||||||
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).(*context)
|
c := e.NewContext(nil, nil).(*echoContext)
|
||||||
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).(*context)
|
c := e.NewContext(nil, nil).(*echoContext)
|
||||||
|
|
||||||
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).(*context)
|
c := e.NewContext(nil, nil).(*echoContext)
|
||||||
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).(*context)
|
c := e.NewContext(nil, nil).(*echoContext)
|
||||||
|
|
||||||
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).(*context)
|
c := e.NewContext(nil, nil).(*echoContext)
|
||||||
|
|
||||||
// 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).(*context)
|
c = e.NewContext(nil, nil).(*echoContext)
|
||||||
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).(*context)
|
c := e.NewContext(nil, nil).(*echoContext)
|
||||||
|
|
||||||
// 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).(*context)
|
c := e.NewContext(nil, nil).(*echoContext)
|
||||||
|
|
||||||
// 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).(*context)
|
c = e.NewContext(nil, nil).(*echoContext)
|
||||||
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).(*context)
|
c := e.NewContext(nil, nil).(*echoContext)
|
||||||
|
|
||||||
// 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).(*context)
|
c := e.NewContext(nil, nil).(*echoContext)
|
||||||
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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user