Skip to content

Commit a0bc028

Browse files
author
Vishal Rana
committed
Fixed #551
1 parent 4e98fa9 commit a0bc028

File tree

5 files changed

+92
-38
lines changed

5 files changed

+92
-38
lines changed

context.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,33 @@ type (
2323
// Context represents the context of the current HTTP request. It holds request and
2424
// response objects, path, path parameters, data and registered handler.
2525
Context interface {
26-
context.Context
26+
// Context returns `net/context.Context`.
27+
Context() context.Context
2728

2829
// SetContext sets `net/context.Context`.
2930
SetContext(context.Context)
3031

32+
// Deadline returns the time when work done on behalf of this context
33+
// should be canceled. Deadline returns ok==false when no deadline is
34+
// set. Successive calls to Deadline return the same results.
35+
Deadline() (deadline time.Time, ok bool)
36+
37+
// Done returns a channel that's closed when work done on behalf of this
38+
// context should be canceled. Done may return nil if this context can
39+
// never be canceled. Successive calls to Done return the same value.
40+
Done() <-chan struct{}
41+
42+
// Err returns a non-nil error value after Done is closed. Err returns
43+
// Canceled if the context was canceled or DeadlineExceeded if the
44+
// context's deadline passed. No other values for Err are defined.
45+
// After Done is closed, successive calls to Err return the same value.
46+
Err() error
47+
48+
// Value returns the value associated with this context for key, or nil
49+
// if no value is associated with key. Successive calls to Value with
50+
// the same key returns the same result.
51+
Value(key interface{}) interface{}
52+
3153
// Request returns `engine.Request` interface.
3254
Request() engine.Request
3355

@@ -170,7 +192,7 @@ type (
170192
}
171193

172194
echoContext struct {
173-
context.Context
195+
context context.Context
174196
request engine.Request
175197
response engine.Response
176198
path string
@@ -185,24 +207,28 @@ const (
185207
indexPage = "index.html"
186208
)
187209

210+
func (c *echoContext) Context() context.Context {
211+
return c.context
212+
}
213+
188214
func (c *echoContext) SetContext(ctx context.Context) {
189-
c.Context = ctx
215+
c.context = ctx
190216
}
191217

192218
func (c *echoContext) Deadline() (deadline time.Time, ok bool) {
193-
return c.Context.Deadline()
219+
return c.context.Deadline()
194220
}
195221

196222
func (c *echoContext) Done() <-chan struct{} {
197-
return c.Context.Done()
223+
return c.context.Done()
198224
}
199225

200226
func (c *echoContext) Err() error {
201-
return c.Context.Err()
227+
return c.context.Err()
202228
}
203229

204230
func (c *echoContext) Value(key interface{}) interface{} {
205-
return c.Context.Value(key)
231+
return c.context.Value(key)
206232
}
207233

208234
func (c *echoContext) Request() engine.Request {
@@ -293,11 +319,11 @@ func (c *echoContext) Cookies() []engine.Cookie {
293319
}
294320

295321
func (c *echoContext) Set(key string, val interface{}) {
296-
c.Context = context.WithValue(c, key, val)
322+
c.context = context.WithValue(c.context, key, val)
297323
}
298324

299325
func (c *echoContext) Get(key string) interface{} {
300-
return c.Context.Value(key)
326+
return c.context.Value(key)
301327
}
302328

303329
func (c *echoContext) Bind(i interface{}) error {
@@ -479,7 +505,7 @@ func ContentTypeByExtension(name string) (t string) {
479505
}
480506

481507
func (c *echoContext) Reset(req engine.Request, res engine.Response) {
482-
c.Context = nil
508+
c.context = context.Background()
483509
c.request = req
484510
c.response = res
485511
c.handler = notFoundHandler

echo.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func New() (e *Echo) {
238238
// NewContext returns a Context instance.
239239
func (e *Echo) NewContext(req engine.Request, res engine.Response) Context {
240240
return &echoContext{
241-
Context: context.Background(),
241+
context: context.Background(),
242242
request: req,
243243
response: res,
244244
echo: e,
@@ -338,7 +338,7 @@ func (e *Echo) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) {
338338

339339
// Connect is deprecated, use `CONNECT()` instead.
340340
func (e *Echo) Connect(path string, h HandlerFunc, m ...MiddlewareFunc) {
341-
e.add(CONNECT, path, h, m...)
341+
e.CONNECT(path, h, m...)
342342
}
343343

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

350350
// Delete is deprecated, use `DELETE()` instead.
351351
func (e *Echo) Delete(path string, h HandlerFunc, m ...MiddlewareFunc) {
352-
e.add(DELETE, path, h, m...)
352+
e.DELETE(path, h, m...)
353353
}
354354

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

361361
// Get is deprecated, use `GET()` instead.
362362
func (e *Echo) Get(path string, h HandlerFunc, m ...MiddlewareFunc) {
363-
e.add(GET, path, h, m...)
363+
e.GET(path, h, m...)
364364
}
365365

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

372372
// Head is deprecated, use `HEAD()` instead.
373373
func (e *Echo) Head(path string, h HandlerFunc, m ...MiddlewareFunc) {
374-
e.add(HEAD, path, h, m...)
374+
e.HEAD(path, h, m...)
375375
}
376376

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

383383
// Options is deprecated, use `OPTIONS()` instead.
384384
func (e *Echo) Options(path string, h HandlerFunc, m ...MiddlewareFunc) {
385-
e.add(OPTIONS, path, h, m...)
385+
e.OPTIONS(path, h, m...)
386386
}
387387

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

394394
// Patch is deprecated, use `PATCH()` instead.
395395
func (e *Echo) Patch(path string, h HandlerFunc, m ...MiddlewareFunc) {
396-
e.add(PATCH, path, h, m...)
396+
e.PATCH(path, h, m...)
397397
}
398398

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

405405
// Post is deprecated, use `POST()` instead.
406406
func (e *Echo) Post(path string, h HandlerFunc, m ...MiddlewareFunc) {
407-
e.add(POST, path, h, m...)
407+
e.POST(path, h, m...)
408408
}
409409

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

416416
// Put is deprecated, use `PUT()` instead.
417417
func (e *Echo) Put(path string, h HandlerFunc, m ...MiddlewareFunc) {
418-
e.add(PUT, path, h, m...)
418+
e.PUT(path, h, m...)
419419
}
420420

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

427427
// Trace is deprecated, use `TRACE()` instead.
428428
func (e *Echo) Trace(path string, h HandlerFunc, m ...MiddlewareFunc) {
429-
e.add(TRACE, path, h, m...)
429+
e.TRACE(path, h, m...)
430430
}
431431

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

534-
// GetContext is deprecated, use `AcquireContext()` instead.
535-
func (e *Echo) GetContext() Context {
536-
return e.pool.Get().(Context)
537-
}
538-
539534
// ReleaseContext returns the `Context` instance back to the pool.
540535
// You must call it after `AcquireContext()`.
541536
func (e *Echo) ReleaseContext(c Context) {
542537
e.pool.Put(c)
543538
}
544539

545-
// PutContext is deprecated, use `ReleaseContext()` instead.
546-
func (e *Echo) PutContext(c Context) {
547-
e.ReleaseContext(c)
548-
}
549-
550540
func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
551541
c := e.pool.Get().(*echoContext)
552542
c.Reset(req, res)

echo_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package echo
33
import (
44
"bytes"
55
"fmt"
6+
"io/ioutil"
67
"net/http"
78
"testing"
89

@@ -12,6 +13,7 @@ import (
1213
"errors"
1314

1415
"github.com/labstack/echo/test"
16+
"github.com/labstack/gommon/log"
1517
"github.com/stretchr/testify/assert"
1618
)
1719

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

333+
func TestEchoContext(t *testing.T) {
334+
e := New()
335+
c := e.AcquireContext()
336+
assert.IsType(t, new(echoContext), c)
337+
e.ReleaseContext(c)
338+
}
339+
340+
func TestEchoLogger(t *testing.T) {
341+
e := New()
342+
l := log.New("test")
343+
e.SetLogger(l)
344+
assert.Equal(t, l, e.Logger())
345+
e.SetLogOutput(ioutil.Discard)
346+
assert.Equal(t, l.Output(), ioutil.Discard)
347+
e.SetLogLevel(log.OFF)
348+
assert.Equal(t, l.Level(), log.OFF)
349+
}
350+
331351
func testMethod(t *testing.T, method, path string, e *Echo) {
332352
m := fmt.Sprintf("%c%s", method[0], strings.ToLower(method[1:]))
333353
p := reflect.ValueOf(path)

glide.lock

Lines changed: 16 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

group_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@ package echo
22

33
import "testing"
44

5+
// TODO: Fix me
56
func TestGroup(t *testing.T) {
67
g := New().Group("/group")
78
h := func(Context) error { return nil }
89
g.CONNECT("/", h)
10+
g.Connect("/", h)
911
g.DELETE("/", h)
12+
g.Delete("/", h)
1013
g.GET("/", h)
14+
g.Get("/", h)
1115
g.HEAD("/", h)
16+
g.Head("/", h)
1217
g.OPTIONS("/", h)
18+
g.Options("/", h)
1319
g.PATCH("/", h)
20+
g.Patch("/", h)
1421
g.POST("/", h)
22+
g.Post("/", h)
1523
g.PUT("/", h)
24+
g.Put("/", h)
1625
g.TRACE("/", h)
26+
g.Trace("/", h)
1727
g.Any("/", h)
1828
g.Match([]string{GET, POST}, "/", h)
1929
g.Static("/static", "/tmp")

0 commit comments

Comments
 (0)