Skip to content

Commit cee2d59

Browse files
committed
Add Content-Length & body length check
1 parent 90f5e30 commit cee2d59

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

cbsr_test.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,14 @@ import (
55
"fmt"
66
"net/http"
77
"net/http/httptest"
8+
"strconv"
89
"strings"
910
"testing"
1011
)
1112

1213
//go:embed testdata
1314
var testdata embed.FS
1415

15-
type test struct {
16-
Path string
17-
Method string
18-
AE string
19-
20-
Status int
21-
CE string
22-
Vary string
23-
}
24-
25-
const AE = "Accept-Encoding"
26-
2716
var methods = []string{"GET", "HEAD"}
2817
var paths = map[string]string{
2918
"testdata/static/js/2.js": "text/javascript; charset=UTF-8",
@@ -82,12 +71,24 @@ func TestOK(t *testing.T) {
8271
for acceptEncoding, contentEncoding := range encodings {
8372
t.Run(fmt.Sprintf("%s %s %s", method, path, acceptEncoding), func(t *testing.T) {
8473
r := httptest.NewRecorder()
74+
8575
mux.ServeHTTP(r, newRequest(t, method, srIndex[path], acceptEncoding))
76+
8677
assertStatus(t, http.StatusOK, r.Code)
8778
if r.Code == http.StatusOK {
8879
assertEqualFold(t, "Content-Type", contentType, r.Header().Get("Content-Type"))
8980
assertEqualFold(t, "Content-Encoding", contentEncoding, r.Header().Get("Content-Encoding"))
9081
assertEqualFold(t, "Vary", "Accept-Encoding", r.Header().Get("Vary"))
82+
switch method {
83+
case http.MethodGet:
84+
contentLength, err := strconv.Atoi(r.Header().Get("Content-Length"))
85+
if err != nil {
86+
t.Errorf("failed to parse Content-Length: %v", err)
87+
}
88+
assert(t, "body length", r.Body.Len(), contentLength)
89+
case http.MethodHead:
90+
assert(t, "body length", r.Body.Len(), 0)
91+
}
9192
}
9293
})
9394
}

0 commit comments

Comments
 (0)