Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mafredri committed Aug 21, 2024
1 parent 12cbf54 commit 230a06c
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions accept_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,53 @@ func TestAccept(t *testing.T) {
_, err := Accept(w, r, nil)
assert.Contains(t, err, `failed to hijack connection`)
})

t.Run("wrapperHijackerIsUnwrapped", func(t *testing.T) {
t.Parallel()

rr := httptest.NewRecorder()
w := mockUnwrapper{
ResponseWriter: rr,
unwrap: func() http.ResponseWriter {
return mockHijacker{
ResponseWriter: rr,
hijack: func() (conn net.Conn, writer *bufio.ReadWriter, err error) {
return nil, nil, errors.New("haha")
},
}
},
}

r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("Connection", "Upgrade")
r.Header.Set("Upgrade", "websocket")
r.Header.Set("Sec-WebSocket-Version", "13")
r.Header.Set("Sec-WebSocket-Key", xrand.Base64(16))

_, err := Accept(w, r, nil)
assert.Contains(t, err, "failed to hijack connection")
})

t.Run("hijackerHTTPResponseControllerCompatibility", func(t *testing.T) {
t.Parallel()

rr := httptest.NewRecorder()
w := mockUnwrapper{
ResponseWriter: rr,
unwrap: func() http.ResponseWriter {
return mockHijacker{
ResponseWriter: rr,
hijack: func() (conn net.Conn, writer *bufio.ReadWriter, err error) {
return nil, nil, errors.New("haha")
},
}
},
}

_, _, err := http.NewResponseController(w).Hijack()

Check failure on line 189 in accept_test.go

View workflow job for this annotation

GitHub Actions / bench

undefined: http.NewResponseController

Check failure on line 189 in accept_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: http.NewResponseController

Check failure on line 189 in accept_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: http.NewResponseController

Check failure on line 189 in accept_test.go

View workflow job for this annotation

GitHub Actions / bench

undefined: http.NewResponseController
assert.Contains(t, err, "haha")
})

t.Run("closeRace", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -534,3 +581,14 @@ var _ http.Hijacker = mockHijacker{}
func (mj mockHijacker) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return mj.hijack()
}

type mockUnwrapper struct {
http.ResponseWriter
unwrap func() http.ResponseWriter
}

var _ rwUnwrapper = mockUnwrapper{}

func (mu mockUnwrapper) Unwrap() http.ResponseWriter {
return mu.unwrap()
}

0 comments on commit 230a06c

Please sign in to comment.