Add GetRequest() method to Context

This allows to use the raw http.Request even when the context was
only passed as an interface.
This commit is contained in:
Philippe Hässig 2018-02-11 14:16:08 +01:00
parent 783c7ee9c1
commit e863977fae
No known key found for this signature in database
GPG Key ID: 57338A09DD84E473
2 changed files with 13 additions and 0 deletions

View File

@ -94,6 +94,11 @@ func (c *Context) Handler() HandlerFunc {
return c.handlers.Last() return c.handlers.Last()
} }
// GetRequest returns the raw http.Request.
func (c *Context) GetRequest() *http.Request {
return c.Request
}
/************************************/ /************************************/
/*********** FLOW CONTROL ***********/ /*********** FLOW CONTROL ***********/
/************************************/ /************************************/

View File

@ -332,6 +332,14 @@ func TestContextHandler(t *testing.T) {
assert.Equal(t, reflect.ValueOf(handlerTest).Pointer(), reflect.ValueOf(c.Handler()).Pointer()) assert.Equal(t, reflect.ValueOf(handlerTest).Pointer(), reflect.ValueOf(c.Handler()).Pointer())
} }
func TestContextGetRequest(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
request, _ := http.NewRequest("GET", "http://example.com/?foo=bar&page=10&id=", nil)
c.Request = request
assert.Equal(t, request, c.GetRequest())
}
func TestContextQuery(t *testing.T) { func TestContextQuery(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder()) c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10&id=", nil) c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10&id=", nil)