mirror of
				https://github.com/labstack/echo.git
				synced 2025-10-30 23:57:38 +02:00 
			
		
		
		
	Fixed #551
This commit is contained in:
		
							
								
								
									
										46
									
								
								context.go
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								context.go
									
									
									
									
									
								
							| @@ -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 | ||||
|  | ||||
| @@ -170,7 +192,7 @@ type ( | ||||
| 	} | ||||
|  | ||||
| 	echoContext struct { | ||||
| 		context.Context | ||||
| 		context  context.Context | ||||
| 		request  engine.Request | ||||
| 		response engine.Response | ||||
| 		path     string | ||||
| @@ -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 { | ||||
| @@ -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 { | ||||
| @@ -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 | ||||
|   | ||||
							
								
								
									
										30
									
								
								echo.go
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								echo.go
									
									
									
									
									
								
							| @@ -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, | ||||
| @@ -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 | ||||
| @@ -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 | ||||
| @@ -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 | ||||
| @@ -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 | ||||
| @@ -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 | ||||
| @@ -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 | ||||
| @@ -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 | ||||
| @@ -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 | ||||
| @@ -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 | ||||
| @@ -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) | ||||
|   | ||||
							
								
								
									
										20
									
								
								echo_test.go
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								echo_test.go
									
									
									
									
									
								
							| @@ -3,6 +3,7 @@ package echo | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"net/http" | ||||
| 	"testing" | ||||
|  | ||||
| @@ -12,6 +13,7 @@ import ( | ||||
| 	"errors" | ||||
|  | ||||
| 	"github.com/labstack/echo/test" | ||||
| 	"github.com/labstack/gommon/log" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| @@ -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) | ||||
|   | ||||
							
								
								
									
										24
									
								
								glide.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										24
									
								
								glide.lock
									
									
									
										generated
									
									
									
								
							| @@ -1,8 +1,12 @@ | ||||
| hash: 21820434709470e49c64df0f854d3352088ca664d193e29bc6cd434518c27a7c | ||||
| updated: 2016-05-10T08:56:44.956487077-07:00 | ||||
| updated: 2016-06-06T13:46:58.552123246-07:00 | ||||
| imports: | ||||
| - name: github.com/davecgh/go-spew | ||||
|   version: 5215b55f46b2b919f50a1df0eaa5886afe4e3b3d | ||||
|   subpackages: | ||||
|   - spew | ||||
| - name: github.com/dgrijalva/jwt-go | ||||
|   version: 40bd0f3b4891a9d7f121bfb7b8e8b0525625e262 | ||||
|   version: 9b486c879bab3fde556ce8c27d9a2bb05d5b2c60 | ||||
| - name: github.com/klauspost/compress | ||||
|   version: 14eb9c4951195779ecfbec34431a976de7335b0a | ||||
|   subpackages: | ||||
| @@ -14,30 +18,34 @@ imports: | ||||
| - name: github.com/klauspost/crc32 | ||||
|   version: 19b0b332c9e4516a6370a0456e6182c3b5036720 | ||||
| - name: github.com/labstack/gommon | ||||
|   version: c8e533fc9cb891db60f659489cb7c55361ca393c | ||||
|   version: c21cbdaff511c8f7c27dab37216552d8e7ea7bde | ||||
|   subpackages: | ||||
|   - color | ||||
|   - log | ||||
|   - bytes | ||||
| - name: github.com/mattn/go-colorable | ||||
|   version: 45ce6a6f60010487dd0dbab368b5fbbed1c14ef0 | ||||
|   version: 9056b7a9f2d1f2d96498d6d146acd1f9d5ed3d59 | ||||
| - name: github.com/mattn/go-isatty | ||||
|   version: 56b76bdf51f7708750eac80fa38b952bb9f32639 | ||||
| - name: github.com/pmezard/go-difflib | ||||
|   version: d8ed2627bdf02c080bf22230dbb337003b7aba2d | ||||
|   subpackages: | ||||
|   - difflib | ||||
| - name: github.com/stretchr/testify | ||||
|   version: 6cb3b85ef5a0efef77caef88363ec4d4b5c0976d | ||||
|   version: 8d64eb7173c7753d6419fd4a9caf057398611364 | ||||
|   subpackages: | ||||
|   - assert | ||||
| - name: github.com/valyala/fasthttp | ||||
|   version: e4ed265221501e609b08aa95c05aacfd2f9a3167 | ||||
|   version: 3c5ba2c98d622df52926a4d17be18a6728ccaf1c | ||||
| - name: github.com/valyala/fasttemplate | ||||
|   version: 3b874956e03f1636d171bda64b130f9135f42cff | ||||
| - name: golang.org/x/net | ||||
|   version: 2a35e686583654a1b89ca79c4ac78cb3d6529ca3 | ||||
|   version: c4c3ea71919de159c9e246d7be66deb7f0a39a58 | ||||
|   subpackages: | ||||
|   - context | ||||
|   - websocket | ||||
| - name: golang.org/x/sys | ||||
|   version: b776ec39b3e54652e09028aaaaac9757f4f8211a | ||||
|   version: 076b546753157f758b316e59bcb51e6807c04057 | ||||
|   subpackages: | ||||
|   - unix | ||||
| devImports: [] | ||||
|   | ||||
| @@ -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") | ||||
|   | ||||
		Reference in New Issue
	
	Block a user