github.com/yandex/pandora@v0.5.32/components/guns/grpc/scenario/templater_text_test.go (about)

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