1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-28 08:38:39 +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 ( import (
"github.com/labstack/echo" "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" mw "github.com/labstack/echo/middleware"
) )
@ -25,10 +26,10 @@ func main() {
}) })
// FastHTTP // FastHTTP
e.Run(fasthttp.New(":4444", e)) // e.Run(fasthttp.New(":4444"))
// Standard // Standard
// e.Run(standard.New(":4444")) e.Run(standard.New(":4444"))
} }
``` ```

View File

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

31
echo.go
View File

@ -17,6 +17,7 @@ import (
"encoding/xml" "encoding/xml"
"github.com/labstack/echo/engine" "github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log" "github.com/labstack/gommon/log"
) )
@ -35,7 +36,7 @@ type (
hook engine.HandlerFunc hook engine.HandlerFunc
autoIndex bool autoIndex bool
router *Router router *Router
logger Logger logger logger.Logger
} }
Route struct { Route struct {
@ -81,24 +82,6 @@ type (
Renderer interface { Renderer interface {
Render(w io.Writer, name string, data interface{}) error 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 ( const (
@ -242,12 +225,12 @@ func (e *Echo) Router() *Router {
} }
// SetLogger sets the logger instance. // SetLogger sets the logger instance.
func (e *Echo) SetLogger(l Logger) { func (e *Echo) SetLogger(l logger.Logger) {
e.logger = l e.logger = l
} }
// Logger returns the logger instance. // Logger returns the logger instance.
func (e *Echo) Logger() Logger { func (e *Echo) Logger() logger.Logger {
return e.logger return e.logger
} }
@ -560,8 +543,10 @@ func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
// } // }
// Run starts the HTTP engine. // Run starts the HTTP engine.
func (*Echo) Run(e engine.Engine) { func (e *Echo) Run(eng engine.Engine) {
e.Start() eng.SetHandler(e.ServeHTTP)
eng.SetLogger(e.logger)
eng.Start()
} }
func NewHTTPError(code int, msg ...string) *HTTPError { func NewHTTPError(code int, msg ...string) *HTTPError {

View File

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

View File

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

View File

@ -5,8 +5,9 @@ package fasthttp
import ( import (
"io" "io"
"github.com/labstack/echo"
"github.com/labstack/echo/engine" "github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )
@ -18,16 +19,16 @@ type (
size int64 size int64
committed bool committed bool
writer io.Writer 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{ return &Response{
context: c, context: c,
header: &ResponseHeader{c.Response.Header}, header: &ResponseHeader{c.Response.Header},
writer: c, writer: c,
logger: l, logger: log.New("test"),
} }
} }

View File

@ -5,8 +5,9 @@ package fasthttp
import ( import (
"net/http" "net/http"
"github.com/labstack/echo"
"github.com/labstack/echo/engine" "github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )
@ -15,31 +16,42 @@ type (
*http.Server *http.Server
config *engine.Config config *engine.Config
handler engine.HandlerFunc 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} 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{ c := &engine.Config{
Address: addr, Address: addr,
TLSCertfile: certfile, TLSCertfile: certfile,
TLSKeyfile: keyfile, TLSKeyfile: keyfile,
} }
return NewConfig(c, e) return NewConfig(c)
} }
func NewConfig(c *engine.Config, e *echo.Echo) *Server { func NewConfig(c *engine.Config) (s *Server) {
return &Server{ s = &Server{
Server: new(http.Server), Server: new(http.Server),
config: c, config: c,
handler: e.ServeHTTP, handler: func(req engine.Request, res engine.Response) {
logger: e.Logger(), 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() { func (s *Server) Start() {

View File

@ -4,8 +4,8 @@ import (
"io" "io"
"net/http" "net/http"
"github.com/labstack/echo"
"github.com/labstack/echo/engine" "github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
) )
type ( type (
@ -16,11 +16,11 @@ type (
size int64 size int64
committed bool committed bool
writer io.Writer 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{ return &Response{
response: w, response: w,
header: &Header{w.Header()}, header: &Header{w.Header()},

View File

@ -4,8 +4,9 @@ import (
"net/http" "net/http"
"sync" "sync"
"github.com/labstack/echo"
"github.com/labstack/echo/engine" "github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
) )
type ( type (
@ -14,7 +15,7 @@ type (
config *engine.Config config *engine.Config
handler engine.HandlerFunc handler engine.HandlerFunc
pool *Pool pool *Pool
logger echo.Logger logger logger.Logger
} }
Pool struct { 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} 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{ c := &engine.Config{
Address: addr, Address: addr,
TLSCertfile: certfile, TLSCertfile: certfile,
TLSKeyfile: keyfile, TLSKeyfile: keyfile,
} }
return NewConfig(c, e) return NewConfig(c)
} }
func NewConfig(c *engine.Config, e *echo.Echo) *Server { func NewConfig(c *engine.Config) (s *Server) {
return &Server{ s = &Server{
Server: new(http.Server), Server: new(http.Server),
config: c, config: c,
handler: e.ServeHTTP,
pool: &Pool{ pool: &Pool{
request: sync.Pool{ request: sync.Pool{
New: func() interface{} { New: func() interface{} {
@ -52,7 +52,7 @@ func NewConfig(c *engine.Config, e *echo.Echo) *Server {
}, },
response: sync.Pool{ response: sync.Pool{
New: func() interface{} { New: func() interface{} {
return &Response{logger: e.Logger()} return &Response{logger: s.logger}
}, },
}, },
header: sync.Pool{ 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() { 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 { func NewRequest(method, url string, body io.Reader) engine.Request {
// switch t {
// case engine.Standard:
r, _ := http.NewRequest(method, url, body) r, _ := http.NewRequest(method, url, body)
return standard.NewRequest(r) return standard.NewRequest(r)
// default:
// panic("invalid engine")
// }
} }
func NewResponseRecorder() *ResponseRecorder { func NewResponseRecorder() *ResponseRecorder {