Skip to content

Commit

Permalink
Fixed labstack#500
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Jun 6, 2016
1 parent d0ed583 commit 4e98fa9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 44 deletions.
40 changes: 5 additions & 35 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ type (
Context interface {
context.Context

// StdContext returns `net/context.Context`. By default it is set the background
// context. To change the context, use SetStdContext().
StdContext() context.Context

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

// Request returns `engine.Request` interface.
Request() engine.Request
Expand Down Expand Up @@ -104,12 +100,6 @@ type (
// Set saves data in the context.
Set(string, interface{})

// Del deletes data from the context.
Del(string)

// Contains checks if the key exists in the context.
Contains(string) bool

// Bind binds the request body into provided type `i`. The default binder
// does it based on Content-Type header.
Bind(interface{}) error
Expand Down Expand Up @@ -186,23 +176,16 @@ type (
path string
pnames []string
pvalues []string
store store
handler HandlerFunc
echo *Echo
}

store map[string]interface{}
)

const (
indexPage = "index.html"
)

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

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

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

func (c *echoContext) Set(key string, val interface{}) {
if c.store == nil {
c.store = make(store)
}
c.store[key] = val
c.Context = context.WithValue(c, key, val)
}

func (c *echoContext) Get(key string) interface{} {
return c.store[key]
}

func (c *echoContext) Del(key string) {
delete(c.store, key)
}

func (c *echoContext) Contains(key string) bool {
_, ok := c.store[key]
return ok
return c.Context.Value(key)
}

func (c *echoContext) Bind(i interface{}) error {
Expand Down Expand Up @@ -511,6 +482,5 @@ func (c *echoContext) Reset(req engine.Request, res engine.Response) {
c.Context = nil
c.request = req
c.response = res
c.store = nil
c.handler = notFoundHandler
}
15 changes: 7 additions & 8 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,24 +327,23 @@ func TestContextRedirect(t *testing.T) {
assert.Error(t, c.Redirect(310, "http://labstack.github.io/echo"))
}

func TestContextStdContext(t *testing.T) {
func TestContextEmbedded(t *testing.T) {
c := new(echoContext)
c.SetStdContext(context.WithValue(c.StdContext(), "key", "val"))
c.SetContext(context.WithValue(c, "key", "val"))
assert.Equal(t, "val", c.Value("key"))
ctx, _ := context.WithDeadline(context.Background(), time.Now())
c.SetStdContext(ctx)
now := time.Now()
ctx, _ := context.WithDeadline(context.Background(), now)
c.SetContext(ctx)
n, _ := ctx.Deadline()
assert.Equal(t, now, n)
assert.Equal(t, context.DeadlineExceeded, c.Err())
assert.NotNil(t, c.Done())
}

func TestContextStore(t *testing.T) {
c := new(echoContext)
c.store = nil
c.Set("name", "Jon Snow")
assert.Equal(t, "Jon Snow", c.Get("name"))
assert.True(t, c.Contains("name"))
c.Del("name")
assert.Empty(t, c.Get("name"))
}

func TestContextServeContent(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ func (e *Echo) NewContext(req engine.Request, res engine.Response) Context {
response: res,
echo: e,
pvalues: make([]string, *e.maxParam),
store: make(store),
handler: notFoundHandler,
}
}
Expand Down

0 comments on commit 4e98fa9

Please sign in to comment.