1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Fixed #430, Closes #469

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-04-16 09:15:37 -07:00
parent 288164d00a
commit fd104333f2
8 changed files with 70 additions and 15 deletions

View File

@ -14,8 +14,6 @@ import (
"github.com/labstack/echo/engine"
"github.com/labstack/gommon/log"
"net/url"
"bytes"
netContext "golang.org/x/net/context"
@ -138,7 +136,7 @@ type (
// and `Last-Modified` response headers.
ServeContent(io.ReadSeeker, string, time.Time) error
// Object returns the `context` instance.
// Object returns the internal context implementation.
Object() *context
// Reset resets the context after request completes. It must be called along
@ -153,7 +151,6 @@ type (
path string
pnames []string
pvalues []string
query url.Values
store store
handler HandlerFunc
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 {
return c.netContext
}
@ -454,7 +465,6 @@ func (c *context) Reset(rq engine.Request, rs engine.Response) {
c.netContext = nil
c.request = rq
c.response = rs
c.query = nil
c.store = nil
c.handler = notFoundHandler
}

View File

@ -78,7 +78,7 @@ type (
Handler string
}
// HTTPError represents an error that occured while handling a request.
// HTTPError represents an error that occurred while handling a request.
HTTPError struct {
Code int
Message string

View File

@ -6,10 +6,9 @@ import (
"bytes"
"io"
"mime/multipart"
)
import (
"github.com/labstack/echo/engine"
"github.com/labstack/gommon/log"
"github.com/valyala/fasthttp"
)
@ -19,9 +18,21 @@ type (
*fasthttp.RequestCtx
url engine.URL
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.
func (r *Request) IsTLS() bool {
return r.IsTLS()

View File

@ -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.
func (r *Response) Header() engine.Header {
return r.header

View File

@ -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 {
c := engine.Config{Address: addr}
return WithConfig(c)
}
// WithTLS returns `fasthttp.Server` with TLS config.
// WithTLS returns `Server` with provided TLS config.
func WithTLS(addr, certfile, keyfile string) *Server {
c := engine.Config{
Address: addr,
@ -46,7 +46,7 @@ func WithTLS(addr, certfile, keyfile string) *Server {
return WithConfig(c)
}
// WithConfig returns `standard.Server` with config.
// WithConfig returns `Server` with provided config.
func WithConfig(c engine.Config) (s *Server) {
s = &Server{
Server: new(fasthttp.Server),
@ -54,7 +54,7 @@ func WithConfig(c engine.Config) (s *Server) {
pool: &pool{
request: sync.Pool{
New: func() interface{} {
return &Request{}
return &Request{logger: s.logger}
},
},
response: sync.Pool{

View File

@ -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.
func (r *Request) IsTLS() bool {
return r.Request.TLS != nil

View File

@ -5,6 +5,7 @@ import (
"io"
"net"
"net/http"
"net/http/httptest"
"github.com/labstack/echo/engine"
"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.
func (r *Response) Header() engine.Header {
return r.header

View File

@ -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 {
c := engine.Config{Address: addr}
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 {
c := engine.Config{
Address: addr,
@ -44,7 +44,7 @@ func WithTLS(addr, certfile, keyfile string) *Server {
return WithConfig(c)
}
// WithConfig returns `standard.Server` with config.
// WithConfig returns `Server` instance with provided config.
func WithConfig(c engine.Config) (s *Server) {
s = &Server{
Server: new(http.Server),