github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/mux/regexp_test.go (about)

     1  package mux
     2  
     3  import (
     4  	"net/url"
     5  	"reflect"
     6  	"strconv"
     7  	"testing"
     8  )
     9  
    10  func Test_findFirstQueryKey(t *testing.T) {
    11  	tests := []string{
    12  		"a=1&b=2",
    13  		"a=1&a=2&a=banana",
    14  		"ascii=%3Ckey%3A+0x90%3E",
    15  		"a=1;b=2",
    16  		"a=1&a=2;a=banana",
    17  		"a==",
    18  		"a=%2",
    19  		"a=20&%20%3F&=%23+%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09:%2F@$%27%28%29%2A%2C%3B&a=30",
    20  		"a=1& ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;&a=5",
    21  		"a=xxxxxxxxxxxxxxxx&b=YYYYYYYYYYYYYYY&c=ppppppppppppppppppp&f=ttttttttttttttttt&a=uuuuuuuuuuuuu",
    22  	}
    23  	for _, query := range tests {
    24  		t.Run(query, func(t *testing.T) {
    25  			// Check against url.ParseQuery, ignoring the error.
    26  			all, _ := url.ParseQuery(query)
    27  			for key, want := range all {
    28  				t.Run(key, func(t *testing.T) {
    29  					got, ok := findFirstQueryKey(query, key)
    30  					if !ok {
    31  						t.Error("Did not get expected key", key)
    32  					}
    33  					if !reflect.DeepEqual(got, want[0]) {
    34  						t.Errorf("findFirstQueryKey(%s,%s) = %v, want %v", query, key, got, want[0])
    35  					}
    36  				})
    37  			}
    38  		})
    39  	}
    40  }
    41  
    42  func Benchmark_findQueryKey(b *testing.B) {
    43  	tests := []string{
    44  		"a=1&b=2",
    45  		"ascii=%3Ckey%3A+0x90%3E",
    46  		"a=20&%20%3F&=%23+%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09:%2F@$%27%28%29%2A%2C%3B&a=30",
    47  		"a=xxxxxxxxxxxxxxxx&bbb=YYYYYYYYYYYYYYY&cccc=ppppppppppppppppppp&ddddd=ttttttttttttttttt&a=uuuuuuuuuuuuu",
    48  		"a=;b=;c=;d=;e=;f=;g=;h=;i=,j=;k=",
    49  	}
    50  	for i, query := range tests {
    51  		b.Run(strconv.Itoa(i), func(b *testing.B) {
    52  			// Check against url.ParseQuery, ignoring the error.
    53  			all, _ := url.ParseQuery(query)
    54  			b.ReportAllocs()
    55  			b.ResetTimer()
    56  			for i := 0; i < b.N; i++ {
    57  				for key, _ := range all {
    58  					_, _ = findFirstQueryKey(query, key)
    59  				}
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func Benchmark_findQueryKeyGoLib(b *testing.B) {
    66  	tests := []string{
    67  		"a=1&b=2",
    68  		"ascii=%3Ckey%3A+0x90%3E",
    69  		"a=20&%20%3F&=%23+%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09:%2F@$%27%28%29%2A%2C%3B&a=30",
    70  		"a=xxxxxxxxxxxxxxxx&bbb=YYYYYYYYYYYYYYY&cccc=ppppppppppppppppppp&ddddd=ttttttttttttttttt&a=uuuuuuuuuuuuu",
    71  		"a=;b=;c=;d=;e=;f=;g=;h=;i=,j=;k=",
    72  	}
    73  	for i, query := range tests {
    74  		b.Run(strconv.Itoa(i), func(b *testing.B) {
    75  			// Check against url.ParseQuery, ignoring the error.
    76  			all, _ := url.ParseQuery(query)
    77  			var u url.URL
    78  			u.RawQuery = query
    79  			b.ReportAllocs()
    80  			b.ResetTimer()
    81  			for i := 0; i < b.N; i++ {
    82  				for key, _ := range all {
    83  					v := u.Query()[key]
    84  					if len(v) > 0 {
    85  						_ = v[0]
    86  					}
    87  				}
    88  			}
    89  		})
    90  	}
    91  }