github.com/avenga/couper@v1.12.2/internal/seetie/convert_test.go (about) 1 package seetie 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/google/go-cmp/cmp" 9 "github.com/sirupsen/logrus" 10 "github.com/zclconf/go-cty/cty" 11 ) 12 13 func Test_stringListToValue(t *testing.T) { 14 tests := []struct { 15 slice []string 16 }{ 17 {[]string{"a", "b"}}, 18 {[]string{}}, 19 } 20 21 for _, tt := range tests { 22 t.Run(fmt.Sprintf("%v", tt.slice), func(subT *testing.T) { 23 val := stringListToValue(tt.slice) 24 valType := val.Type() 25 if !valType.IsListType() { 26 t.Error("Expected value type to be list") 27 } 28 if *valType.ListElementType() != cty.String { 29 t.Error("Expected list to contain string values") 30 } 31 sl := val.AsValueSlice() 32 if len(sl) != len(tt.slice) { 33 t.Errorf("Wrong number of items; want: %d, got: %d", len(tt.slice), len(sl)) 34 } 35 for i, v := range tt.slice { 36 if sl[i].AsString() != v { 37 t.Errorf("Wrong item at position %d; want %q, got %q", i, v, sl[i]) 38 } 39 } 40 }) 41 } 42 } 43 44 func Test_ValueToLogFields(t *testing.T) { 45 type testCase struct { 46 name string 47 val cty.Value 48 expLog logrus.Fields 49 } 50 for _, tc := range []testCase{ 51 { 52 name: "form body", 53 val: ValuesMapToValue(map[string][]string{"a": {"b"}}), 54 expLog: logrus.Fields{"v": logrus.Fields{"a": []interface{}{"b"}}}, 55 }, 56 { 57 name: "cookies", 58 val: CookiesToMapValue([]*http.Cookie{{Name: "c", Value: "d"}}), 59 expLog: logrus.Fields{"v": logrus.Fields{"c": "d"}}, 60 }, 61 { 62 name: "headers", 63 val: HeaderToMapValue(http.Header{"c": {"d"}}), 64 expLog: logrus.Fields{"v": logrus.Fields{"c": "d"}}, 65 }, 66 } { 67 t.Run(tc.name, func(subT *testing.T) { 68 logs := cty.MapVal(map[string]cty.Value{"v": tc.val}) 69 lf := ValueToLogFields(logs) 70 if !cmp.Equal(tc.expLog, lf) { 71 t.Errorf("Expected\n%#v, got:\n%#v", tc.expLog, lf) 72 } 73 }) 74 } 75 }