mirror of
https://github.com/labstack/echo.git
synced 2025-07-13 01:30:31 +02:00
Object method to get real object
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
@ -47,7 +47,7 @@ type (
|
|||||||
Redirect(int, string) error
|
Redirect(int, string) error
|
||||||
Error(err error)
|
Error(err error)
|
||||||
Logger() logger.Logger
|
Logger() logger.Logger
|
||||||
Context() *context
|
Object() *context
|
||||||
}
|
}
|
||||||
|
|
||||||
context struct {
|
context struct {
|
||||||
@ -307,8 +307,8 @@ func (c *context) Logger() logger.Logger {
|
|||||||
return c.echo.logger
|
return c.echo.logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// Context returns the `context` instance.
|
// Object returns the `context` object.
|
||||||
func (c *context) Context() *context {
|
func (c *context) Object() *context {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,8 +51,8 @@ func TestContext(t *testing.T) {
|
|||||||
assert.Nil(t, c.Socket())
|
assert.Nil(t, c.Socket())
|
||||||
|
|
||||||
// Param by id
|
// Param by id
|
||||||
c.Context().pnames = []string{"id"}
|
c.Object().pnames = []string{"id"}
|
||||||
c.Context().pvalues = []string{"1"}
|
c.Object().pvalues = []string{"1"}
|
||||||
assert.Equal(t, "1", c.P(0))
|
assert.Equal(t, "1", c.P(0))
|
||||||
|
|
||||||
// Param by name
|
// Param by name
|
||||||
@ -68,13 +68,13 @@ func TestContext(t *testing.T) {
|
|||||||
|
|
||||||
// JSON
|
// JSON
|
||||||
testBindOk(t, c, ApplicationJSON)
|
testBindOk(t, c, ApplicationJSON)
|
||||||
c.Context().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
|
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
|
||||||
testBindError(t, c, ApplicationJSON)
|
testBindError(t, c, ApplicationJSON)
|
||||||
|
|
||||||
// XML
|
// XML
|
||||||
c.Context().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
|
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
|
||||||
testBindOk(t, c, ApplicationXML)
|
testBindOk(t, c, ApplicationXML)
|
||||||
c.Context().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
|
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
|
||||||
testBindError(t, c, ApplicationXML)
|
testBindError(t, c, ApplicationXML)
|
||||||
|
|
||||||
// Unsupported
|
// Unsupported
|
||||||
@ -87,14 +87,14 @@ func TestContext(t *testing.T) {
|
|||||||
tpl := &Template{
|
tpl := &Template{
|
||||||
templates: template.Must(template.New("hello").Parse("Hello, {{.}}!")),
|
templates: template.Must(template.New("hello").Parse("Hello, {{.}}!")),
|
||||||
}
|
}
|
||||||
c.Context().echo.SetRenderer(tpl)
|
c.Object().echo.SetRenderer(tpl)
|
||||||
err := c.Render(http.StatusOK, "hello", "Joe")
|
err := c.Render(http.StatusOK, "hello", "Joe")
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, http.StatusOK, rec.Status())
|
assert.Equal(t, http.StatusOK, rec.Status())
|
||||||
assert.Equal(t, "Hello, Joe!", rec.Body.String())
|
assert.Equal(t, "Hello, Joe!", rec.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Context().echo.renderer = nil
|
c.Object().echo.renderer = nil
|
||||||
err = c.Render(http.StatusOK, "hello", "Joe")
|
err = c.Render(http.StatusOK, "hello", "Joe")
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
@ -226,12 +226,12 @@ func TestContext(t *testing.T) {
|
|||||||
|
|
||||||
// Error
|
// Error
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = NewContext(req, rec, e).Context()
|
c = NewContext(req, rec, e).Object()
|
||||||
c.Error(errors.New("error"))
|
c.Error(errors.New("error"))
|
||||||
assert.Equal(t, http.StatusInternalServerError, c.Response().Status())
|
assert.Equal(t, http.StatusInternalServerError, c.Response().Status())
|
||||||
|
|
||||||
// reset
|
// reset
|
||||||
c.Context().reset(req, test.NewResponseRecorder(), e)
|
c.Object().reset(req, test.NewResponseRecorder(), e)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextPath(t *testing.T) {
|
func TestContextPath(t *testing.T) {
|
||||||
|
@ -15,16 +15,17 @@ type (
|
|||||||
}
|
}
|
||||||
|
|
||||||
Request interface {
|
Request interface {
|
||||||
|
URI() string
|
||||||
|
URL() URL
|
||||||
Header() Header
|
Header() Header
|
||||||
// Proto() string
|
// Proto() string
|
||||||
// ProtoMajor() int
|
// ProtoMajor() int
|
||||||
// ProtoMinor() int
|
// ProtoMinor() int
|
||||||
RemoteAddress() string
|
RemoteAddress() string
|
||||||
Method() string
|
Method() string
|
||||||
URI() string
|
|
||||||
URL() URL
|
|
||||||
Body() io.ReadCloser
|
Body() io.ReadCloser
|
||||||
FormValue(string) string
|
FormValue(string) string
|
||||||
|
Object() interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
Response interface {
|
Response interface {
|
||||||
@ -36,6 +37,7 @@ type (
|
|||||||
Committed() bool
|
Committed() bool
|
||||||
SetWriter(io.Writer)
|
SetWriter(io.Writer)
|
||||||
Writer() io.Writer
|
Writer() io.Writer
|
||||||
|
Object() interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
Header interface {
|
Header interface {
|
||||||
|
@ -9,30 +9,34 @@ import (
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
Request struct {
|
Request struct {
|
||||||
context *fasthttp.RequestCtx
|
request *fasthttp.RequestCtx
|
||||||
url engine.URL
|
url engine.URL
|
||||||
header engine.Header
|
header engine.Header
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (r *Request) Object() interface{} {
|
||||||
|
return r.request
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Request) URI() string {
|
||||||
|
return string(r.request.RequestURI())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Request) URL() engine.URL {
|
||||||
|
return r.url
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Request) Header() engine.Header {
|
func (r *Request) Header() engine.Header {
|
||||||
return r.header
|
return r.header
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) RemoteAddress() string {
|
func (r *Request) RemoteAddress() string {
|
||||||
return r.context.RemoteAddr().String()
|
return r.request.RemoteAddr().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Method() string {
|
func (r *Request) Method() string {
|
||||||
return string(r.context.Method())
|
return string(r.request.Method())
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Request) URI() string {
|
|
||||||
return string(r.context.RequestURI())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Request) URL() engine.URL {
|
|
||||||
return r.url
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Body() io.ReadCloser {
|
func (r *Request) Body() io.ReadCloser {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
Response struct {
|
Response struct {
|
||||||
context *fasthttp.RequestCtx
|
response *fasthttp.RequestCtx
|
||||||
header engine.Header
|
header engine.Header
|
||||||
status int
|
status int
|
||||||
size int64
|
size int64
|
||||||
@ -18,16 +18,20 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (r *Response) Object() interface{} {
|
||||||
|
return r.response
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Response) Header() engine.Header {
|
func (r *Response) Header() engine.Header {
|
||||||
return r.header
|
return r.header
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) WriteHeader(code int) {
|
func (r *Response) WriteHeader(code int) {
|
||||||
r.context.SetStatusCode(code)
|
r.response.SetStatusCode(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) Write(b []byte) (int, error) {
|
func (r *Response) Write(b []byte) (int, error) {
|
||||||
return r.context.Write(b)
|
return r.response.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) Status() int {
|
func (r *Response) Status() int {
|
||||||
|
@ -29,13 +29,13 @@ func NewServer(c *engine.Config, h engine.HandlerFunc, l logger.Logger) *Server
|
|||||||
func (s *Server) Start() {
|
func (s *Server) Start() {
|
||||||
fasthttp.ListenAndServe(s.config.Address, func(ctx *fasthttp.RequestCtx) {
|
fasthttp.ListenAndServe(s.config.Address, func(ctx *fasthttp.RequestCtx) {
|
||||||
req := &Request{
|
req := &Request{
|
||||||
context: ctx,
|
request: ctx,
|
||||||
url: &URL{ctx.URI()},
|
url: &URL{ctx.URI()},
|
||||||
header: &RequestHeader{ctx.Request.Header},
|
header: &RequestHeader{ctx.Request.Header},
|
||||||
}
|
}
|
||||||
res := &Response{
|
res := &Response{
|
||||||
context: ctx,
|
response: ctx,
|
||||||
header: &ResponseHeader{ctx.Response.Header},
|
header: &ResponseHeader{ctx.Response.Header},
|
||||||
}
|
}
|
||||||
s.handler(req, res)
|
s.handler(req, res)
|
||||||
})
|
})
|
||||||
|
@ -23,18 +23,18 @@ func NewRequest(r *http.Request) *Request {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Request() *http.Request {
|
func (r *Request) Object() interface{} {
|
||||||
return r.request
|
return r.request
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Header() engine.Header {
|
|
||||||
return r.header
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Request) URL() engine.URL {
|
func (r *Request) URL() engine.URL {
|
||||||
return r.url
|
return r.url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Request) Header() engine.Header {
|
||||||
|
return r.header
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Request) RemoteAddress() string {
|
func (r *Request) RemoteAddress() string {
|
||||||
return r.request.RemoteAddr
|
return r.request.RemoteAddr
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,10 @@ func NewResponse(w http.ResponseWriter, l logger.Logger) *Response {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Response) Object() interface{} {
|
||||||
|
return r.response
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Response) Header() engine.Header {
|
func (r *Response) Header() engine.Header {
|
||||||
return r.header
|
return r.header
|
||||||
}
|
}
|
||||||
|
@ -274,7 +274,7 @@ func (n *node) check405() HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) Find(method, path string, context Context) (h HandlerFunc, e *Echo) {
|
func (r *Router) Find(method, path string, context Context) (h HandlerFunc, e *Echo) {
|
||||||
x := context.Context()
|
x := context.Object()
|
||||||
h = notFoundHandler
|
h = notFoundHandler
|
||||||
e = r.echo
|
e = r.echo
|
||||||
cn := r.tree // Current node as root
|
cn := r.tree // Current node as root
|
||||||
|
@ -529,16 +529,16 @@ func TestRouterParamNames(t *testing.T) {
|
|||||||
// Route > /users/:id
|
// Route > /users/:id
|
||||||
h, _ = r.Find(GET, "/users/1", c)
|
h, _ = r.Find(GET, "/users/1", c)
|
||||||
if assert.NotNil(t, h) {
|
if assert.NotNil(t, h) {
|
||||||
assert.Equal(t, "id", c.Context().pnames[0])
|
assert.Equal(t, "id", c.Object().pnames[0])
|
||||||
assert.Equal(t, "1", c.P(0))
|
assert.Equal(t, "1", c.P(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Route > /users/:uid/files/:fid
|
// Route > /users/:uid/files/:fid
|
||||||
h, _ = r.Find(GET, "/users/1/files/1", c)
|
h, _ = r.Find(GET, "/users/1/files/1", c)
|
||||||
if assert.NotNil(t, h) {
|
if assert.NotNil(t, h) {
|
||||||
assert.Equal(t, "uid", c.Context().pnames[0])
|
assert.Equal(t, "uid", c.Object().pnames[0])
|
||||||
assert.Equal(t, "1", c.P(0))
|
assert.Equal(t, "1", c.P(0))
|
||||||
assert.Equal(t, "fid", c.Context().pnames[1])
|
assert.Equal(t, "fid", c.Object().pnames[1])
|
||||||
assert.Equal(t, "1", c.P(1))
|
assert.Equal(t, "1", c.P(1))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -556,7 +556,7 @@ func TestRouterAPI(t *testing.T) {
|
|||||||
for _, route := range api {
|
for _, route := range api {
|
||||||
h, _ := r.Find(route.Method, route.Path, c)
|
h, _ := r.Find(route.Method, route.Path, c)
|
||||||
if assert.NotNil(t, h) {
|
if assert.NotNil(t, h) {
|
||||||
for i, n := range c.Context().pnames {
|
for i, n := range c.Object().pnames {
|
||||||
if assert.NotEmpty(t, n) {
|
if assert.NotEmpty(t, n) {
|
||||||
assert.Equal(t, ":"+n, c.P(i))
|
assert.Equal(t, ":"+n, c.P(i))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user