-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_test.go
More file actions
205 lines (188 loc) · 5.89 KB
/
Copy pathhttp_test.go
File metadata and controls
205 lines (188 loc) · 5.89 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
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
package clicky
import (
"net/http/httptest"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = ginkgo.Describe("WithHttpRequest", func() {
ginkgo.Context("when format specified via query param", func() {
tests := []struct {
name string
url string
expected string
}{
{"json query param", "http://example.com/api/users?format=json", "json"},
{"yaml query param", "http://example.com/api/users?format=yaml", "yaml"},
{"csv query param", "http://example.com/api/users?format=csv", "csv"},
{"html query param", "http://example.com/api/users?format=html", "html"},
{"markdown query param", "http://example.com/api/users?format=markdown", "markdown"},
}
for _, tt := range tests {
tt := tt
ginkgo.It(tt.name, func() {
req := httptest.NewRequest("GET", tt.url, nil)
opts := WithHttpRequest(req)
Expect(opts.Format).To(Equal(tt.expected))
})
}
})
ginkgo.Context("when format specified via path extension", func() {
tests := []struct {
name string
url string
expected string
}{
{"json extension", "http://example.com/api/users.json", "json"},
{"yaml extension", "http://example.com/api/users.yaml", "yaml"},
{"yml extension", "http://example.com/api/users.yml", "yaml"},
{"csv extension", "http://example.com/api/users.csv", "csv"},
{"html extension", "http://example.com/api/users.html", "html"},
{"markdown extension", "http://example.com/api/users.md", "markdown"},
}
for _, tt := range tests {
tt := tt
ginkgo.It(tt.name, func() {
req := httptest.NewRequest("GET", tt.url, nil)
opts := WithHttpRequest(req)
Expect(opts.Format).To(Equal(tt.expected))
})
}
})
ginkgo.Context("when format specified via Accept header", func() {
tests := []struct {
name string
accept string
expected string
}{
{"json accept", "application/json", "json"},
{"yaml accept", "application/yaml", "yaml"},
{"text yaml accept", "text/yaml", "yaml"},
{"csv accept", "text/csv", "csv"},
{"html accept", "text/html", "html"},
{"markdown accept", "text/markdown", "markdown"},
{"plain text accept", "text/plain", "pretty"},
{"wildcard accept", "*/*", "json"},
}
for _, tt := range tests {
tt := tt
ginkgo.It(tt.name, func() {
req := httptest.NewRequest("GET", "http://example.com/api/users", nil)
req.Header.Set("Accept", tt.accept)
opts := WithHttpRequest(req)
Expect(opts.Format).To(Equal(tt.expected))
})
}
})
ginkgo.Context("when determining format priority", func() {
ginkgo.It("should prioritize query param over path extension", func() {
req := httptest.NewRequest("GET", "http://example.com/api/users.yaml?format=json", nil)
req.Header.Set("Accept", "text/csv")
opts := WithHttpRequest(req)
Expect(opts.Format).To(Equal("json"))
})
ginkgo.It("should prioritize path extension over Accept header", func() {
req := httptest.NewRequest("GET", "http://example.com/api/users.csv", nil)
req.Header.Set("Accept", "application/json")
opts := WithHttpRequest(req)
Expect(opts.Format).To(Equal("csv"))
})
ginkgo.It("should use Accept header when no query param or extension", func() {
req := httptest.NewRequest("GET", "http://example.com/api/users", nil)
req.Header.Set("Accept", "text/yaml")
opts := WithHttpRequest(req)
Expect(opts.Format).To(Equal("yaml"))
})
})
ginkgo.Context("when handling paging parameters", func() {
tests := []struct {
name string
url string
headers map[string]string
expectedPage int
expectedLimit int
}{
{
name: "query params",
url: "http://example.com/api/users?page=2&limit=50",
expectedPage: 2,
expectedLimit: 50,
},
{
name: "headers",
url: "http://example.com/api/users",
headers: map[string]string{
"X-Page": "3",
"X-Limit": "100",
},
expectedPage: 3,
expectedLimit: 100,
},
{
name: "query overrides headers",
url: "http://example.com/api/users?page=5",
headers: map[string]string{
"X-Page": "1",
"X-Limit": "20",
},
expectedPage: 5,
expectedLimit: 20,
},
{
name: "no paging",
url: "http://example.com/api/users",
expectedPage: 0,
expectedLimit: 0,
},
}
for _, tt := range tests {
tt := tt
ginkgo.It(tt.name, func() {
req := httptest.NewRequest("GET", tt.url, nil)
for key, val := range tt.headers {
req.Header.Set(key, val)
}
opts := WithHttpRequest(req)
Expect(opts.Page).To(Equal(tt.expectedPage))
Expect(opts.Limit).To(Equal(tt.expectedLimit))
})
}
})
ginkgo.Context("when no format is specified", func() {
ginkgo.It("should default to json", func() {
req := httptest.NewRequest("GET", "http://example.com/api/users", nil)
opts := WithHttpRequest(req)
Expect(opts.Format).To(Equal("json"))
})
})
ginkgo.Context("when Accept header has multiple values", func() {
ginkgo.It("should pick first recognized format", func() {
req := httptest.NewRequest("GET", "http://example.com/api/users", nil)
req.Header.Set("Accept", "text/html,application/json;q=0.9,*/*;q=0.8")
opts := WithHttpRequest(req)
Expect(opts.Format).To(Equal("html"))
})
})
})
var _ = ginkgo.Describe("FormatToContentType", func() {
tests := []struct {
format string
expected string
}{
{"json", "application/json"},
{"yaml", "application/yaml"},
{"csv", "text/csv"},
{"html", "text/html; charset=utf-8"},
{"markdown", "text/markdown; charset=utf-8"},
{"pdf", "application/pdf"},
{"excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{"pretty", "text/plain; charset=utf-8"},
{"unknown", "application/octet-stream"},
}
for _, tt := range tests {
tt := tt
ginkgo.It("should convert "+tt.format+" to correct content type", func() {
result := FormatToContentType(tt.format)
Expect(result).To(Equal(tt.expected))
})
}
})