@@ -136,6 +136,7 @@ func TestDecodeHeader_UnmarshalerWithNilPointer(t *testing.T) {
136136
137137type simpleStruct struct {
138138Foo string
139+ Bar int
139140}
140141
141142type fullTypeStruct struct {
@@ -243,7 +244,7 @@ func TestDecodeHeader_more_data_type(t *testing.T) {
243244t .Errorf ("Decode returned error: %#v" , err )
244245}
245246if ! reflect .DeepEqual (want , got ) {
246- t .Errorf ("want %#v, but got %#v" , want , got )
247+ t .Errorf ("want/got: \n %#v\n %#v" , want , got )
247248}
248249}
249250
@@ -388,6 +389,42 @@ func Test_fillValues_errors(t *testing.T) {
388389},
389390wantErr : true ,
390391},
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+ },
391428}
392429for _ , tt := range tests {
393430t .Run (tt .name , func (t * testing.T ) {
@@ -397,3 +434,114 @@ func Test_fillValues_errors(t *testing.T) {
397434})
398435}
399436}
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+ }
492+
493+ func TestDecodeHeader_embeddedStructs (t * testing.T ) {
494+ tests := []struct {
495+ in http.Header
496+ decode func (http.Header ) (interface {}, error )
497+ want interface {}
498+ }{
499+ {
500+ http.Header {"C" : []string {"foo" }},
501+ func (h http.Header ) (interface {}, error ) {
502+ var a A
503+ err := Decode (h , & a )
504+ return a , err
505+ },
506+ A {B {C : "foo" }},
507+ },
508+ {
509+ http.Header {"C" : []string {"foo" }},
510+ func (h http.Header ) (interface {}, error ) {
511+ var d D
512+ err := Decode (h , & d )
513+ return d , err
514+ },
515+ D {B : B {C : "" }, C : "foo" },
516+ },
517+ {
518+ http.Header {"C" : []string {"foo" , "bar" }},
519+ func (h http.Header ) (interface {}, error ) {
520+ var d D
521+ err := Decode (h , & d )
522+ return d , err
523+ },
524+ D {B : B {C : "bar" }, C : "foo" },
525+ },
526+ {
527+ http.Header {"C" : []string {"foo" , "bar" }},
528+ func (h http.Header ) (interface {}, error ) {
529+ var f F
530+ err := Decode (h , & f )
531+ return f , err
532+ },
533+ F {e {B : B {C : "bar" }, C : "foo" }}, // With unexported embed
534+ },
535+ }
536+
537+ for i , tt := range tests {
538+ v , err := tt .decode (tt .in )
539+ if err != nil {
540+ t .Errorf ("%d. Header(%+v) returned error: %v" , i , tt .in , err )
541+ }
542+
543+ if ! reflect .DeepEqual (tt .want , v ) {
544+ t .Errorf ("%d. Header(%+v) returned/want:\n %#+v\n %#+v" , i , tt .in , v , tt .want )
545+ }
546+ }
547+ }
0 commit comments