Repository navigation
Expand file tree
/
Copy pathcompression_test.go
More file actions
78 lines (75 loc) · 2.35 KB
/
Copy pathcompression_test.go
File metadata and controls
78 lines (75 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"bytes"
"compress/gzip"
"fmt"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
)
func TestValidateCompressedBody(t *testing.T) {
compress := func(body string) string {
t.Helper()
var buffer bytes.Buffer
writer := gzip.NewWriter(&buffer)
if _, err := writer.Write([]byte(body)); err != nil {
t.Fatal(err)
}
if err := writer.Close(); err != nil {
t.Fatal(err)
}
return buffer.String()
}
valid := loadTestdata(t, "valid.publiccode.yml")
compressed := compress(valid)
layered := valid
for range 6 {
layered = compress(layered)
}
for _, method := range []string{http.MethodPost, "QUERY"} {
for _, tc := range []struct {
name, encoding, body string
status int
}{
{"malformed", "gzip", "not gzip", 400},
{"truncated", "gzip", compressed[:len(compressed)-5], 400},
{"oversized", "gzip", compress(strings.Repeat("a", 6_000_000)), 413},
{"unsupported", "unknown", valid, 415},
{"too many codings", "gzip, gzip, gzip, gzip, gzip, gzip", layered, 415},
{"unimplemented", "compress", valid, 501},
{"valid", "gzip", compressed, 200},
{"stacked", "gzip, gzip", compress(compressed), 200},
{"x-gzip alias", "x-gzip", compressed, 200},
{"brotli alias", "brotli", string(fasthttp.AppendBrotliBytes(nil, []byte(valid))), 200},
{"identity", "gzip, identity", compressed, 200},
} {
contentType := "application/problem+json"
if tc.status == 200 {
contentType = "application/json; charset=utf-8"
}
runTestCases(t, []TestCase{{
description: method + "/" + tc.name,
query: method + " /v1/validate",
body: tc.body,
headers: map[string][]string{"Content-Encoding": {tc.encoding}},
expectedCode: tc.status,
expectedContentType: contentType,
validateFunc: func(t *testing.T, response map[string]any) {
if tc.status == 200 {
assert.Equal(t, true, response["valid"])
assert.Empty(t, response["results"])
assert.NotEmpty(t, response["normalized"])
} else {
assert.Equal(t, float64(tc.status), response["status"])
assert.NotEmpty(t, response["title"])
for _, key := range []string{"valid", "results", "normalized"} {
assert.NotContains(t, response, key, fmt.Sprint(response))
}
}
},
}})
}
}
}