Skip to content

Commit

Permalink
Fixed labstack#551
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishal Rana committed Jun 6, 2016
1 parent 4e98fa9 commit a0bc028
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 38 deletions.
46 changes: 36 additions & 10 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,33 @@ type (
// Context represents the context of the current HTTP request. It holds request and
// response objects, path, path parameters, data and registered handler.
Context interface {
context.Context
// Context returns `net/context.Context`.
Context() context.Context

// SetContext sets `net/context.Context`.
SetContext(context.Context)

// Deadline returns the time when work done on behalf of this context
// should be canceled. Deadline returns ok==false when no deadline is
// set. Successive calls to Deadline return the same results.
Deadline() (deadline time.Time, ok bool)

// Done returns a channel that's closed when work done on behalf of this
// context should be canceled. Done may return nil if this context can
// never be canceled. Successive calls to Done return the same value.
Done() <-chan struct{}

// Err returns a non-nil error value after Done is closed. Err returns
// Canceled if the context was canceled or DeadlineExceeded if the
// context's deadline passed. No other values for Err are defined.
// After Done is closed, successive calls to Err return the same value.
Err() error

// Value returns the value associated with this context for key, or nil
// if no value is associated with key. Successive calls to Value with
// the same key returns the same result.
Value(key interface{}) interface{}

// Request returns `engine.Request` interface.
Request() engine.Request

Expand Down Expand Up @@ -170,7 +192,7 @@ type (
}

echoContext struct {
context.Context
context context.Context
request engine.Request
response engine.Response
path string
Expand All @@ -185,24 +207,28 @@ const (
indexPage = "index.html"
)

func (c *echoContext) Context() context.Context {
return c.context
}

func (c *echoContext) SetContext(ctx context.Context) {
c.Context = ctx
c.context = ctx
}

func (c *echoContext) Deadline() (deadline time.Time, ok bool) {
return c.Context.Deadline()
return c.context.Deadline()
}

func (c *echoContext) Done() <-chan struct{} {
return c.Context.Done()
return c.context.Done()
}

func (c *echoContext) Err() error {
return c.Context.Err()
return c.context.Err()
}

func (c *echoContext) Value(key interface{}) interface{} {
return c.Context.Value(key)
return c.context.Value(key)
}

func (c *echoContext) Request() engine.Request {
Expand Down Expand Up @@ -293,11 +319,11 @@ func (c *echoContext) Cookies() []engine.Cookie {
}

func (c *echoContext) Set(key string, val interface{}) {
c.Context = context.WithValue(c, key, val)
c.context = context.WithValue(c.context, key, val)
}

func (c *echoContext) Get(key string) interface{} {
return c.Context.Value(key)
return c.context.Value(key)
}

func (c *echoContext) Bind(i interface{}) error {
Expand Down Expand Up @@ -479,7 +505,7 @@ func ContentTypeByExtension(name string) (t string) {
}

func (c *echoContext) Reset(req engine.Request, res engine.Response) {
c.Context = nil
c.context = context.Background()
c.request = req
c.response = res
c.handler = notFoundHandler
Expand Down
30 changes: 10 additions & 20 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func New() (e *Echo) {
// NewContext returns a Context instance.
func (e *Echo) NewContext(req engine.Request, res engine.Response) Context {
return &echoContext{
Context: context.Background(),
context: context.Background(),
request: req,
response: res,
echo: e,
Expand Down Expand Up @@ -338,7 +338,7 @@ func (e *Echo) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) {

// Connect is deprecated, use `CONNECT()` instead.
func (e *Echo) Connect(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(CONNECT, path, h, m...)
e.CONNECT(path, h, m...)
}

// DELETE registers a new DELETE route for a path with matching handler in the router
Expand All @@ -349,7 +349,7 @@ func (e *Echo) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) {

// Delete is deprecated, use `DELETE()` instead.
func (e *Echo) Delete(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(DELETE, path, h, m...)
e.DELETE(path, h, m...)
}

// GET registers a new GET route for a path with matching handler in the router
Expand All @@ -360,7 +360,7 @@ func (e *Echo) GET(path string, h HandlerFunc, m ...MiddlewareFunc) {

// Get is deprecated, use `GET()` instead.
func (e *Echo) Get(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(GET, path, h, m...)
e.GET(path, h, m...)
}

// HEAD registers a new HEAD route for a path with matching handler in the
Expand All @@ -371,7 +371,7 @@ func (e *Echo) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) {

// Head is deprecated, use `HEAD()` instead.
func (e *Echo) Head(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(HEAD, path, h, m...)
e.HEAD(path, h, m...)
}

// OPTIONS registers a new OPTIONS route for a path with matching handler in the
Expand All @@ -382,7 +382,7 @@ func (e *Echo) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) {

// Options is deprecated, use `OPTIONS()` instead.
func (e *Echo) Options(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(OPTIONS, path, h, m...)
e.OPTIONS(path, h, m...)
}

// PATCH registers a new PATCH route for a path with matching handler in the
Expand All @@ -393,7 +393,7 @@ func (e *Echo) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) {

// Patch is deprecated, use `PATCH()` instead.
func (e *Echo) Patch(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(PATCH, path, h, m...)
e.PATCH(path, h, m...)
}

// POST registers a new POST route for a path with matching handler in the
Expand All @@ -404,7 +404,7 @@ func (e *Echo) POST(path string, h HandlerFunc, m ...MiddlewareFunc) {

// Post is deprecated, use `POST()` instead.
func (e *Echo) Post(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(POST, path, h, m...)
e.POST(path, h, m...)
}

// PUT registers a new PUT route for a path with matching handler in the
Expand All @@ -415,7 +415,7 @@ func (e *Echo) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) {

// Put is deprecated, use `PUT()` instead.
func (e *Echo) Put(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(PUT, path, h, m...)
e.PUT(path, h, m...)
}

// TRACE registers a new TRACE route for a path with matching handler in the
Expand All @@ -426,7 +426,7 @@ func (e *Echo) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) {

// Trace is deprecated, use `TRACE()` instead.
func (e *Echo) Trace(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(TRACE, path, h, m...)
e.TRACE(path, h, m...)
}

// Any registers a new route for all HTTP methods and path with matching handler
Expand Down Expand Up @@ -531,22 +531,12 @@ func (e *Echo) AcquireContext() Context {
return e.pool.Get().(Context)
}

// GetContext is deprecated, use `AcquireContext()` instead.
func (e *Echo) GetContext() Context {
return e.pool.Get().(Context)
}

// ReleaseContext returns the `Context` instance back to the pool.
// You must call it after `AcquireContext()`.
func (e *Echo) ReleaseContext(c Context) {
e.pool.Put(c)
}

// PutContext is deprecated, use `ReleaseContext()` instead.
func (e *Echo) PutContext(c Context) {
e.ReleaseContext(c)
}

func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
c := e.pool.Get().(*echoContext)
c.Reset(req, res)
Expand Down
20 changes: 20 additions & 0 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package echo
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"testing"

Expand All @@ -12,6 +13,7 @@ import (
"errors"

"github.com/labstack/echo/test"
"github.com/labstack/gommon/log"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -328,6 +330,24 @@ func TestEchoHTTPError(t *testing.T) {
assert.Equal(t, m, he.Error())
}

func TestEchoContext(t *testing.T) {
e := New()
c := e.AcquireContext()
assert.IsType(t, new(echoContext), c)
e.ReleaseContext(c)
}

func TestEchoLogger(t *testing.T) {
e := New()
l := log.New("test")
e.SetLogger(l)
assert.Equal(t, l, e.Logger())
e.SetLogOutput(ioutil.Discard)
assert.Equal(t, l.Output(), ioutil.Discard)
e.SetLogLevel(log.OFF)
assert.Equal(t, l.Level(), log.OFF)
}

func testMethod(t *testing.T, method, path string, e *Echo) {
m := fmt.Sprintf("%c%s", method[0], strings.ToLower(method[1:]))
p := reflect.ValueOf(path)
Expand Down
24 changes: 16 additions & 8 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ package echo

import "testing"

// TODO: Fix me
func TestGroup(t *testing.T) {
g := New().Group("/group")
h := func(Context) error { return nil }
g.CONNECT("/", h)
g.Connect("/", h)
g.DELETE("/", h)
g.Delete("/", h)
g.GET("/", h)
g.Get("/", h)
g.HEAD("/", h)
g.Head("/", h)
g.OPTIONS("/", h)
g.Options("/", h)
g.PATCH("/", h)
g.Patch("/", h)
g.POST("/", h)
g.Post("/", h)
g.PUT("/", h)
g.Put("/", h)
g.TRACE("/", h)
g.Trace("/", h)
g.Any("/", h)
g.Match([]string{GET, POST}, "/", h)
g.Static("/static", "/tmp")
Expand Down

0 comments on commit a0bc028

Please sign in to comment.