github.com/yandex/pandora@v0.5.32/components/providers/scenario/http/postprocessor/var_jsonpath_test.go (about) 1 package postprocessor 2 3 import ( 4 "bytes" 5 "net/http" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestVarJsonpathPostprocessor_Process(t *testing.T) { 12 testCases := []struct { 13 name string 14 mappings map[string]string 15 body []byte 16 expected map[string]interface{} 17 expectErr bool 18 }{ 19 { 20 name: "Test Case 1", 21 mappings: map[string]string{ 22 "person_name": "$.name", 23 "person_age": "$.age", 24 }, 25 body: []byte(`{"name": "John", "age": 30}`), 26 expected: map[string]interface{}{ 27 "person_name": "John", 28 "person_age": float64(30), 29 }, 30 expectErr: false, 31 }, 32 { 33 name: "Test Case 2", 34 mappings: map[string]string{ 35 "user_name": "$.username", 36 "user_age": "$.age", 37 }, 38 body: []byte(`{"username": "Alice", "age": 25}`), 39 expected: map[string]interface{}{ 40 "user_name": "Alice", 41 "user_age": float64(25), 42 }, 43 expectErr: false, 44 }, 45 { 46 name: "Test Case 3 - JSON parsing error", 47 mappings: map[string]string{ 48 "name": "$.name", 49 }, 50 body: []byte(`invalid json`), 51 expected: map[string]interface{}{}, 52 expectErr: true, 53 }, 54 { 55 name: "Test Case 4 - Missing JSON field", 56 mappings: map[string]string{ 57 "address": "$.address", 58 }, 59 body: []byte(`{"name": "Bob", "age": 35}`), 60 expected: map[string]interface{}{}, 61 expectErr: true, 62 }, 63 { 64 name: "Test Case 5 - Nested JSON", 65 mappings: map[string]string{ 66 "city": "$.address.city", 67 "zip_code": "$.address.zip", 68 "country": "$.address.country", 69 "full_name": "$.personal.name.full", 70 }, 71 body: []byte(`{ 72 "personal": { 73 "name": { 74 "first": "Jane", 75 "last": "Doe", 76 "full": "Jane Doe" 77 }, 78 "age": 28 79 }, 80 "address": { 81 "city": "New York", 82 "zip": "10001", 83 "country": "USA" 84 } 85 }`), 86 expected: map[string]interface{}{ 87 "city": "New York", 88 "zip_code": "10001", 89 "country": "USA", 90 "full_name": "Jane Doe", 91 }, 92 expectErr: false, 93 }, 94 } 95 for _, tc := range testCases { 96 t.Run(tc.name, func(t *testing.T) { 97 p := &VarJsonpathPostprocessor{Mapping: tc.mappings} 98 buf := bytes.NewReader(tc.body) 99 100 reqMap, err := p.Process(&http.Response{}, buf) 101 if tc.expectErr { 102 assert.Error(t, err, "Expected an error, but got none") 103 return 104 } else { 105 assert.NoError(t, err, "Process should not return an error") 106 } 107 assert.Equal(t, tc.expected, reqMap, "Process result not as expected") 108 }) 109 } 110 }