Skip to content

Commit

Permalink
Fixed test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vishal.rana@verizon.com>
  • Loading branch information
Vishal Rana committed Feb 10, 2016
1 parent 4f57582 commit 94e5936
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 69 deletions.
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -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"
)

Expand All @@ -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"))
}
```

Expand Down
5 changes: 3 additions & 2 deletions context.go
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"

"net/url"

Expand Down Expand Up @@ -45,7 +46,7 @@ type (
NoContent(int) error
Redirect(int, string) error
Error(err error)
Logger() Logger
Logger() logger.Logger
Object() *context
}

Expand Down Expand Up @@ -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
}

Expand Down
31 changes: 8 additions & 23 deletions echo.go
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/xml"

"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
"github.com/labstack/gommon/log"
)

Expand All @@ -35,7 +36,7 @@ type (
hook engine.HandlerFunc
autoIndex bool
router *Router
logger Logger
logger logger.Logger
}

Route struct {
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions echo_test.go
Expand Up @@ -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())
}

Expand All @@ -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())
}

Expand Down Expand Up @@ -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")
}

Expand All @@ -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()
}
4 changes: 4 additions & 0 deletions engine/engine.go
Expand Up @@ -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()
}

Expand Down
9 changes: 5 additions & 4 deletions engine/fasthttp/response.go
Expand Up @@ -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"
)

Expand All @@ -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"),
}
}

Expand Down
36 changes: 24 additions & 12 deletions engine/fasthttp/server.go
Expand Up @@ -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"
)

Expand All @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions engine/standard/response.go
Expand Up @@ -4,8 +4,8 @@ import (
"io"
"net/http"

"github.com/labstack/echo"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
)

type (
Expand All @@ -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()},
Expand Down
38 changes: 25 additions & 13 deletions engine/standard/server.go
Expand Up @@ -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 (
Expand All @@ -14,7 +15,7 @@ type (
config *engine.Config
handler engine.HandlerFunc
pool *Pool
logger echo.Logger
logger logger.Logger
}

Pool struct {
Expand All @@ -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{} {
Expand All @@ -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{
Expand All @@ -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() {
Expand Down
21 changes: 21 additions & 0 deletions logger/logger.go
@@ -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{})
}
)

0 comments on commit 94e5936

Please sign in to comment.