github.com/yandex/pandora@v0.5.32/components/providers/scenario/http/preprocessor/preprocessor_test.go (about) 1 package preprocessor 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestPreprocessor_Process(t *testing.T) { 10 tests := []struct { 11 name string 12 prep Preprocessor 13 templVars map[string]any 14 wantMap map[string]any 15 wantErr bool 16 }{ 17 { 18 name: "nil templateVars", 19 prep: Preprocessor{ 20 Mapping: map[string]string{ 21 "var1": "source.items[0].id", 22 "var2": "source.items[1]", 23 }, 24 }, 25 wantErr: true, 26 }, 27 { 28 name: "simple processing", 29 prep: Preprocessor{ 30 Mapping: map[string]string{ 31 "var1": "source.items[0].id", 32 "var2": "source.items[1]", 33 "var3": "request.auth.token", 34 }, 35 }, 36 templVars: map[string]any{ 37 "request": map[string]any{ 38 "auth": map[string]any{"token": "Bearer token"}, 39 }, 40 "source": map[string]any{ 41 "items": []map[string]any{ 42 {"id": "1"}, 43 {"id": "2"}, 44 }, 45 }, 46 }, 47 wantMap: map[string]any{ 48 "var1": "1", 49 "var2": map[string]any{"id": "2"}, 50 "var3": "Bearer token", 51 }, 52 wantErr: false, 53 }, 54 { 55 name: "template funcs", 56 prep: Preprocessor{ 57 Mapping: map[string]string{ 58 "var4": "randInt(.request.auth.min, 201)", 59 "var5": "randString(source.items[last].id, .request.get.letters)", 60 }, 61 }, 62 templVars: map[string]any{ 63 "request": map[string]any{ 64 "auth": map[string]any{"min": 200}, 65 "get": map[string]any{"letters": "a"}, 66 }, 67 "source": map[string]any{ 68 "items": []map[string]any{ 69 {"id": "1"}, 70 {"id": "2"}, 71 {"id": "10"}, 72 }, 73 }, 74 }, 75 wantMap: map[string]any{ 76 "var4": "200", 77 "var5": "aaaaaaaaaa", 78 }, 79 wantErr: false, 80 }, 81 } 82 83 for _, tt := range tests { 84 t.Run(tt.name, func(t *testing.T) { 85 result, err := tt.prep.Process(tt.templVars) 86 87 if tt.wantErr { 88 assert.Error(t, err) 89 } else { 90 assert.NoError(t, err) 91 assert.Equal(t, tt.wantMap, result) 92 } 93 }) 94 } 95 }