1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00

Fixed test cases

Signed-off-by: Vishal Rana <vishal.rana@verizon.com>
This commit is contained in:
Vishal Rana 2016-02-09 17:16:46 -08:00
parent 4f57582b06
commit 94e5936287
11 changed files with 101 additions and 69 deletions

View File

@ -10,7 +10,8 @@ package main
import (
"github.com/labstack/echo"
"github.com/labstack/echo/engine/fasthttp"
// "github.com/labstack/echo/engine/fasthttp"
"github.com/labstack/echo/engine/standard"
mw "github.com/labstack/echo/middleware"
)
@ -25,10 +26,10 @@ func main() {
})
// FastHTTP
e.Run(fasthttp.New(":4444", e))
// e.Run(fasthttp.New(":4444"))
// Standard
// e.Run(standard.New(":4444"))
e.Run(standard.New(":4444"))
}
```

View File

@ -8,6 +8,7 @@ import (
"time"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"net/url"
@ -45,7 +46,7 @@ type (
NoContent(int) error
Redirect(int, string) error
Error(err error)
Logger() Logger
Logger() logger.Logger
Object() *context
}
@ -302,7 +303,7 @@ func (c *context) Error(err error) {
}
// Logger returns the `Logger` instance.
func (c *context) Logger() Logger {
func (c *context) Logger() logger.Logger {
return c.echo.logger
}

31
echo.go
View File

@ -17,6 +17,7 @@ import (
"encoding/xml"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
)
@ -35,7 +36,7 @@ type (
hook engine.HandlerFunc
autoIndex bool
router *Router
logger Logger
logger logger.Logger
}
Route struct {
@ -81,24 +82,6 @@ type (
Renderer interface {
Render(w io.Writer, name string, data interface{}) error
}
// Logger is the interface that declares Echo's logging system.
Logger interface {
Debug(...interface{})
Debugf(string, ...interface{})
Info(...interface{})
Infof(string, ...interface{})
Warn(...interface{})
Warnf(string, ...interface{})
Error(...interface{})
Errorf(string, ...interface{})
Fatal(...interface{})
Fatalf(string, ...interface{})
}
)
const (
@ -242,12 +225,12 @@ func (e *Echo) Router() *Router {
}
// SetLogger sets the logger instance.
func (e *Echo) SetLogger(l Logger) {
func (e *Echo) SetLogger(l logger.Logger) {
e.logger = l
}
// Logger returns the logger instance.
func (e *Echo) Logger() Logger {
func (e *Echo) Logger() logger.Logger {
return e.logger
}
@ -560,8 +543,10 @@ func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
// }
// Run starts the HTTP engine.
func (*Echo) Run(e engine.Engine) {
e.Start()
func (e *Echo) Run(eng engine.Engine) {
eng.SetHandler(e.ServeHTTP)
eng.SetLogger(e.logger)
eng.Start()
}
func NewHTTPError(code int, msg ...string) *HTTPError {

View File

@ -308,7 +308,7 @@ func TestEchoNotFound(t *testing.T) {
e := New()
req := test.NewRequest(GET, "/files", nil)
rec := test.NewResponseRecorder()
e.Handle(req, rec)
e.ServeHTTP(req, rec)
assert.Equal(t, http.StatusNotFound, rec.Status())
}
@ -319,7 +319,7 @@ func TestEchoMethodNotAllowed(t *testing.T) {
})
req := test.NewRequest(POST, "/", nil)
rec := test.NewResponseRecorder()
e.Handle(req, rec)
e.ServeHTTP(req, rec)
assert.Equal(t, http.StatusMethodNotAllowed, rec.Status())
}
@ -350,7 +350,7 @@ func TestEchoHook(t *testing.T) {
})
req := test.NewRequest(GET, "/test/", nil)
rec := test.NewResponseRecorder()
e.Handle(req, rec)
e.ServeHTTP(req, rec)
assert.Equal(t, req.URL().Path(), "/test")
}
@ -371,6 +371,6 @@ func testMethod(t *testing.T, method, path string, e *Echo) {
func request(method, path string, e *Echo) (int, string) {
req := test.NewRequest(method, path, nil)
rec := test.NewResponseRecorder()
e.Handle(req, rec)
e.ServeHTTP(req, rec)
return rec.Status(), rec.Body.String()
}

View File

@ -3,12 +3,16 @@ package engine
import (
"io"
"time"
"github.com/labstack/echo/logger"
)
type (
HandlerFunc func(Request, Response)
Engine interface {
SetHandler(HandlerFunc)
SetLogger(logger.Logger)
Start()
}

View File

@ -5,8 +5,9 @@ package fasthttp
import (
"io"
"github.com/labstack/echo"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
"github.com/valyala/fasthttp"
)
@ -18,16 +19,16 @@ type (
size int64
committed bool
writer io.Writer
logger echo.Logger
logger logger.Logger
}
)
func NewResponse(c *fasthttp.RequestCtx, l echo.Logger) *Response {
func _NewResponse(c *fasthttp.RequestCtx) *Response {
return &Response{
context: c,
header: &ResponseHeader{c.Response.Header},
writer: c,
logger: l,
logger: log.New("test"),
}
}

View File

@ -5,8 +5,9 @@ package fasthttp
import (
"net/http"
"github.com/labstack/echo"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
"github.com/valyala/fasthttp"
)
@ -15,31 +16,42 @@ type (
*http.Server
config *engine.Config
handler engine.HandlerFunc
logger echo.Logger
logger logger.Logger
}
)
func New(addr string, e *echo.Echo) *Server {
func New(addr string) *Server {
c := &engine.Config{Address: addr}
return NewConfig(c, e)
return NewConfig(c)
}
func NewTLS(addr, certfile, keyfile string, e *echo.Echo) *Server {
func NewTLS(addr, certfile, keyfile string) *Server {
c := &engine.Config{
Address: addr,
TLSCertfile: certfile,
TLSKeyfile: keyfile,
}
return NewConfig(c, e)
return NewConfig(c)
}
func NewConfig(c *engine.Config, e *echo.Echo) *Server {
return &Server{
Server: new(http.Server),
config: c,
handler: e.ServeHTTP,
logger: e.Logger(),
func NewConfig(c *engine.Config) (s *Server) {
s = &Server{
Server: new(http.Server),
config: c,
handler: func(req engine.Request, res engine.Response) {
s.logger.Info("handler not set")
},
logger: log.New("echo"),
}
return
}
func (s *Server) SetHandler(h engine.HandlerFunc) {
s.handler = h
}
func (s *Server) SetLogger(l logger.Logger) {
s.logger = l
}
func (s *Server) Start() {

View File

@ -4,8 +4,8 @@ import (
"io"
"net/http"
"github.com/labstack/echo"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
)
type (
@ -16,11 +16,11 @@ type (
size int64
committed bool
writer io.Writer
logger echo.Logger
logger logger.Logger
}
)
func NewResponse(w http.ResponseWriter, l echo.Logger) *Response {
func NewResponse(w http.ResponseWriter, l logger.Logger) *Response {
return &Response{
response: w,
header: &Header{w.Header()},

View File

@ -4,8 +4,9 @@ import (
"net/http"
"sync"
"github.com/labstack/echo"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
)
type (
@ -14,7 +15,7 @@ type (
config *engine.Config
handler engine.HandlerFunc
pool *Pool
logger echo.Logger
logger logger.Logger
}
Pool struct {
@ -25,25 +26,24 @@ type (
}
)
func New(addr string, e *echo.Echo) *Server {
func New(addr string) *Server {
c := &engine.Config{Address: addr}
return NewConfig(c, e)
return NewConfig(c)
}
func NewTLS(addr, certfile, keyfile string, e *echo.Echo) *Server {
func NewTLS(addr, certfile, keyfile string) *Server {
c := &engine.Config{
Address: addr,
TLSCertfile: certfile,
TLSKeyfile: keyfile,
}
return NewConfig(c, e)
return NewConfig(c)
}
func NewConfig(c *engine.Config, e *echo.Echo) *Server {
return &Server{
Server: new(http.Server),
config: c,
handler: e.ServeHTTP,
func NewConfig(c *engine.Config) (s *Server) {
s = &Server{
Server: new(http.Server),
config: c,
pool: &Pool{
request: sync.Pool{
New: func() interface{} {
@ -52,7 +52,7 @@ func NewConfig(c *engine.Config, e *echo.Echo) *Server {
},
response: sync.Pool{
New: func() interface{} {
return &Response{logger: e.Logger()}
return &Response{logger: s.logger}
},
},
header: sync.Pool{
@ -66,8 +66,20 @@ func NewConfig(c *engine.Config, e *echo.Echo) *Server {
},
},
},
logger: e.Logger(),
handler: func(req engine.Request, res engine.Response) {
s.logger.Info("handler not set")
},
logger: log.New("echo"),
}
return
}
func (s *Server) SetHandler(h engine.HandlerFunc) {
s.handler = h
}
func (s *Server) SetLogger(l logger.Logger) {
s.logger = l
}
func (s *Server) Start() {

21
logger/logger.go Normal file
View File

@ -0,0 +1,21 @@
package logger
type (
// Logger is the interface that declares Echo's logging system.
Logger interface {
Debug(...interface{})
Debugf(string, ...interface{})
Info(...interface{})
Infof(string, ...interface{})
Warn(...interface{})
Warnf(string, ...interface{})
Error(...interface{})
Errorf(string, ...interface{})
Fatal(...interface{})
Fatalf(string, ...interface{})
}
)

View File

@ -19,13 +19,8 @@ type (
)
func NewRequest(method, url string, body io.Reader) engine.Request {
// switch t {
// case engine.Standard:
r, _ := http.NewRequest(method, url, body)
return standard.NewRequest(r)
// default:
// panic("invalid engine")
// }
}
func NewResponseRecorder() *ResponseRecorder {