Merge v2.4 into master
This commit is contained in:
commit
d211437d6c
80 changed files with 430 additions and 100 deletions
|
@ -284,9 +284,9 @@ func TestIntegrationShouldCompress(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func generateBytes(len int) []byte {
|
||||
func generateBytes(length int) []byte {
|
||||
var value []byte
|
||||
for i := 0; i < len; i++ {
|
||||
for i := 0; i < length; i++ {
|
||||
value = append(value, 0x61+byte(i))
|
||||
}
|
||||
return value
|
||||
|
|
|
@ -29,6 +29,7 @@ func TestHandler(t *testing.T) {
|
|||
fmt.Fprintln(w, "My error page.")
|
||||
}),
|
||||
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
||||
t.Helper()
|
||||
assert.Equal(t, http.StatusOK, recorder.Code, "HTTP status")
|
||||
assert.Contains(t, recorder.Body.String(), http.StatusText(http.StatusOK))
|
||||
},
|
||||
|
@ -41,6 +42,7 @@ func TestHandler(t *testing.T) {
|
|||
fmt.Fprintln(w, "My error page.")
|
||||
}),
|
||||
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
||||
t.Helper()
|
||||
assert.Equal(t, http.StatusPartialContent, recorder.Code, "HTTP status")
|
||||
assert.Contains(t, recorder.Body.String(), http.StatusText(http.StatusPartialContent))
|
||||
},
|
||||
|
@ -53,6 +55,7 @@ func TestHandler(t *testing.T) {
|
|||
fmt.Fprintln(w, "whatever, should not be called")
|
||||
}),
|
||||
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
||||
t.Helper()
|
||||
assert.Equal(t, http.StatusNotModified, recorder.Code, "HTTP status")
|
||||
assert.Contains(t, recorder.Body.String(), "")
|
||||
},
|
||||
|
@ -65,6 +68,7 @@ func TestHandler(t *testing.T) {
|
|||
fmt.Fprintln(w, "My error page.")
|
||||
}),
|
||||
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
||||
t.Helper()
|
||||
assert.Equal(t, http.StatusInternalServerError, recorder.Code, "HTTP status")
|
||||
assert.Contains(t, recorder.Body.String(), "My error page.")
|
||||
assert.NotContains(t, recorder.Body.String(), "oops", "Should not return the oops page")
|
||||
|
@ -78,6 +82,7 @@ func TestHandler(t *testing.T) {
|
|||
fmt.Fprintln(w, "My error page.")
|
||||
}),
|
||||
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
||||
t.Helper()
|
||||
assert.Equal(t, http.StatusBadGateway, recorder.Code, "HTTP status")
|
||||
assert.Contains(t, recorder.Body.String(), http.StatusText(http.StatusBadGateway))
|
||||
assert.NotContains(t, recorder.Body.String(), "Test Server", "Should return the oops page since we have not configured the 502 code")
|
||||
|
@ -95,6 +100,7 @@ func TestHandler(t *testing.T) {
|
|||
}
|
||||
}),
|
||||
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
||||
t.Helper()
|
||||
assert.Equal(t, http.StatusServiceUnavailable, recorder.Code, "HTTP status")
|
||||
assert.Contains(t, recorder.Body.String(), "My 503 page.")
|
||||
assert.NotContains(t, recorder.Body.String(), "oops", "Should not return the oops page")
|
||||
|
@ -112,6 +118,7 @@ func TestHandler(t *testing.T) {
|
|||
}
|
||||
}),
|
||||
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
||||
t.Helper()
|
||||
assert.Equal(t, http.StatusServiceUnavailable, recorder.Code, "HTTP status")
|
||||
assert.Contains(t, recorder.Body.String(), "My 503 page.")
|
||||
assert.NotContains(t, recorder.Body.String(), "oops", "Should not return the oops page")
|
||||
|
|
|
@ -104,9 +104,10 @@ func isWebsocketRequest(req *http.Request) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
h = h[pos:]
|
||||
h = h[pos+1:]
|
||||
}
|
||||
}
|
||||
|
||||
return containsHeader(connection, "upgrade") && containsHeader(upgrade, "websocket")
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package forwardedheaders
|
|||
import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -299,3 +300,71 @@ func TestServeHTTP(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_isWebsocketRequest(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
connectionHeader string
|
||||
upgradeHeader string
|
||||
assert assert.BoolAssertionFunc
|
||||
}{
|
||||
{
|
||||
desc: "connection Header multiple values middle",
|
||||
connectionHeader: "foo,upgrade,bar",
|
||||
upgradeHeader: "websocket",
|
||||
assert: assert.True,
|
||||
},
|
||||
{
|
||||
desc: "connection Header multiple values end",
|
||||
connectionHeader: "foo,bar,upgrade",
|
||||
upgradeHeader: "websocket",
|
||||
assert: assert.True,
|
||||
},
|
||||
{
|
||||
desc: "connection Header multiple values begin",
|
||||
connectionHeader: "upgrade,foo,bar",
|
||||
upgradeHeader: "websocket",
|
||||
assert: assert.True,
|
||||
},
|
||||
{
|
||||
desc: "connection Header no upgrade",
|
||||
connectionHeader: "foo,bar",
|
||||
upgradeHeader: "websocket",
|
||||
assert: assert.False,
|
||||
},
|
||||
{
|
||||
desc: "connection Header empty",
|
||||
connectionHeader: "",
|
||||
upgradeHeader: "websocket",
|
||||
assert: assert.False,
|
||||
},
|
||||
{
|
||||
desc: "no header values",
|
||||
connectionHeader: "foo,bar",
|
||||
upgradeHeader: "foo,bar",
|
||||
assert: assert.False,
|
||||
},
|
||||
{
|
||||
desc: "upgrade header multiple values",
|
||||
connectionHeader: "upgrade",
|
||||
upgradeHeader: "foo,bar,websocket",
|
||||
assert: assert.True,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
|
||||
req.Header.Set(connection, test.connectionHeader)
|
||||
req.Header.Set(upgrade, test.upgradeHeader)
|
||||
|
||||
ok := isWebsocketRequest(req)
|
||||
|
||||
test.assert(t, ok)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue