Skip to content

Commit

Permalink
feat: support pass context to another cod instance
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed May 9, 2019
1 parent f8850b6 commit 3946816
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: go
sudo: required

go:
- "1.11"
- "1.12"
- master

Expand Down
7 changes: 7 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ func (c *Context) Cod(d *Cod) *Cod {
return c.cod
}

// Pass pass requst to another cod
func (c *Context) Pass(another *Cod) {
// 设置为已commit,避免当前cod继续处理
c.Committed = true
another.ServeHTTP(c.Response, c.Request)
}

// NewContext new a context
func NewContext(resp http.ResponseWriter, req *http.Request) *Context {
c := &Context{}
Expand Down
20 changes: 20 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,23 @@ func TestNewContext(t *testing.T) {
assert.Equal(c.Request, req)
assert.Equal(c.Response, resp)
}

func TestContextPass(t *testing.T) {
assert := assert.New(t)
d := New()
another := New()
another.GET("/", func(c *Context) error {
c.BodyBuffer = bytes.NewBufferString("new data")
return nil
})
req := httptest.NewRequest("GET", "https://aslant.site/", nil)
resp := httptest.NewRecorder()
d.GET("/", func(c *Context) error {
c.Pass(another)
c.BodyBuffer = bytes.NewBufferString("original data")
return nil
})
d.ServeHTTP(resp, req)
assert.Equal(resp.Code, 200)
assert.Equal(resp.Body.String(), "new data")
}

0 comments on commit 3946816

Please sign in to comment.