mirror of
https://github.com/labstack/echo.git
synced 2024-12-22 20:06:21 +02:00
parent
9c0e9d689f
commit
1924528614
155
context.go
155
context.go
@ -17,20 +17,18 @@ import (
|
||||
|
||||
"bytes"
|
||||
|
||||
"github.com/labstack/echo/context"
|
||||
gcontext "github.com/labstack/echo/context"
|
||||
)
|
||||
|
||||
type (
|
||||
// Context represents the context of the current HTTP request. It holds request and
|
||||
// response objects, path, path parameters, data and registered handler.
|
||||
Context interface {
|
||||
context.Context
|
||||
|
||||
// StdContext returns `context.Context`.
|
||||
StdContext() context.Context
|
||||
StdContext() gcontext.Context
|
||||
|
||||
// SetStdContext sets `context.Context`.
|
||||
SetStdContext(context.Context)
|
||||
SetStdContext(gcontext.Context)
|
||||
|
||||
// Request returns `engine.Request` interface.
|
||||
Request() engine.Request
|
||||
@ -187,63 +185,50 @@ type (
|
||||
Reset(engine.Request, engine.Response)
|
||||
}
|
||||
|
||||
echoContext struct {
|
||||
context context.Context
|
||||
request engine.Request
|
||||
response engine.Response
|
||||
path string
|
||||
pnames []string
|
||||
pvalues []string
|
||||
handler HandlerFunc
|
||||
echo *Echo
|
||||
context struct {
|
||||
stdContext gcontext.Context
|
||||
request engine.Request
|
||||
response engine.Response
|
||||
path string
|
||||
pnames []string
|
||||
pvalues []string
|
||||
handler HandlerFunc
|
||||
store store
|
||||
echo *Echo
|
||||
}
|
||||
|
||||
store map[string]interface{}
|
||||
)
|
||||
|
||||
const (
|
||||
indexPage = "index.html"
|
||||
)
|
||||
|
||||
func (c *echoContext) StdContext() context.Context {
|
||||
return c.context
|
||||
func (c *context) StdContext() gcontext.Context {
|
||||
return c.stdContext
|
||||
}
|
||||
|
||||
func (c *echoContext) SetStdContext(ctx context.Context) {
|
||||
c.context = ctx
|
||||
func (c *context) SetStdContext(ctx gcontext.Context) {
|
||||
c.stdContext = ctx
|
||||
}
|
||||
|
||||
func (c *echoContext) Deadline() (deadline time.Time, ok bool) {
|
||||
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 {
|
||||
func (c *context) Request() engine.Request {
|
||||
return c.request
|
||||
}
|
||||
|
||||
func (c *echoContext) Response() engine.Response {
|
||||
func (c *context) Response() engine.Response {
|
||||
return c.response
|
||||
}
|
||||
|
||||
func (c *echoContext) Path() string {
|
||||
func (c *context) Path() string {
|
||||
return c.path
|
||||
}
|
||||
|
||||
func (c *echoContext) SetPath(p string) {
|
||||
func (c *context) SetPath(p string) {
|
||||
c.path = p
|
||||
}
|
||||
|
||||
func (c *echoContext) P(i int) (value string) {
|
||||
func (c *context) P(i int) (value string) {
|
||||
l := len(c.pnames)
|
||||
if i < l {
|
||||
value = c.pvalues[i]
|
||||
@ -251,7 +236,7 @@ func (c *echoContext) P(i int) (value string) {
|
||||
return
|
||||
}
|
||||
|
||||
func (c *echoContext) Param(name string) (value string) {
|
||||
func (c *context) Param(name string) (value string) {
|
||||
l := len(c.pnames)
|
||||
for i, n := range c.pnames {
|
||||
if n == name && i < l {
|
||||
@ -262,71 +247,74 @@ func (c *echoContext) Param(name string) (value string) {
|
||||
return
|
||||
}
|
||||
|
||||
func (c *echoContext) ParamNames() []string {
|
||||
func (c *context) ParamNames() []string {
|
||||
return c.pnames
|
||||
}
|
||||
|
||||
func (c *echoContext) SetParamNames(names ...string) {
|
||||
func (c *context) SetParamNames(names ...string) {
|
||||
c.pnames = names
|
||||
}
|
||||
|
||||
func (c *echoContext) ParamValues() []string {
|
||||
func (c *context) ParamValues() []string {
|
||||
return c.pvalues
|
||||
}
|
||||
|
||||
func (c *echoContext) SetParamValues(values ...string) {
|
||||
func (c *context) SetParamValues(values ...string) {
|
||||
c.pvalues = values
|
||||
}
|
||||
|
||||
func (c *echoContext) QueryParam(name string) string {
|
||||
func (c *context) QueryParam(name string) string {
|
||||
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()
|
||||
}
|
||||
|
||||
func (c *echoContext) FormValue(name string) string {
|
||||
func (c *context) FormValue(name string) string {
|
||||
return c.request.FormValue(name)
|
||||
}
|
||||
|
||||
func (c *echoContext) FormParams() map[string][]string {
|
||||
func (c *context) FormParams() map[string][]string {
|
||||
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)
|
||||
}
|
||||
|
||||
func (c *echoContext) MultipartForm() (*multipart.Form, error) {
|
||||
func (c *context) MultipartForm() (*multipart.Form, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
func (c *echoContext) SetCookie(cookie engine.Cookie) {
|
||||
func (c *context) SetCookie(cookie engine.Cookie) {
|
||||
c.response.SetCookie(cookie)
|
||||
}
|
||||
|
||||
func (c *echoContext) Cookies() []engine.Cookie {
|
||||
func (c *context) Cookies() []engine.Cookie {
|
||||
return c.request.Cookies()
|
||||
}
|
||||
|
||||
func (c *echoContext) Set(key string, val interface{}) {
|
||||
c.context = context.WithValue(c.context, key, val)
|
||||
func (c *context) Set(key string, val interface{}) {
|
||||
if c.store == nil {
|
||||
c.store = make(store)
|
||||
}
|
||||
c.store[key] = val
|
||||
}
|
||||
|
||||
func (c *echoContext) Get(key string) interface{} {
|
||||
return c.context.Value(key)
|
||||
func (c *context) Get(key string) interface{} {
|
||||
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)
|
||||
}
|
||||
|
||||
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 {
|
||||
return ErrRendererNotRegistered
|
||||
}
|
||||
@ -340,21 +328,21 @@ func (c *echoContext) Render(code int, name string, data interface{}) (err error
|
||||
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.WriteHeader(code)
|
||||
_, err = c.response.Write([]byte(html))
|
||||
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.WriteHeader(code)
|
||||
_, err = c.response.Write([]byte(s))
|
||||
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)
|
||||
if c.echo.Debug() {
|
||||
b, err = json.MarshalIndent(i, "", " ")
|
||||
@ -365,11 +353,11 @@ func (c *echoContext) JSON(code int, i interface{}) (err error) {
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -377,7 +365,7 @@ func (c *echoContext) JSONP(code int, callback string, i interface{}) (err error
|
||||
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.WriteHeader(code)
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
if c.echo.Debug() {
|
||||
b, err = xml.MarshalIndent(i, "", " ")
|
||||
@ -401,28 +389,28 @@ func (c *echoContext) XML(code int, i interface{}) (err error) {
|
||||
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 {
|
||||
return
|
||||
}
|
||||
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.WriteHeader(code)
|
||||
_, err = c.response.Write(b)
|
||||
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.WriteHeader(code)
|
||||
_, err = io.Copy(c.response, r)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *echoContext) File(file string) error {
|
||||
func (c *context) File(file string) error {
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
return ErrNotFound
|
||||
@ -443,15 +431,15 @@ func (c *echoContext) File(file string) error {
|
||||
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")
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
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(HeaderContentDisposition, fmt.Sprintf("%s; filename=%s", dispositionType, name))
|
||||
c.response.WriteHeader(http.StatusOK)
|
||||
@ -459,12 +447,12 @@ func (c *echoContext) contentDisposition(r io.ReadSeeker, name, dispositionType
|
||||
return
|
||||
}
|
||||
|
||||
func (c *echoContext) NoContent(code int) error {
|
||||
func (c *context) NoContent(code int) error {
|
||||
c.response.WriteHeader(code)
|
||||
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 {
|
||||
return ErrInvalidRedirectCode
|
||||
}
|
||||
@ -473,27 +461,27 @@ func (c *echoContext) Redirect(code int, url string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *echoContext) Error(err error) {
|
||||
func (c *context) Error(err error) {
|
||||
c.echo.httpErrorHandler(err, c)
|
||||
}
|
||||
|
||||
func (c *echoContext) Echo() *Echo {
|
||||
func (c *context) Echo() *Echo {
|
||||
return c.echo
|
||||
}
|
||||
|
||||
func (c *echoContext) Handler() HandlerFunc {
|
||||
func (c *context) Handler() HandlerFunc {
|
||||
return c.handler
|
||||
}
|
||||
|
||||
func (c *echoContext) SetHandler(h HandlerFunc) {
|
||||
func (c *context) SetHandler(h HandlerFunc) {
|
||||
c.handler = h
|
||||
}
|
||||
|
||||
func (c *echoContext) Logger() log.Logger {
|
||||
func (c *context) Logger() log.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()
|
||||
res := c.Response()
|
||||
|
||||
@ -520,9 +508,10 @@ func ContentTypeByExtension(name string) (t string) {
|
||||
return
|
||||
}
|
||||
|
||||
func (c *echoContext) Reset(req engine.Request, res engine.Response) {
|
||||
c.context = context.Background()
|
||||
func (c *context) Reset(req engine.Request, res engine.Response) {
|
||||
c.stdContext = gcontext.Background()
|
||||
c.request = req
|
||||
c.response = res
|
||||
c.store = nil
|
||||
c.handler = NotFoundHandler
|
||||
}
|
||||
|
@ -11,10 +11,10 @@ import (
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"strings"
|
||||
|
||||
gcontext "github.com/labstack/echo/context"
|
||||
|
||||
"net/url"
|
||||
|
||||
"encoding/xml"
|
||||
@ -37,7 +37,7 @@ func TestContext(t *testing.T) {
|
||||
e := New()
|
||||
req := test.NewRequest(POST, "/", strings.NewReader(userJSON))
|
||||
rec := test.NewResponseRecorder()
|
||||
c := e.NewContext(req, rec).(*echoContext)
|
||||
c := e.NewContext(req, rec).(*context)
|
||||
|
||||
// Echo
|
||||
assert.Equal(t, e, c.Echo())
|
||||
@ -71,7 +71,7 @@ func TestContext(t *testing.T) {
|
||||
|
||||
// JSON
|
||||
rec = test.NewResponseRecorder()
|
||||
c = e.NewContext(req, rec).(*echoContext)
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Status())
|
||||
@ -81,13 +81,13 @@ func TestContext(t *testing.T) {
|
||||
|
||||
// JSON (error)
|
||||
rec = test.NewResponseRecorder()
|
||||
c = e.NewContext(req, rec).(*echoContext)
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
err = c.JSON(http.StatusOK, make(chan bool))
|
||||
assert.Error(t, err)
|
||||
|
||||
// JSONP
|
||||
rec = test.NewResponseRecorder()
|
||||
c = e.NewContext(req, rec).(*echoContext)
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
callback := "callback"
|
||||
err = c.JSONP(http.StatusOK, callback, user{1, "Jon Snow"})
|
||||
if assert.NoError(t, err) {
|
||||
@ -98,7 +98,7 @@ func TestContext(t *testing.T) {
|
||||
|
||||
// XML
|
||||
rec = test.NewResponseRecorder()
|
||||
c = e.NewContext(req, rec).(*echoContext)
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
err = c.XML(http.StatusOK, user{1, "Jon Snow"})
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Status())
|
||||
@ -108,13 +108,13 @@ func TestContext(t *testing.T) {
|
||||
|
||||
// XML (error)
|
||||
rec = test.NewResponseRecorder()
|
||||
c = e.NewContext(req, rec).(*echoContext)
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
err = c.XML(http.StatusOK, make(chan bool))
|
||||
assert.Error(t, err)
|
||||
|
||||
// String
|
||||
rec = test.NewResponseRecorder()
|
||||
c = e.NewContext(req, rec).(*echoContext)
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
err = c.String(http.StatusOK, "Hello, World!")
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Status())
|
||||
@ -124,7 +124,7 @@ func TestContext(t *testing.T) {
|
||||
|
||||
// HTML
|
||||
rec = test.NewResponseRecorder()
|
||||
c = e.NewContext(req, rec).(*echoContext)
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Status())
|
||||
@ -134,7 +134,7 @@ func TestContext(t *testing.T) {
|
||||
|
||||
// Stream
|
||||
rec = test.NewResponseRecorder()
|
||||
c = e.NewContext(req, rec).(*echoContext)
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
r := strings.NewReader("response from a stream")
|
||||
err = c.Stream(http.StatusOK, "application/octet-stream", r)
|
||||
if assert.NoError(t, err) {
|
||||
@ -145,7 +145,7 @@ func TestContext(t *testing.T) {
|
||||
|
||||
// Attachment
|
||||
rec = test.NewResponseRecorder()
|
||||
c = e.NewContext(req, rec).(*echoContext)
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
file, err := os.Open("_fixture/images/walle.png")
|
||||
if assert.NoError(t, err) {
|
||||
err = c.Attachment(file, "walle.png")
|
||||
@ -158,7 +158,7 @@ func TestContext(t *testing.T) {
|
||||
|
||||
// Inline
|
||||
rec = test.NewResponseRecorder()
|
||||
c = e.NewContext(req, rec).(*echoContext)
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
file, err = os.Open("_fixture/images/walle.png")
|
||||
if assert.NoError(t, err) {
|
||||
err = c.Inline(file, "walle.png")
|
||||
@ -171,13 +171,13 @@ func TestContext(t *testing.T) {
|
||||
|
||||
// NoContent
|
||||
rec = test.NewResponseRecorder()
|
||||
c = e.NewContext(req, rec).(*echoContext)
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
c.NoContent(http.StatusOK)
|
||||
assert.Equal(t, http.StatusOK, rec.Status())
|
||||
|
||||
// Error
|
||||
rec = test.NewResponseRecorder()
|
||||
c = e.NewContext(req, rec).(*echoContext)
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
c.Error(errors.New("error"))
|
||||
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, user)
|
||||
rec := test.NewResponseRecorder()
|
||||
c := e.NewContext(req, rec).(*echoContext)
|
||||
c := e.NewContext(req, rec).(*context)
|
||||
|
||||
// Read single
|
||||
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"))
|
||||
}
|
||||
|
||||
func TestContextEmbedded(t *testing.T) {
|
||||
var c Context
|
||||
c = new(echoContext)
|
||||
c.SetStdContext(context.WithValue(c, "key", "val"))
|
||||
assert.Equal(t, "val", c.Value("key"))
|
||||
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 TestStdContextEmbedded(t *testing.T) {
|
||||
c := new(context)
|
||||
sc := gcontext.WithValue(nil, "key", "val")
|
||||
c.SetStdContext(sc)
|
||||
assert.NotEqual(t, c, c.StdContext())
|
||||
}
|
||||
|
||||
func TestContextStore(t *testing.T) {
|
||||
var c Context
|
||||
c = new(echoContext)
|
||||
c = new(context)
|
||||
c.Set("name", "Jon Snow")
|
||||
assert.Equal(t, "Jon Snow", c.Get("name"))
|
||||
}
|
||||
|
20
echo.go
20
echo.go
@ -48,8 +48,7 @@ import (
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
gcontext "github.com/labstack/echo/context"
|
||||
"github.com/labstack/echo/engine"
|
||||
"github.com/labstack/echo/log"
|
||||
glog "github.com/labstack/gommon/log"
|
||||
@ -238,13 +237,14 @@ func New() (e *Echo) {
|
||||
|
||||
// NewContext returns a Context instance.
|
||||
func (e *Echo) NewContext(req engine.Request, res engine.Response) Context {
|
||||
return &echoContext{
|
||||
context: context.Background(),
|
||||
request: req,
|
||||
response: res,
|
||||
echo: e,
|
||||
pvalues: make([]string, *e.maxParam),
|
||||
handler: NotFoundHandler,
|
||||
return &context{
|
||||
stdContext: gcontext.Background(),
|
||||
request: req,
|
||||
response: res,
|
||||
store: make(store),
|
||||
echo: e,
|
||||
pvalues: make([]string, *e.maxParam),
|
||||
handler: NotFoundHandler,
|
||||
}
|
||||
}
|
||||
|
||||
@ -541,7 +541,7 @@ func (e *Echo) ReleaseContext(c Context) {
|
||||
}
|
||||
|
||||
func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
|
||||
c := e.pool.Get().(*echoContext)
|
||||
c := e.pool.Get().(*context)
|
||||
c.Reset(req, res)
|
||||
|
||||
// Middleware
|
||||
|
@ -333,7 +333,7 @@ func TestEchoHTTPError(t *testing.T) {
|
||||
func TestEchoContext(t *testing.T) {
|
||||
e := New()
|
||||
c := e.AcquireContext()
|
||||
assert.IsType(t, new(echoContext), c)
|
||||
assert.IsType(t, new(context), c)
|
||||
e.ReleaseContext(c)
|
||||
}
|
||||
|
||||
|
@ -281,7 +281,7 @@ func TestRouterStatic(t *testing.T) {
|
||||
c.Set("path", path)
|
||||
return nil
|
||||
}, e)
|
||||
c := e.NewContext(nil, nil).(*echoContext)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
r.Find(GET, path, c)
|
||||
c.handler(c)
|
||||
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 {
|
||||
return nil
|
||||
}, e)
|
||||
c := e.NewContext(nil, nil).(*echoContext)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
r.Find(GET, "/users/1", c)
|
||||
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 {
|
||||
return nil
|
||||
}, e)
|
||||
c := e.NewContext(nil, nil).(*echoContext)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
|
||||
r.Find(GET, "/users/1/files/1", c)
|
||||
assert.Equal(t, "1", c.P(0))
|
||||
@ -324,7 +324,7 @@ func TestRouterParamWithSlash(t *testing.T) {
|
||||
return nil
|
||||
}, e)
|
||||
|
||||
c := e.NewContext(nil, nil).(*echoContext)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
assert.NotPanics(t, func() {
|
||||
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 {
|
||||
return nil
|
||||
}, e)
|
||||
c := e.NewContext(nil, nil).(*echoContext)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
|
||||
r.Find(GET, "/", c)
|
||||
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 {
|
||||
return nil
|
||||
}, e)
|
||||
c := e.NewContext(nil, nil).(*echoContext)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
r.Find(GET, "/1/2/3", c)
|
||||
assert.Equal(t, "1", c.P(0))
|
||||
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 {
|
||||
return nil
|
||||
}, e)
|
||||
c := e.NewContext(nil, nil).(*echoContext)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
|
||||
r.Find(GET, "/users/joe/comments", c)
|
||||
c.handler(c)
|
||||
@ -396,7 +396,7 @@ func TestRouterMultiRoute(t *testing.T) {
|
||||
r.Add(GET, "/users/:id", func(c Context) error {
|
||||
return nil
|
||||
}, e)
|
||||
c := e.NewContext(nil, nil).(*echoContext)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
|
||||
// Route > /users
|
||||
r.Find(GET, "/users", c)
|
||||
@ -408,7 +408,7 @@ func TestRouterMultiRoute(t *testing.T) {
|
||||
assert.Equal(t, "1", c.P(0))
|
||||
|
||||
// Route > /user
|
||||
c = e.NewContext(nil, nil).(*echoContext)
|
||||
c = e.NewContext(nil, nil).(*context)
|
||||
r.Find(GET, "/user", c)
|
||||
he := c.handler(c).(*HTTPError)
|
||||
assert.Equal(t, http.StatusNotFound, he.Code)
|
||||
@ -447,7 +447,7 @@ func TestRouterPriority(t *testing.T) {
|
||||
c.Set("g", 7)
|
||||
return nil
|
||||
}, e)
|
||||
c := e.NewContext(nil, nil).(*echoContext)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
|
||||
// Route > /users
|
||||
r.Find(GET, "/users", c)
|
||||
@ -490,7 +490,7 @@ func TestRouterPriority(t *testing.T) {
|
||||
func TestRouterPriorityNotFound(t *testing.T) {
|
||||
e := New()
|
||||
r := e.router
|
||||
c := e.NewContext(nil, nil).(*echoContext)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
|
||||
// Add
|
||||
r.Add(GET, "/a/foo", func(c Context) error {
|
||||
@ -511,7 +511,7 @@ func TestRouterPriorityNotFound(t *testing.T) {
|
||||
c.handler(c)
|
||||
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)
|
||||
he := c.handler(c).(*HTTPError)
|
||||
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 {
|
||||
return nil
|
||||
}, e)
|
||||
c := e.NewContext(nil, nil).(*echoContext)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
|
||||
// Route > /users
|
||||
r.Find(GET, "/users", c)
|
||||
@ -561,7 +561,7 @@ func TestRouterAPI(t *testing.T) {
|
||||
return nil
|
||||
}, e)
|
||||
}
|
||||
c := e.NewContext(nil, nil).(*echoContext)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
for _, route := range api {
|
||||
r.Find(route.Method, route.Path, c)
|
||||
for i, n := range c.pnames {
|
||||
|
Loading…
Reference in New Issue
Block a user