github.com/yandex/pandora@v0.5.32/components/providers/scenario/http/postprocessor/var_xpath_test.go (about)

     1  package postprocessor
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  	"golang.org/x/net/html"
    10  )
    11  
    12  func TestVarXpathPostprocessor_Process(t *testing.T) {
    13  	// Define test cases with different bodies and mappings.
    14  	testCases := []struct {
    15  		name           string
    16  		body           []byte
    17  		mappings       map[string]string
    18  		expectedReqMap map[string]interface{}
    19  	}{
    20  		{
    21  			name: "Test Case 1",
    22  			body: []byte(`
    23  				<html>
    24  					<body>
    25  						<div class="data">Value1</div>
    26  						<div class="data">Value2</div>
    27  					</body>
    28  				</html>
    29  			`),
    30  			mappings: map[string]string{
    31  				"key1": "//div[@class='data']",
    32  			},
    33  			expectedReqMap: map[string]interface{}{
    34  				"key1": []string{"Value1", "Value2"},
    35  			},
    36  		},
    37  		{
    38  			name: "Test Case 2",
    39  			body: []byte(`
    40  				<html>
    41  					<body>
    42  						<span class="span-data">ValueX</span>
    43  						<span class="span-data">ValueY</span>
    44  					</body>
    45  				</html>
    46  			`),
    47  			mappings: map[string]string{
    48  				"keyAlpha": "//span[@class='span-data']",
    49  			},
    50  			expectedReqMap: map[string]interface{}{
    51  				"keyAlpha": []string{"ValueX", "ValueY"},
    52  			},
    53  		},
    54  		{
    55  			name: "Test Case 3",
    56  			body: []byte(`
    57  				<html>
    58  					<body>
    59  						<p class="paragraph">This is a paragraph</p>
    60  					</body>
    61  				</html>
    62  			`),
    63  			mappings: map[string]string{
    64  				"keyParagraph": "//p[@class='paragraph']",
    65  			},
    66  			expectedReqMap: map[string]interface{}{
    67  				"keyParagraph": "This is a paragraph",
    68  			},
    69  		},
    70  	}
    71  
    72  	for _, tc := range testCases {
    73  		t.Run(tc.name, func(t *testing.T) {
    74  			postprocessor := &VarXpathPostprocessor{
    75  				Mapping: tc.mappings,
    76  			}
    77  
    78  			buf := bytes.NewBuffer(tc.body)
    79  			reqMap, err := postprocessor.Process(nil, buf)
    80  
    81  			assert.NoError(t, err)
    82  			assert.Equal(t, tc.expectedReqMap, reqMap)
    83  		})
    84  	}
    85  }
    86  
    87  func Test_getValuesFromDOM(t *testing.T) {
    88  	data := []byte(`
    89  		<html>
    90  			<head>
    91  				<title>Example</title>
    92  			</head>
    93  			<body>
    94  				<ul>
    95  					<li>Order 1</li>
    96  					<li>Order 2</li>
    97  					<li>Order 3</li>
    98  				</ul>
    99  			</body>
   100  		</html>
   101  	`)
   102  
   103  	doc, err := html.Parse(bytes.NewReader(data))
   104  	require.NoError(t, err)
   105  
   106  	xpathQuery := "//li"
   107  	p := &VarXpathPostprocessor{}
   108  	results, err := p.getValuesFromDOM(doc, xpathQuery)
   109  	require.NoError(t, err)
   110  
   111  	require.Equal(t, []string{"Order 1", "Order 2", "Order 3"}, results)
   112  }