mirror of
https://github.com/labstack/echo.git
synced 2025-05-29 23:17:34 +02:00
parent
288164d00a
commit
fd104333f2
20
context.go
20
context.go
@ -14,8 +14,6 @@ import (
|
|||||||
"github.com/labstack/echo/engine"
|
"github.com/labstack/echo/engine"
|
||||||
"github.com/labstack/gommon/log"
|
"github.com/labstack/gommon/log"
|
||||||
|
|
||||||
"net/url"
|
|
||||||
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
netContext "golang.org/x/net/context"
|
netContext "golang.org/x/net/context"
|
||||||
@ -138,7 +136,7 @@ type (
|
|||||||
// and `Last-Modified` response headers.
|
// and `Last-Modified` response headers.
|
||||||
ServeContent(io.ReadSeeker, string, time.Time) error
|
ServeContent(io.ReadSeeker, string, time.Time) error
|
||||||
|
|
||||||
// Object returns the `context` instance.
|
// Object returns the internal context implementation.
|
||||||
Object() *context
|
Object() *context
|
||||||
|
|
||||||
// Reset resets the context after request completes. It must be called along
|
// Reset resets the context after request completes. It must be called along
|
||||||
@ -153,7 +151,6 @@ type (
|
|||||||
path string
|
path string
|
||||||
pnames []string
|
pnames []string
|
||||||
pvalues []string
|
pvalues []string
|
||||||
query url.Values
|
|
||||||
store store
|
store store
|
||||||
handler HandlerFunc
|
handler HandlerFunc
|
||||||
echo *Echo
|
echo *Echo
|
||||||
@ -178,6 +175,20 @@ func NewContext(rq engine.Request, rs engine.Response, e *Echo) Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MockContext returns `Context` for testing purpose.
|
||||||
|
func MockContext(request engine.Request, response engine.Response, path string, paramNames []string, paramValues []string) Context {
|
||||||
|
return &context{
|
||||||
|
request: request,
|
||||||
|
response: response,
|
||||||
|
echo: new(Echo),
|
||||||
|
path: path,
|
||||||
|
pnames: paramNames,
|
||||||
|
pvalues: paramValues,
|
||||||
|
store: make(store),
|
||||||
|
handler: notFoundHandler,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *context) NetContext() netContext.Context {
|
func (c *context) NetContext() netContext.Context {
|
||||||
return c.netContext
|
return c.netContext
|
||||||
}
|
}
|
||||||
@ -454,7 +465,6 @@ func (c *context) Reset(rq engine.Request, rs engine.Response) {
|
|||||||
c.netContext = nil
|
c.netContext = nil
|
||||||
c.request = rq
|
c.request = rq
|
||||||
c.response = rs
|
c.response = rs
|
||||||
c.query = nil
|
|
||||||
c.store = nil
|
c.store = nil
|
||||||
c.handler = notFoundHandler
|
c.handler = notFoundHandler
|
||||||
}
|
}
|
||||||
|
2
echo.go
2
echo.go
@ -78,7 +78,7 @@ type (
|
|||||||
Handler string
|
Handler string
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTPError represents an error that occured while handling a request.
|
// HTTPError represents an error that occurred while handling a request.
|
||||||
HTTPError struct {
|
HTTPError struct {
|
||||||
Code int
|
Code int
|
||||||
Message string
|
Message string
|
||||||
|
@ -6,10 +6,9 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
)
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/labstack/echo/engine"
|
"github.com/labstack/echo/engine"
|
||||||
|
"github.com/labstack/gommon/log"
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,9 +18,21 @@ type (
|
|||||||
*fasthttp.RequestCtx
|
*fasthttp.RequestCtx
|
||||||
url engine.URL
|
url engine.URL
|
||||||
header engine.Header
|
header engine.Header
|
||||||
|
logger *log.Logger
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MockRequest returns `Request` instance for testing purpose.
|
||||||
|
func MockRequest() *Request {
|
||||||
|
ctx := new(fasthttp.RequestCtx)
|
||||||
|
return &Request{
|
||||||
|
RequestCtx: ctx,
|
||||||
|
url: &URL{URI: ctx.URI()},
|
||||||
|
header: &RequestHeader{RequestHeader: &ctx.Request.Header},
|
||||||
|
logger: log.New("test"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// IsTLS implements `engine.Request#TLS` function.
|
// IsTLS implements `engine.Request#TLS` function.
|
||||||
func (r *Request) IsTLS() bool {
|
func (r *Request) IsTLS() bool {
|
||||||
return r.IsTLS()
|
return r.IsTLS()
|
||||||
|
@ -24,6 +24,17 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MockResponse returns `Response` instance for testing purpose.
|
||||||
|
func MockResponse() *Response {
|
||||||
|
ctx := new(fasthttp.RequestCtx)
|
||||||
|
return &Response{
|
||||||
|
RequestCtx: ctx,
|
||||||
|
header: &ResponseHeader{ResponseHeader: &ctx.Response.Header},
|
||||||
|
writer: ctx,
|
||||||
|
logger: log.New("test"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Header implements `engine.Response#Header` function.
|
// Header implements `engine.Response#Header` function.
|
||||||
func (r *Response) Header() engine.Header {
|
func (r *Response) Header() engine.Header {
|
||||||
return r.header
|
return r.header
|
||||||
|
@ -30,13 +30,13 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// New returns `fasthttp.Server` with provided listen address.
|
// New returns `Server` with provided listen address.
|
||||||
func New(addr string) *Server {
|
func New(addr string) *Server {
|
||||||
c := engine.Config{Address: addr}
|
c := engine.Config{Address: addr}
|
||||||
return WithConfig(c)
|
return WithConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithTLS returns `fasthttp.Server` with TLS config.
|
// WithTLS returns `Server` with provided TLS config.
|
||||||
func WithTLS(addr, certfile, keyfile string) *Server {
|
func WithTLS(addr, certfile, keyfile string) *Server {
|
||||||
c := engine.Config{
|
c := engine.Config{
|
||||||
Address: addr,
|
Address: addr,
|
||||||
@ -46,7 +46,7 @@ func WithTLS(addr, certfile, keyfile string) *Server {
|
|||||||
return WithConfig(c)
|
return WithConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithConfig returns `standard.Server` with config.
|
// WithConfig returns `Server` with provided config.
|
||||||
func WithConfig(c engine.Config) (s *Server) {
|
func WithConfig(c engine.Config) (s *Server) {
|
||||||
s = &Server{
|
s = &Server{
|
||||||
Server: new(fasthttp.Server),
|
Server: new(fasthttp.Server),
|
||||||
@ -54,7 +54,7 @@ func WithConfig(c engine.Config) (s *Server) {
|
|||||||
pool: &pool{
|
pool: &pool{
|
||||||
request: sync.Pool{
|
request: sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return &Request{}
|
return &Request{logger: s.logger}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
response: sync.Pool{
|
response: sync.Pool{
|
||||||
|
@ -19,6 +19,17 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MockRequest returns `Request` instance for testing purpose.
|
||||||
|
func MockRequest() *Request {
|
||||||
|
rq := new(http.Request)
|
||||||
|
return &Request{
|
||||||
|
Request: new(http.Request),
|
||||||
|
url: &URL{URL: rq.URL},
|
||||||
|
header: &Header{Header: rq.Header},
|
||||||
|
logger: log.New("test"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// IsTLS implements `engine.Request#TLS` function.
|
// IsTLS implements `engine.Request#TLS` function.
|
||||||
func (r *Request) IsTLS() bool {
|
func (r *Request) IsTLS() bool {
|
||||||
return r.Request.TLS != nil
|
return r.Request.TLS != nil
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
|
||||||
"github.com/labstack/echo/engine"
|
"github.com/labstack/echo/engine"
|
||||||
"github.com/labstack/gommon/log"
|
"github.com/labstack/gommon/log"
|
||||||
@ -28,6 +29,17 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MockResponse returns `Response` instance for testing purpose.
|
||||||
|
func MockResponse() *Response {
|
||||||
|
rc := httptest.NewRecorder()
|
||||||
|
return &Response{
|
||||||
|
ResponseWriter: rc,
|
||||||
|
header: &Header{Header: rc.Header()},
|
||||||
|
writer: rc,
|
||||||
|
logger: log.New("test"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Header implements `engine.Response#Header` function.
|
// Header implements `engine.Response#Header` function.
|
||||||
func (r *Response) Header() engine.Header {
|
func (r *Response) Header() engine.Header {
|
||||||
return r.header
|
return r.header
|
||||||
|
@ -28,13 +28,13 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// New returns `standard.Server` with provided listen address.
|
// New returns `Server` instance with provided listen address.
|
||||||
func New(addr string) *Server {
|
func New(addr string) *Server {
|
||||||
c := engine.Config{Address: addr}
|
c := engine.Config{Address: addr}
|
||||||
return WithConfig(c)
|
return WithConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithTLS returns `standard.Server` with TLS config.
|
// WithTLS returns `Server` instance with provided TLS config.
|
||||||
func WithTLS(addr, certfile, keyfile string) *Server {
|
func WithTLS(addr, certfile, keyfile string) *Server {
|
||||||
c := engine.Config{
|
c := engine.Config{
|
||||||
Address: addr,
|
Address: addr,
|
||||||
@ -44,7 +44,7 @@ func WithTLS(addr, certfile, keyfile string) *Server {
|
|||||||
return WithConfig(c)
|
return WithConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithConfig returns `standard.Server` with config.
|
// WithConfig returns `Server` instance with provided config.
|
||||||
func WithConfig(c engine.Config) (s *Server) {
|
func WithConfig(c engine.Config) (s *Server) {
|
||||||
s = &Server{
|
s = &Server{
|
||||||
Server: new(http.Server),
|
Server: new(http.Server),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user