mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
parent
d0ed5830c4
commit
4e98fa9664
40
context.go
40
context.go
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
@ -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
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user