github.com/avenga/couper@v1.12.2/server/form_params_test.go (about)

     1  package server_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"net/http"
     7  	"path"
     8  	"testing"
     9  
    10  	"github.com/avenga/couper/internal/test"
    11  )
    12  
    13  func TestIntegration_FormParams(t *testing.T) {
    14  	client := newClient()
    15  
    16  	const confPath = "testdata/integration/form_params/"
    17  
    18  	type testCase struct {
    19  		file    string
    20  		method  string
    21  		ct      string
    22  		post    string
    23  		expArgs string
    24  		expCT   string
    25  		expErr  string
    26  	}
    27  
    28  	for i, tc := range []testCase{
    29  		{
    30  			file:    "01_couper.hcl",
    31  			method:  http.MethodPost,
    32  			ct:      "application/x-www-form-urlencoded",
    33  			post:    "x=X+1&x=X%202&y=Y&d=d",
    34  			expArgs: `"Args":{"a":["A"],"b":["B"],"c":["C 1","C 2"],"d":["d","D"],"y":["Y"]}`,
    35  			expCT:   `"Content-Type":["application/x-www-form-urlencoded"]`,
    36  			expErr:  "",
    37  		},
    38  		{
    39  			file:    "01_couper.hcl",
    40  			method:  http.MethodDelete,
    41  			ct:      "application/x-www-form-urlencoded",
    42  			post:    "x=X+1&x=X%202&y=Y",
    43  			expArgs: `"Args":{}`,
    44  			expCT:   `"Content-Type":["application/x-www-form-urlencoded"]`,
    45  			expErr:  "expression evaluation error: form_params: method mismatch: DELETE",
    46  		},
    47  		{
    48  			file:    "01_couper.hcl",
    49  			method:  http.MethodPut,
    50  			ct:      "application/x-www-form-urlencoded",
    51  			post:    "x=X+1&x=X%202&y=Y",
    52  			expArgs: `"Args":{"x":["X 1","X 2"],"y":["Y"]}`,
    53  			expCT:   `"Content-Type":["application/x-www-form-urlencoded"]`,
    54  			expErr:  "expression evaluation error: form_params: method mismatch: PUT",
    55  		},
    56  		{
    57  			file:    "01_couper.hcl",
    58  			method:  http.MethodGet,
    59  			ct:      "text/plain",
    60  			post:    "",
    61  			expArgs: `"Args":{}`,
    62  			expCT:   `"Content-Type":["text/plain"]`,
    63  			expErr:  "expression evaluation error: form_params: method mismatch: GET",
    64  		},
    65  		{
    66  			file:    "01_couper.hcl",
    67  			method:  http.MethodGet,
    68  			ct:      "text/plain",
    69  			post:    "not-supported",
    70  			expArgs: `"Args":{}`,
    71  			expCT:   `"Content-Type":["text/plain"]`,
    72  			expErr:  `expression evaluation error: form_params: method mismatch: GET`,
    73  		},
    74  		{
    75  			file:    "01_couper.hcl",
    76  			method:  http.MethodPost,
    77  			ct:      "application/foo",
    78  			post:    "",
    79  			expArgs: ``,
    80  			expCT:   ``,
    81  			expErr:  `expression evaluation error: form_params: content-type mismatch: application/foo`,
    82  		},
    83  		{
    84  			file:    "01_couper.hcl",
    85  			method:  http.MethodDelete,
    86  			ct:      "application/x-www-form-urlencoded",
    87  			post:    "",
    88  			expArgs: ``,
    89  			expCT:   ``,
    90  			expErr:  `expression evaluation error: form_params: method mismatch: DELETE`,
    91  		},
    92  		{
    93  			file:    "01_couper.hcl",
    94  			method:  http.MethodPut,
    95  			ct:      "application/x-www-form-urlencoded",
    96  			post:    "",
    97  			expArgs: ``,
    98  			expCT:   ``,
    99  			expErr:  `expression evaluation error: form_params: method mismatch: PUT`,
   100  		},
   101  		{
   102  			file:    "02_couper.hcl",
   103  			method:  http.MethodGet,
   104  			ct:      "application/x-www-form-urlencoded",
   105  			post:    "x=X+1&x=X%202&y=Y",
   106  			expArgs: `"Args":{}`,
   107  			expCT:   `"Content-Type":["application/x-www-form-urlencoded"]`,
   108  			expErr:  "",
   109  		},
   110  		{
   111  			file:    "03_couper.hcl",
   112  			method:  http.MethodPost,
   113  			ct:      "application/x-www-form-urlencoded",
   114  			post:    "x=X+1&x=X%202&y=Y",
   115  			expArgs: `"Args":{"y":["Y"]}`,
   116  			expCT:   `"Content-Type":["application/x-www-form-urlencoded"]`,
   117  			expErr:  "",
   118  		},
   119  		{
   120  			file:    "04_couper.hcl",
   121  			method:  http.MethodPost,
   122  			ct:      "application/x-www-form-urlencoded",
   123  			post:    "x=X+1&x=X%202&y=Y",
   124  			expArgs: `"Args":{"y":["Y"]}`,
   125  			expCT:   `"Content-Type":["application/x-www-form-urlencoded"]`,
   126  			expErr:  "",
   127  		},
   128  	} {
   129  		t.Run("_"+tc.post, func(subT *testing.T) {
   130  			helper := test.New(subT)
   131  
   132  			shutdown, hook := newCouper(path.Join(confPath, tc.file), helper)
   133  			defer shutdown()
   134  
   135  			req, err := http.NewRequest(tc.method, "http://example.com:8080/", nil)
   136  			helper.Must(err)
   137  
   138  			req.Close = true
   139  
   140  			if tc.post != "" {
   141  				req.Body = io.NopCloser(bytes.NewBuffer([]byte(tc.post)))
   142  			}
   143  
   144  			if tc.ct != "" {
   145  				req.Header.Set("Content-Type", tc.ct)
   146  			}
   147  
   148  			hook.Reset()
   149  			res, err := client.Do(req)
   150  			helper.Must(err)
   151  
   152  			defer res.Body.Close()
   153  
   154  			if res.StatusCode != http.StatusOK {
   155  				subT.Errorf("%d: Expected status 200, given %d", i, res.StatusCode)
   156  			}
   157  
   158  			entries := hook.AllEntries()
   159  			if len(entries) == 0 {
   160  				subT.Fatal("Expected log messages, got none")
   161  			}
   162  
   163  			if entries[0].Message != tc.expErr {
   164  				subT.Logf("%v", hook)
   165  				subT.Errorf("%d: Expected message log: %s", i, tc.expErr)
   166  			}
   167  
   168  			resBytes, err := io.ReadAll(res.Body)
   169  			helper.Must(err)
   170  
   171  			if !bytes.Contains(resBytes, []byte(tc.expArgs)) {
   172  				subT.Errorf("%d: \nwant: \n%s\nin: \n%s", i, tc.expArgs, resBytes)
   173  			}
   174  
   175  			if !bytes.Contains(resBytes, []byte(tc.expCT)) {
   176  				subT.Errorf("%d: \nwant: \n%s\nin: \n%s", i, tc.expCT, resBytes)
   177  			}
   178  		})
   179  	}
   180  }