Debian Codegraph @

decode_test.go

raw symbols SCIP
1 package httpheader
2
3 import (
4 "fmt"
5 "net/http"
6 "net/textproto"
7 "reflect"
8 "regexp"
9 "sort"
10 "testing"
11 "time"
12 )
13
14 // Event defines a Google Calendar hook event type
15 type Event string
16
17 // GoogleCalendar hook types
18 const (
19 SyncEvent Event = "sync"
20 ExistsEvent Event = "exists"
21 NotExistsEvent Event = "not_exists"
22 )
23
24 // GoogleCalendarPayload a google calendar notice
25 // https://developers.google.com/calendar/v3/push
26 type GoogleCalendarPayload struct {
27 ChannelID string `header:"X-Goog-Channel-ID"`
28 ChannelToken string `header:"X-Goog-Channel-Token,omitempty"`
29 ChannelExpiration time.Time `header:"X-Goog-Channel-Expiration,omitempty"`
30 ResourceID string `header:"X-Goog-Resource-ID"`
31 ResourceURI string `header:"X-Goog-Resource-URI"`
32 ResourceState string `header:"X-Goog-Resource-State"`
33 MessageNumber int `header:"X-Goog-Message-Number"`
34 }
35
36 func getHeader(e Event) http.Header {
37 h := http.Header{}
38 h.Add("X-Goog-Channel-ID", "channel-ID-value")
39 h.Add("X-Goog-Channel-Token", "channel-token-value")
40 h.Add("X-Goog-Channel-Expiration", "Tue, 19 Nov 2013 01:13:52 GMT")
41 h.Add("X-Goog-Resource-ID", "identifier-for-the-watched-resource")
42 h.Add("X-Goog-Resource-URI", "version-specific-URI-of-the-watched-resource")
43 h.Add("X-Goog-Message-Number", "1")
44 h.Add("X-Goog-Resource-State", string(e))
45 return h
46 }
47
48 func TestDecodeHeader(t *testing.T) {
49 type args struct {
50 e Event
51 }
52 tests := []struct {
53 name string
54 args args
55 wantErr bool
56 }{
57 {"Google Calendar sync", args{SyncEvent}, false},
58 {"Google Calendar exists", args{ExistsEvent}, false},
59 {"Google Calendar no exists", args{NotExistsEvent}, false},
60 }
61
62 for i, tt := range tests {
63 t.Run(tt.name, func(t *testing.T) {
64 plrun := GoogleCalendarPayload{
65 ChannelID: "channel-ID-value",
66 ChannelToken: "channel-token-value",
67 ResourceID: "identifier-for-the-watched-resource",
68 ResourceURI: "version-specific-URI-of-the-watched-resource",
69 MessageNumber: 1,
70 }
71 plrun.ChannelExpiration, _ = time.Parse(http.TimeFormat, "Tue, 19 Nov 2013 01:13:52 GMT")
72 plrun.ResourceState = string(tt.args.e)
73 gcp := GoogleCalendarPayload{}
74 err := Decode(getHeader(tt.args.e), &gcp)
75 if (err != nil) != tt.wantErr {
76 t.Errorf("%d. Decode() error = %+v, wantErr %+v", i, err, tt.wantErr)
77 }
78 if !reflect.DeepEqual(gcp, plrun) {
79 t.Errorf("%d. Decode() does not work as expected, \ngot %+v \nwant %+v", i, gcp, plrun)
80 }
81 })
82 }
83 }
84
85 type DecodedArgs []string
86
87 func (m *DecodedArgs) DecodeHeader(header http.Header, key string) error {
88 baseKey := textproto.CanonicalMIMEHeaderKey(key)
89 keyMatch := regexp.MustCompile(fmt.Sprintf(`^%s\.\d+$`, baseKey))
90 var args DecodedArgs
91 for k := range header {
92 if keyMatch.MatchString(textproto.CanonicalMIMEHeaderKey(k)) {
93 args = append(args, header.Get(k))
94 }
95 }
96 // TODO: sort args by id
97 sort.Strings(args)
98 if len(args) > 0 {
99 *m = args
100 }
101 return nil
102 }
103
104 func TestDecodeHeader_Unmarshaler(t *testing.T) {
105 type ArgStruct struct {
106 Args DecodedArgs `header:"Arg"`
107 }
108 input := http.Header{
109 "Arg.0": []string{"a"},
110 "Arg.1": []string{"b"},
111 "Arg.2": []string{"c"},
112 }
113 want := ArgStruct{
114 Args: []string{"a", "b", "c"},
115 }
116 var got ArgStruct
117
118 err := Decode(input, &got)
119 if err != nil {
120 t.Errorf("want no error, got error: %#v", err)
121 }
122 if !reflect.DeepEqual(want, got) {
123 t.Errorf("Decode returned %#v, want %#v", got, want)
124 }
125 }
126
127 func TestDecodeHeader_UnmarshalerWithNilPointer(t *testing.T) {
128 s := struct {
129 Args *EncodedArgs `header:"Arg"`
130 }{}
131 err := Decode(http.Header{}, s)
132 if err == nil {
133 t.Error("want error but got nil")
134 }
135 }
136
137 type simpleStruct struct {
138 Foo string
139 Bar int
140 }
141
142 type fullTypeStruct struct {
143 unExport string
144 UnExportTwo string `header:"-"`
145 Bool bool `header:"Bool"`
146 BoolInt bool `header:"Bool-Int,int"`
147 String string
148 StringEmpty string `header:"String-Empty"`
149 StringEmptyIgnore string `header:"String-Empty-Ignore,omitempty"`
150 Uint uint
151 Uint64 uint64
152 Uint8 uint8
153 Uint16 uint16
154 Uint32 uint32
155 Int int
156 Int64 int64
157 Int8 int8
158 Int16 int16
159 Int32 int32
160 Float32 float32
161 Float64 float64
162 Slice []string
163 SliceTwo []int `header:"Slice-Two"`
164 Array [3]string
165 ArrayTwo [2]int `header:"Array-Two"`
166 Interface interface{}
167 Time time.Time
168 TimeUnix time.Time `header:"Time-Unix,unix"`
169 // Point *string
170 Args DecodedArgs `header:"Arg"`
171 Foo simpleStruct
172 }
173
174 func TestDecodeHeader_more_data_type(t *testing.T) {
175 timeV := time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)
176 timeS := "Sat, 01 Jan 2000 12:34:56 GMT"
177 timeU := "946730096"
178 h := http.Header{
179 "UnExportTwo": []string{"foo"},
180 "UnExport-Two": []string{"foo"},
181 "Bool": []string{"true"},
182 "Bool-Int": []string{"1"},
183 "String": []string{"foobar"},
184 "String-Empty": []string{""},
185 "Uint": []string{"2"},
186 "Uint64": []string{"3"},
187 "Uint8": []string{"4"},
188 "Uint16": []string{"5"},
189 "Uint32": []string{"6"},
190 "Int": []string{"7"},
191 "Int64": []string{"8"},
192 "Int8": []string{"9"},
193 "Int16": []string{"10"},
194 "Int32": []string{"11"},
195 "Float32": []string{"12.2"},
196 "Float64": []string{"13.2"},
197 "Slice": []string{"a", "b", "c"},
198 "Slice-Two": []string{"1", "2", "3"},
199 "Array": []string{"a", "b", "c"},
200 "Array-Two": []string{"1", "2", "3"},
201 "Interface": []string{"foo", "bar"},
202 "Time": []string{timeS},
203 "Time-Unix": []string{timeU},
204 "Point": []string{"foo"},
205 "Arg.0": []string{"a"},
206 "Arg.1": []string{"b"},
207 "Arg.2": []string{"c"},
208 "Foo": []string{"bar"},
209 }
210 want := fullTypeStruct{
211 unExport: "",
212 UnExportTwo: "",
213 Bool: true,
214 BoolInt: true,
215 String: "foobar",
216 StringEmpty: "",
217 StringEmptyIgnore: "",
218 Uint: 2,
219 Uint64: 3,
220 Uint8: 4,
221 Uint16: 5,
222 Uint32: 6,
223 Int: 7,
224 Int64: 8,
225 Int8: 9,
226 Int16: 10,
227 Int32: 11,
228 Float32: 12.2,
229 Float64: 13.2,
230 Slice: []string{"a", "b", "c"},
231 SliceTwo: []int{1, 2, 3},
232 Array: [3]string{"a", "b", "c"},
233 ArrayTwo: [2]int{1, 2},
234 Interface: interface{}([]string{"foo", "bar"}),
235 Time: timeV,
236 TimeUnix: timeV,
237 // Point: stringPoint("foo"),
238 Args: []string{"a", "b", "c"},
239 Foo: simpleStruct{Foo: "bar"},
240 }
241 var got fullTypeStruct
242 err := Decode(h, &got)
243 if err != nil {
244 t.Errorf("Decode returned error: %#v", err)
245 }
246 if !reflect.DeepEqual(want, got) {
247 t.Errorf("want %#v, but got %#v", want, got)
248 }
249 }
250
251 func TestDecodeHeader_point(t *testing.T) {
252 type A struct {
253 Point *string
254 }
255 h := http.Header{}
256 h.Set("Point", "foobar")
257 want := A{
258 Point: stringPoint("foobar"),
259 }
260 var got A
261 err := Decode(h, &got)
262 if err != nil {
263 t.Errorf("Decode returned error: %#v", err)
264 }
265 if !reflect.DeepEqual(want, got) {
266 t.Errorf("want %#v, but got %#v", want, got)
267 }
268 }
269 func stringPoint(s string) *string {
270 return &s
271 }
272
273 func Test_fillValues_errors(t *testing.T) {
274 type args struct {
275 sv reflect.Value
276 opts tagOptions
277 valArr []string
278 }
279 tests := []struct {
280 name string
281 args args
282 wantErr bool
283 }{
284 {
285 name: "Uint",
286 args: args{
287 sv: reflect.New(reflect.TypeOf(uint(3))),
288 opts: tagOptions{},
289 valArr: []string{"a"},
290 },
291 wantErr: true,
292 },
293 {
294 name: "Uint64",
295 args: args{
296 sv: reflect.New(reflect.TypeOf(uint64(3))),
297 opts: tagOptions{},
298 valArr: []string{"a"},
299 },
300 wantErr: true,
301 },
302 {
303 name: "Uint8",
304 args: args{
305 sv: reflect.New(reflect.TypeOf(uint8(3))),
306 opts: tagOptions{},
307 valArr: []string{"a"},
308 },
309 wantErr: true,
310 },
311 {
312 name: "Uint16",
313 args: args{
314 sv: reflect.New(reflect.TypeOf(uint16(3))),
315 opts: tagOptions{},
316 valArr: []string{"a"},
317 },
318 wantErr: true,
319 },
320 {
321 name: "Uint32",
322 args: args{
323 sv: reflect.New(reflect.TypeOf(uint32(3))),
324 opts: tagOptions{},
325 valArr: []string{"a"},
326 },
327 wantErr: true,
328 },
329 {
330 name: "int",
331 args: args{
332 sv: reflect.New(reflect.TypeOf(int(3))),
333 opts: tagOptions{},
334 valArr: []string{"a"},
335 },
336 wantErr: true,
337 },
338 {
339 name: "int64",
340 args: args{
341 sv: reflect.New(reflect.TypeOf(int64(3))),
342 opts: tagOptions{},
343 valArr: []string{"a"},
344 },
345 wantErr: true,
346 },
347 {
348 name: "int8",
349 args: args{
350 sv: reflect.New(reflect.TypeOf(int8(3))),
351 opts: tagOptions{},
352 valArr: []string{"a"},
353 },
354 wantErr: true,
355 },
356 {
357 name: "int16",
358 args: args{
359 sv: reflect.New(reflect.TypeOf(int16(3))),
360 opts: tagOptions{},
361 valArr: []string{"a"},
362 },
363 wantErr: true,
364 },
365 {
366 name: "int32",
367 args: args{
368 sv: reflect.New(reflect.TypeOf(int32(3))),
369 opts: tagOptions{},
370 valArr: []string{"a"},
371 },
372 wantErr: true,
373 },
374 {
375 name: "float32",
376 args: args{
377 sv: reflect.New(reflect.TypeOf(float32(3))),
378 opts: tagOptions{},
379 valArr: []string{"a"},
380 },
381 wantErr: true,
382 },
383 {
384 name: "float64",
385 args: args{
386 sv: reflect.New(reflect.TypeOf(float64(3))),
387 opts: tagOptions{},
388 valArr: []string{"a"},
389 },
390 wantErr: true,
391 },
392 {
393 name: "slice",
394 args: args{
395 sv: reflect.New(reflect.TypeOf([]int{})),
396 opts: tagOptions{},
397 valArr: []string{"a"},
398 },
399 wantErr: true,
400 },
401 {
402 name: "array",
403 args: args{
404 sv: reflect.New(reflect.TypeOf([1]int{})),
405 opts: tagOptions{},
406 valArr: []string{"a"},
407 },
408 wantErr: true,
409 },
410 {
411 name: "time",
412 args: args{
413 sv: reflect.New(reflect.TypeOf(time.Time{})),
414 opts: tagOptions{},
415 valArr: []string{"a"},
416 },
417 wantErr: true,
418 },
419 {
420 name: "time unix",
421 args: args{
422 sv: reflect.New(reflect.TypeOf(time.Time{})),
423 opts: tagOptions{"unix"},
424 valArr: []string{"a"},
425 },
426 wantErr: true,
427 },
428 }
429 for _, tt := range tests {
430 t.Run(tt.name, func(t *testing.T) {
431 if err := fillValues(tt.args.sv, tt.args.opts, tt.args.valArr); (err != nil) != tt.wantErr {
432 t.Errorf("fillValues() error = %v, wantErr %v", err, tt.wantErr)
433 }
434 })
435 }
436 }
437
438 func TestDecode_check_header_key_not_present_no_point(t *testing.T) {
439 h := http.Header{}
440 h.Set("Length", "100")
441
442 var got fullTypeStruct
443 err := Decode(h, &got)
444 if err != nil {
445 t.Errorf("Decode returned error: %#v", err)
446 }
447
448 var want fullTypeStruct
449 if !reflect.DeepEqual(want, got) {
450 t.Errorf("want %#v, but got %#v", want, got)
451 }
452 }
453
454 func TestDecode_check_header_key_not_present_point(t *testing.T) {
455 type testStruct struct {
456 A *string
457 B *fullTypeStruct
458 C *int
459 D *[]string
460 E *[2]string
461 F interface{}
462 G *time.Time
463 }
464 h := http.Header{}
465 h.Set("Length", "100")
466
467 var got testStruct
468 err := Decode(h, &got)
469 if err != nil {
470 t.Errorf("Decode returned error: %#v", err)
471 }
472
473 var want testStruct
474 if !reflect.DeepEqual(want, got) {
475 t.Errorf("want %#v, but got %#v", want, got)
476 }
477 if got.A != nil || got.B != nil || got.C != nil || got.D != nil || got.E != nil || got.F != nil || got.G != nil {
478 t.Error("all fields should be nil")
479 }
480 }
481
482 func TestDecode_error(t *testing.T) {
483 h := http.Header{
484 "Int": []string{"abc"},
485 }
486 var got fullTypeStruct
487 err := Decode(h, &got)
488 if err == nil {
489 t.Errorf("expect error, got : %#v", got)
490 }
491 }