github.com/msales/pkg/v3@v3.24.0/httpx/middleware/query_normalizer_test.go (about)

     1  package middleware_test
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"testing"
     7  
     8  	"net/url"
     9  
    10  	"github.com/msales/pkg/v3/httpx/middleware"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  var testsData = []struct {
    15  	inUrl  string
    16  	outMap url.Values
    17  }{
    18  	{
    19  		inUrl: "/?param[0]=value1&param[1]=value2",
    20  		outMap: url.Values{
    21  			"param[]": {
    22  				"value1",
    23  				"value2",
    24  			},
    25  		},
    26  	},
    27  	{
    28  		inUrl: "/?param%5B0%5D=value1&param%5B1%5D=value2",
    29  		outMap: url.Values{
    30  			"param[]": {
    31  				"value1",
    32  				"value2",
    33  			},
    34  		},
    35  	},
    36  	{
    37  		inUrl: "/?param[]=value1&param[]=value2",
    38  		outMap: url.Values{
    39  			"param[]": {
    40  				"value1",
    41  				"value2",
    42  			},
    43  		},
    44  	},
    45  	{
    46  		inUrl: "/?param[]=value1&param[]=value2&param2[]=Param1&param2[]=Param2",
    47  		outMap: url.Values{
    48  			"param[]": {
    49  				"value1",
    50  				"value2",
    51  			},
    52  			"param2[]": {
    53  				"Param1",
    54  				"Param2",
    55  			},
    56  		},
    57  	},
    58  	{
    59  		inUrl: "/?param[0]=value1&param[1]=value2&param2[]=Param1&param2[]=Param2",
    60  		outMap: url.Values{
    61  			"param[]": {
    62  				"value1",
    63  				"value2",
    64  			},
    65  			"param2[]": {
    66  				"Param1",
    67  				"Param2",
    68  			},
    69  		},
    70  	},
    71  	{
    72  		inUrl: "/?single=asdf&param[0]=valuea&param[1]=valueb&param2[]=Parama&param2[]=Paramb",
    73  		outMap: url.Values{
    74  			"param[]": {
    75  				"valuea",
    76  				"valueb",
    77  			},
    78  			"param2[]": {
    79  				"Parama",
    80  				"Paramb",
    81  			},
    82  			"single": {
    83  				"asdf",
    84  			},
    85  		},
    86  	},
    87  	{
    88  		inUrl: "/?param[smth][0]=zero&param[smth][0][val]=val",
    89  		outMap: url.Values{
    90  			"param[smth][]": {
    91  				"zero",
    92  			},
    93  			"param[smth][][val]": {
    94  				"val",
    95  			},
    96  		},
    97  	},
    98  	{
    99  		inUrl: "/?param=value",
   100  		outMap: url.Values{
   101  			"param": {
   102  				"value",
   103  			},
   104  		},
   105  	},
   106  }
   107  
   108  func TestWithQueryNormalizer(t *testing.T) {
   109  	for _, tt := range testsData {
   110  		t.Run(tt.inUrl, func(t *testing.T) {
   111  			h := middleware.WithQueryNormalizer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   112  				res := map[string][]string(r.URL.Query())
   113  				for k, v := range res {
   114  					mm := tt.outMap[k]
   115  					assert.ElementsMatch(t, v, mm)
   116  				}
   117  
   118  			}))
   119  
   120  			req, _ := http.NewRequest("GET", tt.inUrl, nil)
   121  			resp := httptest.NewRecorder()
   122  
   123  			h.ServeHTTP(resp, req)
   124  		})
   125  	}
   126  }
   127  
   128  func BenchmarkWithVerySimpleQueryNormalizer(b *testing.B) {
   129  
   130  	for n := 0; n < b.N; n++ {
   131  		h := middleware.WithQueryNormalizer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
   132  
   133  		req, _ := http.NewRequest("GET", "/?param=val", nil)
   134  		resp := httptest.NewRecorder()
   135  
   136  		h.ServeHTTP(resp, req)
   137  	}
   138  }
   139  
   140  func BenchmarkWithQueryBigerNormalizer(b *testing.B) {
   141  
   142  	for n := 0; n < b.N; n++ {
   143  		h := middleware.WithQueryNormalizer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
   144  
   145  		req, _ := http.NewRequest("GET", "/?param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val", nil)
   146  		resp := httptest.NewRecorder()
   147  
   148  		h.ServeHTTP(resp, req)
   149  	}
   150  }