Skip to content

Commit

Permalink
Merge branch 'master' of github.com:alecthomas/martini into alecthoma…
Browse files Browse the repository at this point in the history
…s-master
  • Loading branch information
codegangsta committed Dec 5, 2013
2 parents b649d7b + d2e7658 commit fc85f10
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions response_writer.go
Expand Up @@ -78,6 +78,10 @@ func (rw *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return hijacker.Hijack()
}

func (rw *responseWriter) CloseNotify() <-chan bool {
return rw.ResponseWriter.(http.CloseNotifier).CloseNotify()
}

func (rw *responseWriter) callBefore() {
for i := len(rw.beforeFuncs) - 1; i >= 0; i-- {
rw.beforeFuncs[i](rw)
Expand Down
35 changes: 35 additions & 0 deletions response_writer_test.go
Expand Up @@ -6,8 +6,29 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"
)

type closeNotifyingRecorder struct {
*httptest.ResponseRecorder
closed chan bool
}

func newCloseNotifyingRecorder() *closeNotifyingRecorder {
return &closeNotifyingRecorder{
httptest.NewRecorder(),
make(chan bool, 1),
}
}

func (c *closeNotifyingRecorder) close() {
c.closed <- true
}

func (c *closeNotifyingRecorder) CloseNotify() <-chan bool {
return c.closed
}

type hijackableResponse struct {
Hijacked bool
}
Expand Down Expand Up @@ -95,3 +116,17 @@ func Test_ResponseWriter_Hijack(t *testing.T) {
}
expect(t, hijackable.Hijacked, true)
}

func Test_ResponseWriter_CloseNotify(t *testing.T) {
rec := newCloseNotifyingRecorder()
rw := NewResponseWriter(rec)
closed := false
notifier := rw.(http.CloseNotifier).CloseNotify()
rec.close()
select {
case <-notifier:
closed = true
case <-time.After(time.Second):
}
expect(t, closed, true)
}

0 comments on commit fc85f10

Please sign in to comment.