-
Notifications
You must be signed in to change notification settings - Fork 7
/
io_test.go
180 lines (156 loc) · 4.79 KB
/
io_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
package utility
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNopWriteCloser(t *testing.T) {
wc := &myWriteCloser{}
nop := NopWriteCloser(wc)
nop.Close()
assert.False(t, wc.closed)
message := []byte("message")
_, err := nop.Write(message)
assert.Equal(t, message, wc.contents)
assert.NoError(t, err)
}
type myWriteCloser struct {
closed bool
contents []byte
}
func (w *myWriteCloser) Close() {
w.closed = true
}
func (w *myWriteCloser) Write(p []byte) (n int, err error) {
w.contents = append(w.contents, p...)
return 0, nil
}
func TestPaginatedReadCloser(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
t.Run("PaginatedRoute", func(t *testing.T) {
handler := &mockHandler{pages: 3}
server := httptest.NewServer(handler)
handler.baseURL = server.URL
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL, nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
var r io.ReadCloser
r = &paginatedReadCloser{
ctx: ctx,
client: http.DefaultClient,
respHeader: resp.Header,
ReadCloser: resp.Body,
}
data, err := ioutil.ReadAll(r)
require.NoError(t, err)
assert.Equal(t, "PAGINATED BODY PAGE 1\nPAGINATED BODY PAGE 2\nPAGINATED BODY PAGE 3\n", string(data))
assert.NoError(t, r.Close())
})
t.Run("PaginatedRouteWithReqHeader", func(t *testing.T) {
handler := &mockHandler{headerKeys: []string{"key1", "key2"}, pages: 3}
server := httptest.NewServer(handler)
handler.baseURL = server.URL
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL, nil)
require.NoError(t, err)
req.Header.Add("key1", "val1")
req.Header.Add("key2", "val2")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
var r io.ReadCloser
r = &paginatedReadCloser{
ctx: ctx,
client: http.DefaultClient,
reqHeader: req.Header,
respHeader: resp.Header,
ReadCloser: resp.Body,
}
data, err := ioutil.ReadAll(r)
require.NoError(t, err)
assert.Equal(t, "HEADER: key1:val1,key2:val2\nPAGINATED BODY PAGE 1\nHEADER: key1:val1,key2:val2\nPAGINATED BODY PAGE 2\nHEADER: key1:val1,key2:val2\nPAGINATED BODY PAGE 3\n", string(data))
assert.NoError(t, r.Close())
})
t.Run("NonPaginatedRoute", func(t *testing.T) {
handler := &mockHandler{}
server := httptest.NewServer(handler)
handler.baseURL = server.URL
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL, nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
var r io.ReadCloser
r = &paginatedReadCloser{
ctx: ctx,
client: http.DefaultClient,
respHeader: resp.Header,
ReadCloser: resp.Body,
}
data, err := ioutil.ReadAll(r)
require.NoError(t, err)
assert.Equal(t, "NON-PAGINATED BODY PAGE", string(data))
assert.NoError(t, r.Close())
})
t.Run("SplitPageByteSlice", func(t *testing.T) {
handler := &mockHandler{pages: 2}
server := httptest.NewServer(handler)
handler.baseURL = server.URL
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL, nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
var r io.ReadCloser
r = &paginatedReadCloser{
ctx: ctx,
client: http.DefaultClient,
respHeader: resp.Header,
ReadCloser: resp.Body,
}
p := make([]byte, 33) // 1.5X len of each page
n, err := r.Read(p)
require.NoError(t, err)
assert.Equal(t, len(p), n)
assert.Equal(t, "PAGINATED BODY PAGE 1\nPAGINATED B", string(p))
p = make([]byte, 33)
n, err = r.Read(p)
require.Equal(t, io.EOF, err)
assert.Equal(t, 11, n)
assert.Equal(t, "ODY PAGE 2\n", string(p[:11]))
assert.NoError(t, r.Close())
})
}
type mockHandler struct {
baseURL string
headerKeys []string
pages int
count int
}
func (h *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.pages > 0 {
if h.count <= h.pages-1 {
w.Header().Set("Link", fmt.Sprintf("<%s>; rel=\"%s\"", h.baseURL, "next"))
if len(h.headerKeys) > 0 {
var headerPairs []string
for _, headerKey := range h.headerKeys {
headerPairs = append(headerPairs, fmt.Sprintf("%s:%s", headerKey, r.Header.Get(headerKey)))
}
_, _ = w.Write([]byte(fmt.Sprintf("HEADER: %s\n", strings.Join(headerPairs, ","))))
}
_, _ = w.Write([]byte(fmt.Sprintf("PAGINATED BODY PAGE %d\n", h.count+1)))
}
h.count++
} else {
_, _ = w.Write([]byte("NON-PAGINATED BODY PAGE"))
}
}