-
Notifications
You must be signed in to change notification settings - Fork 817
/
Copy pathhttp_test.go
222 lines (198 loc) · 5.72 KB
/
http_test.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package util_test
import (
"bytes"
"context"
"html/template"
"io"
"math/rand"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
"github.com/cortexproject/cortex/pkg/cortexpb"
"github.com/cortexproject/cortex/pkg/util"
util_log "github.com/cortexproject/cortex/pkg/util/log"
)
func TestRenderHTTPResponse(t *testing.T) {
type testStruct struct {
Name string `json:"name"`
Value int `json:"value"`
}
tests := []struct {
name string
headers map[string]string
tmpl string
expectedOutput string
expectedContentType string
value testStruct
}{
{
name: "Test Renders json",
headers: map[string]string{
"Accept": "application/json",
},
tmpl: "<html></html>",
expectedOutput: `{"name":"testName","value":42}`,
expectedContentType: "application/json",
value: testStruct{
Name: "testName",
Value: 42,
},
},
{
name: "Test Renders html",
headers: map[string]string{},
tmpl: "<html>{{ .Name }}</html>",
expectedOutput: "<html>testName</html>",
expectedContentType: "text/html; charset=utf-8",
value: testStruct{
Name: "testName",
Value: 42,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpl := template.Must(template.New("webpage").Parse(tt.tmpl))
writer := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", nil)
for k, v := range tt.headers {
request.Header.Add(k, v)
}
util.RenderHTTPResponse(writer, tt.value, tmpl, request)
assert.Equal(t, tt.expectedContentType, writer.Header().Get("Content-Type"))
assert.Equal(t, 200, writer.Code)
assert.Equal(t, tt.expectedOutput, writer.Body.String())
})
}
}
func TestWriteTextResponse(t *testing.T) {
w := httptest.NewRecorder()
util.WriteTextResponse(w, "hello world")
assert.Equal(t, 200, w.Code)
assert.Equal(t, "hello world", w.Body.String())
assert.Equal(t, "text/plain", w.Header().Get("Content-Type"))
}
func TestStreamWriteYAMLResponse(t *testing.T) {
type testStruct struct {
Name string `yaml:"name"`
Value int `yaml:"value"`
}
tt := struct {
name string
headers map[string]string
expectedOutput string
expectedContentType string
value map[string]*testStruct
}{
name: "Test Stream Render YAML",
headers: map[string]string{
"Content-Type": "application/yaml",
},
expectedContentType: "application/yaml",
value: make(map[string]*testStruct),
}
// Generate some data to serialize.
for i := 0; i < rand.Intn(100)+1; i++ {
ts := testStruct{
Name: "testName" + strconv.Itoa(i),
Value: i,
}
tt.value[ts.Name] = &ts
}
d, err := yaml.Marshal(tt.value)
require.NoError(t, err)
tt.expectedOutput = string(d)
w := httptest.NewRecorder()
done := make(chan struct{})
iter := make(chan interface{})
go func() {
util.StreamWriteYAMLResponse(w, iter, util_log.Logger)
close(done)
}()
for k, v := range tt.value {
iter <- map[string]*testStruct{k: v}
}
close(iter)
<-done
assert.Equal(t, tt.expectedContentType, w.Header().Get("Content-Type"))
assert.Equal(t, 200, w.Code)
assert.YAMLEq(t, tt.expectedOutput, w.Body.String())
}
func TestParseProtoReader(t *testing.T) {
// 47 bytes compressed and 53 uncompressed
req := &cortexpb.PreallocWriteRequest{
WriteRequest: cortexpb.WriteRequest{
Timeseries: []cortexpb.PreallocTimeseries{
{
TimeSeries: &cortexpb.TimeSeries{
Labels: []cortexpb.LabelAdapter{
{Name: "foo", Value: "bar"},
},
Samples: []cortexpb.Sample{
{Value: 10, TimestampMs: 1},
{Value: 20, TimestampMs: 2},
{Value: 30, TimestampMs: 3},
},
Exemplars: []cortexpb.Exemplar{},
Histograms: []cortexpb.Histogram{},
},
},
},
},
}
for _, tt := range []struct {
name string
compression util.CompressionType
maxSize int
expectErr bool
useBytesBuffer bool
}{
{"rawSnappy", util.RawSnappy, 53, false, false},
{"noCompression", util.NoCompression, 53, false, false},
{"too big rawSnappy", util.RawSnappy, 10, true, false},
{"too big decoded rawSnappy", util.RawSnappy, 50, true, false},
{"too big noCompression", util.NoCompression, 10, true, false},
{"bytesbuffer rawSnappy", util.RawSnappy, 53, false, true},
{"bytesbuffer noCompression", util.NoCompression, 53, false, true},
{"bytesbuffer too big rawSnappy", util.RawSnappy, 10, true, true},
{"bytesbuffer too big decoded rawSnappy", util.RawSnappy, 50, true, true},
{"bytesbuffer too big noCompression", util.NoCompression, 10, true, true},
} {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
assert.Nil(t, util.SerializeProtoResponse(w, req, tt.compression))
var fromWire cortexpb.PreallocWriteRequest
reader := w.Result().Body
if tt.useBytesBuffer {
buf := bytes.Buffer{}
_, err := buf.ReadFrom(reader)
assert.Nil(t, err)
reader = bytesBuffered{Buffer: &buf}
}
_, err := util.ParseProtoReader(context.Background(), reader, 0, tt.maxSize, &fromWire, tt.compression)
if tt.expectErr {
assert.NotNil(t, err)
return
}
assert.Nil(t, err)
assert.Equal(t, req, &fromWire)
})
}
}
type bytesBuffered struct {
*bytes.Buffer
}
func (b bytesBuffered) Close() error {
return nil
}
func (b bytesBuffered) BytesBuffer() *bytes.Buffer {
return b.Buffer
}
func TestIsRequestBodyTooLargeRegression(t *testing.T) {
_, err := io.ReadAll(http.MaxBytesReader(httptest.NewRecorder(), io.NopCloser(bytes.NewReader([]byte{1, 2, 3, 4})), 1))
assert.True(t, util.IsRequestBodyTooLarge(err))
}