mirror of
https://github.com/labstack/echo.git
synced 2024-11-24 08:22:21 +02:00
Object method to get real object
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
3f48b92c5d
commit
0731959a98
@ -47,7 +47,7 @@ type (
|
||||
Redirect(int, string) error
|
||||
Error(err error)
|
||||
Logger() logger.Logger
|
||||
Context() *context
|
||||
Object() *context
|
||||
}
|
||||
|
||||
context struct {
|
||||
@ -307,8 +307,8 @@ func (c *context) Logger() logger.Logger {
|
||||
return c.echo.logger
|
||||
}
|
||||
|
||||
// Context returns the `context` instance.
|
||||
func (c *context) Context() *context {
|
||||
// Object returns the `context` object.
|
||||
func (c *context) Object() *context {
|
||||
return c
|
||||
}
|
||||
|
||||
|
@ -51,8 +51,8 @@ func TestContext(t *testing.T) {
|
||||
assert.Nil(t, c.Socket())
|
||||
|
||||
// Param by id
|
||||
c.Context().pnames = []string{"id"}
|
||||
c.Context().pvalues = []string{"1"}
|
||||
c.Object().pnames = []string{"id"}
|
||||
c.Object().pvalues = []string{"1"}
|
||||
assert.Equal(t, "1", c.P(0))
|
||||
|
||||
// Param by name
|
||||
@ -68,13 +68,13 @@ func TestContext(t *testing.T) {
|
||||
|
||||
// JSON
|
||||
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)
|
||||
|
||||
// XML
|
||||
c.Context().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
|
||||
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
|
||||
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)
|
||||
|
||||
// Unsupported
|
||||
@ -87,14 +87,14 @@ func TestContext(t *testing.T) {
|
||||
tpl := &Template{
|
||||
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")
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Status())
|
||||
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")
|
||||
assert.Error(t, err)
|
||||
|
||||
@ -226,12 +226,12 @@ func TestContext(t *testing.T) {
|
||||
|
||||
// Error
|
||||
rec = test.NewResponseRecorder()
|
||||
c = NewContext(req, rec, e).Context()
|
||||
c = NewContext(req, rec, e).Object()
|
||||
c.Error(errors.New("error"))
|
||||
assert.Equal(t, http.StatusInternalServerError, c.Response().Status())
|
||||
|
||||
// reset
|
||||
c.Context().reset(req, test.NewResponseRecorder(), e)
|
||||
c.Object().reset(req, test.NewResponseRecorder(), e)
|
||||
}
|
||||
|
||||
func TestContextPath(t *testing.T) {
|
||||
|
@ -15,16 +15,17 @@ type (
|
||||
}
|
||||
|
||||
Request interface {
|
||||
URI() string
|
||||
URL() URL
|
||||
Header() Header
|
||||
// Proto() string
|
||||
// ProtoMajor() int
|
||||
// ProtoMinor() int
|
||||
RemoteAddress() string
|
||||
Method() string
|
||||
URI() string
|
||||
URL() URL
|
||||
Body() io.ReadCloser
|
||||
FormValue(string) string
|
||||
Object() interface{}
|
||||
}
|
||||
|
||||
Response interface {
|
||||
@ -36,6 +37,7 @@ type (
|
||||
Committed() bool
|
||||
SetWriter(io.Writer)
|
||||
Writer() io.Writer
|
||||
Object() interface{}
|
||||
}
|
||||
|
||||
Header interface {
|
||||
|
@ -9,30 +9,34 @@ import (
|
||||
|
||||
type (
|
||||
Request struct {
|
||||
context *fasthttp.RequestCtx
|
||||
request *fasthttp.RequestCtx
|
||||
url engine.URL
|
||||
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 {
|
||||
return r.header
|
||||
}
|
||||
|
||||
func (r *Request) RemoteAddress() string {
|
||||
return r.context.RemoteAddr().String()
|
||||
return r.request.RemoteAddr().String()
|
||||
}
|
||||
|
||||
func (r *Request) Method() string {
|
||||
return string(r.context.Method())
|
||||
}
|
||||
|
||||
func (r *Request) URI() string {
|
||||
return string(r.context.RequestURI())
|
||||
}
|
||||
|
||||
func (r *Request) URL() engine.URL {
|
||||
return r.url
|
||||
return string(r.request.Method())
|
||||
}
|
||||
|
||||
func (r *Request) Body() io.ReadCloser {
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
|
||||
type (
|
||||
Response struct {
|
||||
context *fasthttp.RequestCtx
|
||||
response *fasthttp.RequestCtx
|
||||
header engine.Header
|
||||
status int
|
||||
size int64
|
||||
@ -18,16 +18,20 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func (r *Response) Object() interface{} {
|
||||
return r.response
|
||||
}
|
||||
|
||||
func (r *Response) Header() engine.Header {
|
||||
return r.header
|
||||
}
|
||||
|
||||
func (r *Response) WriteHeader(code int) {
|
||||
r.context.SetStatusCode(code)
|
||||
r.response.SetStatusCode(code)
|
||||
}
|
||||
|
||||
func (r *Response) Write(b []byte) (int, error) {
|
||||
return r.context.Write(b)
|
||||
return r.response.Write(b)
|
||||
}
|
||||
|
||||
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() {
|
||||
fasthttp.ListenAndServe(s.config.Address, func(ctx *fasthttp.RequestCtx) {
|
||||
req := &Request{
|
||||
context: ctx,
|
||||
request: ctx,
|
||||
url: &URL{ctx.URI()},
|
||||
header: &RequestHeader{ctx.Request.Header},
|
||||
}
|
||||
res := &Response{
|
||||
context: ctx,
|
||||
header: &ResponseHeader{ctx.Response.Header},
|
||||
response: ctx,
|
||||
header: &ResponseHeader{ctx.Response.Header},
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func (r *Request) Header() engine.Header {
|
||||
return r.header
|
||||
}
|
||||
|
||||
func (r *Request) URL() engine.URL {
|
||||
return r.url
|
||||
}
|
||||
|
||||
func (r *Request) Header() engine.Header {
|
||||
return r.header
|
||||
}
|
||||
|
||||
func (r *Request) RemoteAddress() string {
|
||||
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 {
|
||||
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) {
|
||||
x := context.Context()
|
||||
x := context.Object()
|
||||
h = notFoundHandler
|
||||
e = r.echo
|
||||
cn := r.tree // Current node as root
|
||||
|
@ -529,16 +529,16 @@ func TestRouterParamNames(t *testing.T) {
|
||||
// Route > /users/:id
|
||||
h, _ = r.Find(GET, "/users/1", c)
|
||||
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))
|
||||
}
|
||||
|
||||
// Route > /users/:uid/files/:fid
|
||||
h, _ = r.Find(GET, "/users/1/files/1", c)
|
||||
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, "fid", c.Context().pnames[1])
|
||||
assert.Equal(t, "fid", c.Object().pnames[1])
|
||||
assert.Equal(t, "1", c.P(1))
|
||||
}
|
||||
}
|
||||
@ -556,7 +556,7 @@ func TestRouterAPI(t *testing.T) {
|
||||
for _, route := range api {
|
||||
h, _ := r.Find(route.Method, route.Path, c)
|
||||
if assert.NotNil(t, h) {
|
||||
for i, n := range c.Context().pnames {
|
||||
for i, n := range c.Object().pnames {
|
||||
if assert.NotEmpty(t, n) {
|
||||
assert.Equal(t, ":"+n, c.P(i))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user