-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_request_decoder_test.go
More file actions
77 lines (50 loc) · 1.61 KB
/
http_request_decoder_test.go
File metadata and controls
77 lines (50 loc) · 1.61 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
package json_decoder
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestHTTPRequestDecoder_Decode(t *testing.T) {
t.Parallel()
data := []byte(`{}`)
r := newHTTPRequest(t, bytes.NewReader(data))
w := httptest.NewRecorder()
require.NoError(t, NewHTTPRequestDecoder(int64(len(data))).Decode(w, r, nil))
}
func TestHTTPRequestDecoder_Decode_ErrRequestEntityTooLarge(t *testing.T) {
t.Parallel()
t.Run("content length limit exceeded", func(t *testing.T) {
t.Parallel()
data := []byte(`{}`)
r := newHTTPRequest(t, bytes.NewReader(data))
w := httptest.NewRecorder()
require.ErrorIs(t, NewHTTPRequestDecoder(0).Decode(w, r, nil), ErrRequestEntityTooLarge)
})
t.Run("http.MaxBytesError", func(t *testing.T) {
t.Parallel()
data := []byte(`{}`)
r := newHTTPRequest(t, bytes.NewReader(data))
r.ContentLength = 0
r.Header.Set("Content-Length", "0")
w := httptest.NewRecorder()
require.ErrorIs(t, NewHTTPRequestDecoder(0).Decode(w, r, nil), ErrRequestEntityTooLarge)
})
}
func TestHTTPRequestDecoder_Decode_ErrUnsupportedMediaType(t *testing.T) {
t.Parallel()
data := []byte(`{}`)
r := newHTTPRequest(t, bytes.NewReader(data))
r.Header.Del("Content-Type")
w := httptest.NewRecorder()
require.ErrorIs(t, NewHTTPRequestDecoder(int64(len(data))).Decode(w, r, nil), ErrUnsupportedMediaType)
}
func newHTTPRequest(t *testing.T, body io.Reader) *http.Request {
t.Helper()
r, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "/", body)
require.NoError(t, err)
r.Header.Set("Content-Type", "application/json")
return r
}