1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-26 03:20:08 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-06-06 09:47:22 -07:00
parent d0ed5830c4
commit 4e98fa9664
3 changed files with 12 additions and 44 deletions

View File

@ -25,12 +25,8 @@ type (
Context interface { Context interface {
context.Context context.Context
// StdContext returns `net/context.Context`. By default it is set the background // SetContext sets `net/context.Context`.
// context. To change the context, use SetStdContext(). SetContext(context.Context)
StdContext() context.Context
// SetStdContext sets `net/context.Context`.
SetStdContext(context.Context)
// Request returns `engine.Request` interface. // Request returns `engine.Request` interface.
Request() engine.Request Request() engine.Request
@ -104,12 +100,6 @@ type (
// Set saves data in the context. // Set saves data in the context.
Set(string, interface{}) 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 // Bind binds the request body into provided type `i`. The default binder
// does it based on Content-Type header. // does it based on Content-Type header.
Bind(interface{}) error Bind(interface{}) error
@ -186,23 +176,16 @@ type (
path string path string
pnames []string pnames []string
pvalues []string pvalues []string
store store
handler HandlerFunc handler HandlerFunc
echo *Echo echo *Echo
} }
store map[string]interface{}
) )
const ( const (
indexPage = "index.html" indexPage = "index.html"
) )
func (c *echoContext) StdContext() context.Context { func (c *echoContext) SetContext(ctx context.Context) {
return c.Context
}
func (c *echoContext) SetStdContext(ctx context.Context) {
c.Context = ctx c.Context = ctx
} }
@ -310,23 +293,11 @@ func (c *echoContext) Cookies() []engine.Cookie {
} }
func (c *echoContext) Set(key string, val interface{}) { func (c *echoContext) Set(key string, val interface{}) {
if c.store == nil { c.Context = context.WithValue(c, key, val)
c.store = make(store)
}
c.store[key] = val
} }
func (c *echoContext) Get(key string) interface{} { func (c *echoContext) Get(key string) interface{} {
return c.store[key] return c.Context.Value(key)
}
func (c *echoContext) Del(key string) {
delete(c.store, key)
}
func (c *echoContext) Contains(key string) bool {
_, ok := c.store[key]
return ok
} }
func (c *echoContext) Bind(i interface{}) error { func (c *echoContext) Bind(i interface{}) error {
@ -511,6 +482,5 @@ func (c *echoContext) Reset(req engine.Request, res engine.Response) {
c.Context = nil c.Context = nil
c.request = req c.request = req
c.response = res c.response = res
c.store = nil
c.handler = notFoundHandler c.handler = notFoundHandler
} }

View File

@ -327,24 +327,23 @@ func TestContextRedirect(t *testing.T) {
assert.Error(t, c.Redirect(310, "http://labstack.github.io/echo")) 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 := new(echoContext)
c.SetStdContext(context.WithValue(c.StdContext(), "key", "val")) c.SetContext(context.WithValue(c, "key", "val"))
assert.Equal(t, "val", c.Value("key")) assert.Equal(t, "val", c.Value("key"))
ctx, _ := context.WithDeadline(context.Background(), time.Now()) now := time.Now()
c.SetStdContext(ctx) 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.Equal(t, context.DeadlineExceeded, c.Err())
assert.NotNil(t, c.Done()) assert.NotNil(t, c.Done())
} }
func TestContextStore(t *testing.T) { func TestContextStore(t *testing.T) {
c := new(echoContext) c := new(echoContext)
c.store = nil
c.Set("name", "Jon Snow") c.Set("name", "Jon Snow")
assert.Equal(t, "Jon Snow", c.Get("name")) 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) { func TestContextServeContent(t *testing.T) {

View File

@ -243,7 +243,6 @@ func (e *Echo) NewContext(req engine.Request, res engine.Response) Context {
response: res, response: res,
echo: e, echo: e,
pvalues: make([]string, *e.maxParam), pvalues: make([]string, *e.maxParam),
store: make(store),
handler: notFoundHandler, handler: notFoundHandler,
} }
} }