1 package testing
2
3 import (
4 "fmt"
5 "net/http"
6 "strings"
7 )
8
9
10
11 func HasHeader(expect, actual http.Header) error {
12 var errs errors
13 for key, es := range expect {
14 as := actual.Values(key)
15 if len(as) == 0 {
16 errs = append(errs, fmt.Errorf("expect %v header in %v",
17 key, actual))
18 continue
19 }
20
21
22
23
24 e := strings.Join(es, ", ")
25 a := strings.Join(as, ", ")
26 if e != a {
27 errs = append(errs, fmt.Errorf("expect %v=%v to match %v",
28 key, e, a))
29 continue
30 }
31 }
32
33 return errs
34 }
35
36
37
38
39 func AssertHasHeader(t T, expect, actual http.Header) bool {
40 t.Helper()
41
42 if err := HasHeader(expect, actual); err != nil {
43 for _, e := range err.(errors) {
44 t.Error(e)
45 }
46 return false
47 }
48 return true
49 }
50
51
52
53 func HasHeaderKeys(keys []string, actual http.Header) error {
54 var errs errors
55 for _, key := range keys {
56 if vs := actual.Values(key); len(vs) == 0 {
57 errs = append(errs, fmt.Errorf("expect %v key in %v", key, actual))
58 continue
59 }
60 }
61 return errs
62 }
63
64
65
66 func AssertHasHeaderKeys(t T, keys []string, actual http.Header) bool {
67 t.Helper()
68
69 if err := HasHeaderKeys(keys, actual); err != nil {
70 for _, e := range err.(errors) {
71 t.Error(e)
72 }
73 return false
74 }
75 return true
76 }
77
78
79
80 func NotHaveHeaderKeys(keys []string, actual http.Header) error {
81 var errs errors
82 for _, k := range keys {
83 if vs := actual.Values(k); len(vs) != 0 {
84 errs = append(errs, fmt.Errorf("expect %v key not in %v", k, actual))
85 continue
86 }
87 }
88 return errs
89 }
90
91
92
93
94 func AssertNotHaveHeaderKeys(t T, keys []string, actual http.Header) bool {
95 t.Helper()
96
97 if err := NotHaveHeaderKeys(keys, actual); err != nil {
98 for _, e := range err.(errors) {
99 t.Error(e)
100 }
101 return false
102 }
103 return true
104 }
105
106
107
108 type QueryItem struct {
109 Key string
110 Value string
111 }
112
113
114
115
116
117
118 func ParseRawQuery(rawQuery string) (items []QueryItem) {
119 for _, item := range strings.Split(rawQuery, `&`) {
120 parts := strings.SplitN(item, `=`, 2)
121 var value string
122 if len(parts) > 1 {
123 value = parts[1]
124 }
125 items = append(items, QueryItem{
126
127
128 Key: strings.ReplaceAll(parts[0], `+`, `%20`),
129 Value: strings.ReplaceAll(value, `+`, `%20`),
130 })
131 }
132 return items
133 }
134
135
136
137
138 func HasQuery(expect, actual []QueryItem) error {
139 var errs errors
140 for _, item := range expect {
141 var found bool
142 for _, v := range actual {
143 if item.Key == v.Key && item.Value == v.Value {
144 found = true
145 break
146 }
147 }
148 if !found {
149 errs = append(errs, fmt.Errorf("expect %v query item in %v", item, actual))
150 }
151 }
152 return errs
153 }
154
155
156
157
158 func AssertHasQuery(t T, expect, actual []QueryItem) bool {
159 t.Helper()
160
161 if err := HasQuery(expect, actual); err != nil {
162 for _, e := range err.(errors) {
163 t.Error(e.Error())
164 }
165 return false
166 }
167 return true
168 }
169
170
171
172 func HasQueryKeys(keys []string, actual []QueryItem) error {
173 var errs errors
174 for _, key := range keys {
175 var found bool
176 for _, v := range actual {
177 if key == v.Key {
178 found = true
179 break
180 }
181 }
182 if !found {
183 errs = append(errs, fmt.Errorf("expect %v query key in %v", key, actual))
184 }
185 }
186 return errs
187 }
188
189
190
191 func AssertHasQueryKeys(t T, keys []string, actual []QueryItem) bool {
192 t.Helper()
193
194 if err := HasQueryKeys(keys, actual); err != nil {
195 for _, e := range err.(errors) {
196 t.Error(e)
197 }
198 return false
199 }
200 return true
201 }
202
203
204
205 func NotHaveQueryKeys(keys []string, actual []QueryItem) error {
206 var errs errors
207 for _, key := range keys {
208 for _, v := range actual {
209 if key == v.Key {
210 errs = append(errs, fmt.Errorf("expect %v query key not in %v",
211 key, actual))
212 continue
213 }
214 }
215 }
216 return errs
217 }
218
219
220
221 func AssertNotHaveQueryKeys(t T, keys []string, actual []QueryItem) bool {
222 t.Helper()
223
224 if err := NotHaveQueryKeys(keys, actual); err != nil {
225 for _, e := range err.(errors) {
226 t.Error(e)
227 }
228 return false
229 }
230 return true
231 }
232
View as plain text