-
Notifications
You must be signed in to change notification settings - Fork 53
/
request_test.go
221 lines (207 loc) · 5.12 KB
/
request_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
package http
import (
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"testing"
)
func TestRequestRewindable(t *testing.T) {
cases := map[string]struct {
Stream io.Reader
ExpectErr string
}{
"rewindable": {
Stream: bytes.NewReader([]byte{}),
},
"empty not rewindable": {
Stream: bytes.NewBuffer([]byte{}),
// ExpectErr: "stream is not seekable",
},
"not empty not rewindable": {
Stream: bytes.NewBuffer([]byte("abc123")),
ExpectErr: "stream is not seekable",
},
"nil stream": {},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
req := NewStackRequest().(*Request)
req, err := req.SetStream(c.Stream)
if err != nil {
t.Fatalf("expect no error setting stream, %v", err)
}
err = req.RewindStream()
if len(c.ExpectErr) != 0 {
if err == nil {
t.Fatalf("expect error, got none")
}
if e, a := c.ExpectErr, err.Error(); !strings.Contains(a, e) {
t.Fatalf("expect error to contain %v, got %v", e, a)
}
return
}
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
})
}
}
func TestRequestBuild_contentLength(t *testing.T) {
cases := []struct {
Request *Request
Expected int64
}{
{
Request: &Request{
Request: &http.Request{
ContentLength: 100,
},
},
Expected: 100,
},
{
Request: &Request{
Request: &http.Request{
ContentLength: -1,
},
},
Expected: 0,
},
{
Request: &Request{
Request: &http.Request{
ContentLength: 100,
},
stream: bytes.NewReader(make([]byte, 100)),
},
Expected: 100,
},
{
Request: &Request{
Request: &http.Request{
ContentLength: 100,
},
stream: http.NoBody,
},
Expected: 100,
},
}
for i, tt := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
build := tt.Request.Build(context.Background())
if build.ContentLength != tt.Expected {
t.Errorf("expect %v, got %v", tt.Expected, build.ContentLength)
}
})
}
}
func TestRequestSetStream(t *testing.T) {
cases := map[string]struct {
reader io.Reader
expectSeekable bool
expectStreamStartPos int64
expectContentLength int64
expectNilStream bool
expectNilBody bool
expectReqContentLength int64
}{
"nil stream": {
expectNilStream: true,
expectNilBody: true,
},
"empty unseekable stream": {
reader: bytes.NewBuffer([]byte{}),
expectNilStream: true,
expectNilBody: true,
},
"empty seekable stream": {
reader: bytes.NewReader([]byte{}),
expectContentLength: 0,
expectSeekable: true,
expectNilStream: false,
expectNilBody: true,
},
"unseekable no len stream": {
reader: ioutil.NopCloser(bytes.NewBuffer([]byte("abc123"))),
expectContentLength: -1,
expectNilStream: false,
expectNilBody: false,
expectReqContentLength: -1,
},
"unseekable stream": {
reader: bytes.NewBuffer([]byte("abc123")),
expectContentLength: 6,
expectNilStream: false,
expectNilBody: false,
expectReqContentLength: 6,
},
"seekable stream": {
reader: bytes.NewReader([]byte("abc123")),
expectContentLength: 6,
expectNilStream: false,
expectSeekable: true,
expectNilBody: false,
expectReqContentLength: 6,
},
"offset seekable stream": {
reader: func() io.Reader {
r := bytes.NewReader([]byte("abc123"))
_, _ = r.Seek(1, os.SEEK_SET)
return r
}(),
expectStreamStartPos: 1,
expectContentLength: 5,
expectSeekable: true,
expectNilStream: false,
expectNilBody: false,
expectReqContentLength: 5,
},
"NoBody stream": {
reader: http.NoBody,
expectNilStream: true,
expectNilBody: true,
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
var err error
req := NewStackRequest().(*Request)
req, err = req.SetStream(c.reader)
if err != nil {
t.Fatalf("expect not error, got %v", err)
}
if e, a := c.expectSeekable, req.IsStreamSeekable(); e != a {
t.Errorf("expect %v seekable, got %v", e, a)
}
if e, a := c.expectStreamStartPos, req.streamStartPos; e != a {
t.Errorf("expect %v seek start position, got %v", e, a)
}
if e, a := c.expectNilStream, req.stream == nil; e != a {
t.Errorf("expect %v nil stream, got %v", e, a)
}
if l, ok, err := req.StreamLength(); err != nil {
t.Fatalf("expect no stream length error, got %v", err)
} else if ok {
req.ContentLength = l
}
if e, a := c.expectContentLength, req.ContentLength; e != a {
t.Errorf("expect %v content-length, got %v", e, a)
}
if e, a := c.expectStreamStartPos, req.streamStartPos; e != a {
t.Errorf("expect %v streamStartPos, got %v", e, a)
}
r := req.Build(context.Background())
if e, a := c.expectNilBody, r.Body == nil; e != a {
t.Errorf("expect %v request nil body, got %v", e, a)
}
if e, a := c.expectContentLength, req.ContentLength; e != a {
t.Errorf("expect %v request content-length, got %v", e, a)
}
})
}
}