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

     1  package http
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  	gun "github.com/yandex/pandora/components/guns/http_scenario"
     9  	"github.com/yandex/pandora/components/providers/scenario/config"
    10  	"github.com/yandex/pandora/components/providers/scenario/http/postprocessor"
    11  	"github.com/yandex/pandora/components/providers/scenario/http/preprocessor"
    12  	"github.com/yandex/pandora/components/providers/scenario/http/templater"
    13  	"github.com/yandex/pandora/components/providers/scenario/vs"
    14  )
    15  
    16  func Test_decodeAmmo(t *testing.T) {
    17  	storage := &vs.SourceStorage{}
    18  	tests := []struct {
    19  		name    string
    20  		cfg     *config.AmmoConfig
    21  		want    []*gun.Scenario
    22  		wantErr bool
    23  	}{
    24  		{
    25  			name: "full",
    26  			cfg: &config.AmmoConfig{
    27  				Scenarios: []config.ScenarioConfig{
    28  					{
    29  						Name:           "sc1",
    30  						MinWaitingTime: 30,
    31  						Weight:         1,
    32  						Requests: []string{
    33  							"req1(2, 100)",
    34  							"req2",
    35  							"sleep(200)",
    36  						},
    37  					},
    38  					{
    39  						Name:           "sc2",
    40  						MinWaitingTime: 40,
    41  						Weight:         2,
    42  						Requests: []string{
    43  							"req1(2, 300)",
    44  							"sleep(100)",
    45  							"req2",
    46  							"sleep(400)",
    47  						},
    48  					},
    49  				},
    50  				Requests: []config.RequestConfig{
    51  					{
    52  						Name:           "req1",
    53  						Method:         "GET",
    54  						Headers:        map[string]string{"Content-Type": "application/json"},
    55  						Tag:            "",
    56  						Body:           nil,
    57  						URI:            "http://localhost:8080/get",
    58  						Preprocessor:   &preprocessor.Preprocessor{Mapping: map[string]string{"a": "b"}},
    59  						Postprocessors: []gun.Postprocessor{&postprocessor.VarHeaderPostprocessor{}, &postprocessor.VarJsonpathPostprocessor{}},
    60  						Templater:      templater.NewHTMLTemplater(),
    61  					},
    62  					{
    63  						Name:           "req2",
    64  						Method:         "POST",
    65  						Headers:        map[string]string{"Content-Type": "application/json", "Date": "2020-01-01"},
    66  						Tag:            "",
    67  						Body:           nil,
    68  						URI:            "http://localhost:8080/post",
    69  						Preprocessor:   &preprocessor.Preprocessor{Mapping: map[string]string{"c": "d"}},
    70  						Postprocessors: []gun.Postprocessor{&postprocessor.VarXpathPostprocessor{}},
    71  						Templater:      nil,
    72  					},
    73  				},
    74  			},
    75  			want: []*gun.Scenario{
    76  				{
    77  					Requests: []gun.Request{
    78  						{
    79  							Method:         "GET",
    80  							Headers:        map[string]string{"Content-Type": "application/json"},
    81  							Tag:            "",
    82  							Body:           nil,
    83  							Name:           "req1",
    84  							URI:            "http://localhost:8080/get",
    85  							Preprocessor:   &preprocessor.Preprocessor{Mapping: map[string]string{"a": "b"}},
    86  							Postprocessors: []gun.Postprocessor{&postprocessor.VarHeaderPostprocessor{}, &postprocessor.VarJsonpathPostprocessor{}},
    87  							Templater:      templater.NewHTMLTemplater(),
    88  							Sleep:          100 * time.Millisecond,
    89  						},
    90  						{
    91  							Method:         "GET",
    92  							Headers:        map[string]string{"Content-Type": "application/json"},
    93  							Tag:            "",
    94  							Body:           nil,
    95  							Name:           "req1",
    96  							URI:            "http://localhost:8080/get",
    97  							Preprocessor:   &preprocessor.Preprocessor{Mapping: map[string]string{"a": "b"}},
    98  							Postprocessors: []gun.Postprocessor{&postprocessor.VarHeaderPostprocessor{}, &postprocessor.VarJsonpathPostprocessor{}},
    99  							Templater:      templater.NewHTMLTemplater(),
   100  							Sleep:          100 * time.Millisecond,
   101  						},
   102  						{
   103  							Method:         "POST",
   104  							Headers:        map[string]string{"Content-Type": "application/json", "Date": "2020-01-01"},
   105  							Tag:            "",
   106  							Body:           nil,
   107  							Name:           "req2",
   108  							URI:            "http://localhost:8080/post",
   109  							Preprocessor:   &preprocessor.Preprocessor{Mapping: map[string]string{"c": "d"}},
   110  							Postprocessors: []gun.Postprocessor{&postprocessor.VarXpathPostprocessor{}},
   111  							Templater:      templater.NewTextTemplater(),
   112  							Sleep:          200 * time.Millisecond,
   113  						},
   114  					},
   115  					ID:              0,
   116  					Name:            "sc1",
   117  					MinWaitingTime:  30 * time.Millisecond,
   118  					VariableStorage: storage,
   119  				},
   120  				{
   121  					Requests: []gun.Request{
   122  						{
   123  							Method:         "GET",
   124  							Headers:        map[string]string{"Content-Type": "application/json"},
   125  							Tag:            "",
   126  							Body:           nil,
   127  							Name:           "req1",
   128  							URI:            "http://localhost:8080/get",
   129  							Preprocessor:   &preprocessor.Preprocessor{Mapping: map[string]string{"a": "b"}},
   130  							Postprocessors: []gun.Postprocessor{&postprocessor.VarHeaderPostprocessor{}, &postprocessor.VarJsonpathPostprocessor{}},
   131  							Templater:      templater.NewHTMLTemplater(),
   132  							Sleep:          300 * time.Millisecond,
   133  						},
   134  						{
   135  							Method:         "GET",
   136  							Headers:        map[string]string{"Content-Type": "application/json"},
   137  							Tag:            "",
   138  							Body:           nil,
   139  							Name:           "req1",
   140  							URI:            "http://localhost:8080/get",
   141  							Preprocessor:   &preprocessor.Preprocessor{Mapping: map[string]string{"a": "b"}},
   142  							Postprocessors: []gun.Postprocessor{&postprocessor.VarHeaderPostprocessor{}, &postprocessor.VarJsonpathPostprocessor{}},
   143  							Templater:      templater.NewHTMLTemplater(),
   144  							Sleep:          400 * time.Millisecond,
   145  						},
   146  						{
   147  							Method:         "POST",
   148  							Headers:        map[string]string{"Content-Type": "application/json", "Date": "2020-01-01"},
   149  							Tag:            "",
   150  							Body:           nil,
   151  							Name:           "req2",
   152  							URI:            "http://localhost:8080/post",
   153  							Preprocessor:   &preprocessor.Preprocessor{Mapping: map[string]string{"c": "d"}},
   154  							Postprocessors: []gun.Postprocessor{&postprocessor.VarXpathPostprocessor{}},
   155  							Templater:      templater.NewTextTemplater(),
   156  							Sleep:          400 * time.Millisecond,
   157  						},
   158  					},
   159  					ID:              0,
   160  					Name:            "sc2",
   161  					MinWaitingTime:  40 * time.Millisecond,
   162  					VariableStorage: storage,
   163  				},
   164  				{
   165  					Requests: []gun.Request{
   166  						{
   167  							Method:         "GET",
   168  							Headers:        map[string]string{"Content-Type": "application/json"},
   169  							Tag:            "",
   170  							Body:           nil,
   171  							Name:           "req1",
   172  							URI:            "http://localhost:8080/get",
   173  							Preprocessor:   &preprocessor.Preprocessor{Mapping: map[string]string{"a": "b"}},
   174  							Postprocessors: []gun.Postprocessor{&postprocessor.VarHeaderPostprocessor{}, &postprocessor.VarJsonpathPostprocessor{}},
   175  							Templater:      templater.NewHTMLTemplater(),
   176  							Sleep:          300 * time.Millisecond,
   177  						},
   178  						{
   179  							Method:         "GET",
   180  							Headers:        map[string]string{"Content-Type": "application/json"},
   181  							Tag:            "",
   182  							Body:           nil,
   183  							Name:           "req1",
   184  							URI:            "http://localhost:8080/get",
   185  							Preprocessor:   &preprocessor.Preprocessor{Mapping: map[string]string{"a": "b"}},
   186  							Postprocessors: []gun.Postprocessor{&postprocessor.VarHeaderPostprocessor{}, &postprocessor.VarJsonpathPostprocessor{}},
   187  							Templater:      templater.NewHTMLTemplater(),
   188  							Sleep:          400 * time.Millisecond,
   189  						},
   190  						{
   191  							Method:         "POST",
   192  							Headers:        map[string]string{"Content-Type": "application/json", "Date": "2020-01-01"},
   193  							Tag:            "",
   194  							Body:           nil,
   195  							Name:           "req2",
   196  							URI:            "http://localhost:8080/post",
   197  							Preprocessor:   &preprocessor.Preprocessor{Mapping: map[string]string{"c": "d"}},
   198  							Postprocessors: []gun.Postprocessor{&postprocessor.VarXpathPostprocessor{}},
   199  							Templater:      templater.NewTextTemplater(),
   200  							Sleep:          400 * time.Millisecond,
   201  						},
   202  					},
   203  					ID:              0,
   204  					Name:            "sc2",
   205  					MinWaitingTime:  40 * time.Millisecond,
   206  					VariableStorage: storage,
   207  				},
   208  			},
   209  		},
   210  	}
   211  	for _, tt := range tests {
   212  		t.Run(tt.name, func(t *testing.T) {
   213  			got, err := decodeAmmo(tt.cfg, storage)
   214  			if tt.wantErr {
   215  				require.Error(t, err)
   216  				return
   217  			}
   218  			for _, s := range got {
   219  				for _, r := range s.Requests {
   220  					if p, ok := r.Preprocessor.(IteratorIniter); ok {
   221  						p.InitIterator(nil)
   222  					}
   223  				}
   224  			}
   225  			require.NoError(t, err)
   226  			require.Equal(t, tt.want, got)
   227  		})
   228  	}
   229  }