You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
3 years ago
|
package response
|
||
4 years ago
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"net/http/httptest"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestWithCodeResponseWriter(t *testing.T) {
|
||
|
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
cw := &WithCodeResponseWriter{Writer: w}
|
||
|
|
||
|
cw.Header().Set("X-Test", "test")
|
||
|
cw.WriteHeader(http.StatusServiceUnavailable)
|
||
|
assert.Equal(t, cw.Code, http.StatusServiceUnavailable)
|
||
|
|
||
|
_, err := cw.Write([]byte("content"))
|
||
|
assert.Nil(t, err)
|
||
|
|
||
|
flusher, ok := http.ResponseWriter(cw).(http.Flusher)
|
||
4 years ago
|
assert.True(t, ok)
|
||
4 years ago
|
flusher.Flush()
|
||
|
})
|
||
|
|
||
|
resp := httptest.NewRecorder()
|
||
|
handler.ServeHTTP(resp, req)
|
||
|
assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
|
||
|
assert.Equal(t, "test", resp.Header().Get("X-Test"))
|
||
|
assert.Equal(t, "content", resp.Body.String())
|
||
|
}
|
||
3 years ago
|
|
||
|
func TestWithCodeResponseWriter_Hijack(t *testing.T) {
|
||
|
resp := httptest.NewRecorder()
|
||
|
writer := &WithCodeResponseWriter{
|
||
|
Writer: resp,
|
||
|
}
|
||
|
assert.NotPanics(t, func() {
|
||
|
writer.Hijack()
|
||
|
})
|
||
|
|
||
|
writer = &WithCodeResponseWriter{
|
||
|
Writer: mockedHijackable{resp},
|
||
|
}
|
||
|
assert.NotPanics(t, func() {
|
||
|
writer.Hijack()
|
||
|
})
|
||
|
}
|