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

     1  package templater
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  	gun "github.com/yandex/pandora/components/guns/http_scenario"
     9  )
    10  
    11  func TestHTMLTemplater_Apply(t *testing.T) {
    12  	tests := []struct {
    13  		name            string
    14  		scenarioName    string
    15  		stepName        string
    16  		parts           *gun.RequestParts
    17  		vs              map[string]interface{}
    18  		expectedURL     string
    19  		expectedHeaders map[string]string
    20  		expectedBody    string
    21  		expectError     bool
    22  	}{
    23  		{
    24  			name:         "Test Scenario 1",
    25  			scenarioName: "TestScenario",
    26  			stepName:     "TestStep",
    27  			parts: &gun.RequestParts{
    28  				URL: "http://example.com/{{.endpoint}}",
    29  				Headers: map[string]string{
    30  					"Authorization": "Bearer {{.token}}",
    31  					"Content-Type":  "application/json",
    32  				},
    33  				Body: []byte(`<div data-age="{{.age}}" >{{.name}}</div>`),
    34  			},
    35  			vs: map[string]interface{}{
    36  				"endpoint": "users",
    37  				"token":    "abc123",
    38  				"name":     "John",
    39  				"age":      30,
    40  			},
    41  			expectedURL: "http://example.com/users",
    42  			expectedHeaders: map[string]string{
    43  				"Authorization": "Bearer abc123",
    44  				"Content-Type":  "application/json",
    45  			},
    46  			expectedBody: `<div data-age="30" >John</div>`,
    47  			expectError:  false,
    48  		},
    49  		{
    50  			name:         "Test Scenario 2 (Invalid Template)",
    51  			scenarioName: "TestScenario",
    52  			stepName:     "TestStep",
    53  			parts: &gun.RequestParts{
    54  				URL: "http://example.com/{{.endpoint",
    55  			},
    56  			vs: map[string]interface{}{
    57  				"endpoint": "users",
    58  			},
    59  			expectedURL:     "",
    60  			expectedHeaders: nil,
    61  			expectedBody:    "",
    62  			expectError:     true,
    63  		},
    64  		{
    65  			name:            "Test Scenario 3 (Empty gun.RequestParts)",
    66  			scenarioName:    "EmptyScenario",
    67  			stepName:        "EmptyStep",
    68  			parts:           &gun.RequestParts{},
    69  			vs:              map[string]interface{}{},
    70  			expectedURL:     "",
    71  			expectedHeaders: nil,
    72  			expectedBody:    "",
    73  			expectError:     false,
    74  		},
    75  		{
    76  			name:         "Test Scenario 4 (No Variables)",
    77  			scenarioName: "NoVarsScenario",
    78  			stepName:     "NoVarsStep",
    79  			parts: &gun.RequestParts{
    80  				URL: "http://example.com",
    81  				Headers: map[string]string{
    82  					"Authorization": "Bearer abc123",
    83  				},
    84  				Body: []byte(`<div data-age="30" >John</div>`),
    85  			},
    86  			vs:          map[string]interface{}{},
    87  			expectedURL: "http://example.com",
    88  			expectedHeaders: map[string]string{
    89  				"Authorization": "Bearer abc123",
    90  			},
    91  			expectedBody: `<div data-age="30" >John</div>`,
    92  			expectError:  false,
    93  		},
    94  		{
    95  			name:         "Test Scenario 5 (URL Only)",
    96  			scenarioName: "URLScenario",
    97  			stepName:     "URLStep",
    98  			parts: &gun.RequestParts{
    99  				URL: "http://example.com/{{.endpoint}}",
   100  			},
   101  			vs: map[string]interface{}{
   102  				"endpoint": "users",
   103  			},
   104  			expectedURL:     "http://example.com/users",
   105  			expectedHeaders: nil,
   106  			expectedBody:    "",
   107  			expectError:     false,
   108  		},
   109  		{
   110  			name:         "Test Scenario 6 (Headers Only)",
   111  			scenarioName: "HeaderScenario",
   112  			stepName:     "HeaderStep",
   113  			parts: &gun.RequestParts{
   114  				Headers: map[string]string{
   115  					"Authorization": "Bearer {{.token}}",
   116  					"Content-Type":  "application/json",
   117  				},
   118  			},
   119  			vs: map[string]interface{}{
   120  				"token": "xyz456",
   121  			},
   122  			expectedURL: "",
   123  			expectedHeaders: map[string]string{
   124  				"Authorization": "Bearer xyz456",
   125  				"Content-Type":  "application/json",
   126  			},
   127  			expectedBody: "",
   128  			expectError:  false,
   129  		},
   130  		{
   131  			name:         "Test Scenario 7 (Body Only)",
   132  			scenarioName: "BodyScenario",
   133  			stepName:     "BodyStep",
   134  			parts: &gun.RequestParts{
   135  				Body: []byte(`<div data-age="{{.age}}" >{{.name}}</div>`),
   136  			},
   137  			vs: map[string]interface{}{
   138  				"name": "Alice",
   139  				"age":  25,
   140  			},
   141  			expectedURL:     "",
   142  			expectedHeaders: nil,
   143  			expectedBody:    `<div data-age="25" >Alice</div>`,
   144  			expectError:     false,
   145  		},
   146  		{
   147  			name:         "Test Scenario 8 (Invalid Template in Headers)",
   148  			scenarioName: "InvalidHeaderScenario",
   149  			stepName:     "InvalidHeaderStep",
   150  			parts: &gun.RequestParts{
   151  				Headers: map[string]string{
   152  					"Authorization": "Bearer {{.token",
   153  				},
   154  			},
   155  			vs:              map[string]interface{}{},
   156  			expectedURL:     "",
   157  			expectedHeaders: nil,
   158  			expectedBody:    "",
   159  			expectError:     true,
   160  		},
   161  		{
   162  			name:         "Test Scenario 9 (Invalid Template in URL)",
   163  			scenarioName: "InvalidURLScenario",
   164  			stepName:     "InvalidURLStep",
   165  			parts: &gun.RequestParts{
   166  				URL: "http://example.com/{{.endpoint",
   167  			},
   168  			vs:              map[string]interface{}{},
   169  			expectedURL:     "",
   170  			expectedHeaders: nil,
   171  			expectedBody:    "",
   172  			expectError:     true,
   173  		},
   174  		{
   175  			name:         "Test Scenario 10 (Invalid Template in Body)",
   176  			scenarioName: "InvalidBodyScenario",
   177  			stepName:     "InvalidBodyStep",
   178  			parts: &gun.RequestParts{
   179  				Body: []byte(`{"name": "{{.name}"}`),
   180  			},
   181  			vs:              map[string]interface{}{},
   182  			expectedURL:     "",
   183  			expectedHeaders: nil,
   184  			expectedBody:    "",
   185  			expectError:     true,
   186  		},
   187  	}
   188  
   189  	for _, test := range tests {
   190  		t.Run(test.name, func(t *testing.T) {
   191  			templater := &HTMLTemplater{}
   192  			err := templater.Apply(test.parts, test.vs, test.scenarioName, test.stepName)
   193  
   194  			if test.expectError {
   195  				require.Error(t, err)
   196  			} else {
   197  				require.NoError(t, err)
   198  				assert.Equal(t, test.expectedURL, test.parts.URL)
   199  				assert.Equal(t, test.expectedHeaders, test.parts.Headers)
   200  				assert.Equal(t, test.expectedBody, string(test.parts.Body))
   201  			}
   202  		})
   203  	}
   204  }