github.com/prysmaticlabs/prysm@v1.4.4/shared/gateway/param_handling_test.go (about) 1 package gateway 2 3 import ( 4 "bytes" 5 "net/http/httptest" 6 "testing" 7 8 "github.com/gorilla/mux" 9 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 10 "github.com/prysmaticlabs/prysm/shared/testutil/require" 11 ) 12 13 func TestHandleURLParameters(t *testing.T) { 14 var body bytes.Buffer 15 16 t.Run("no_params", func(t *testing.T) { 17 request := httptest.NewRequest("GET", "http://foo.example/bar", &body) 18 19 errJson := HandleURLParameters("/not_param", request, []string{}) 20 require.Equal(t, true, errJson == nil) 21 assert.Equal(t, "/bar", request.URL.Path) 22 }) 23 24 t.Run("with_params", func(t *testing.T) { 25 muxVars := make(map[string]string) 26 muxVars["bar_param"] = "bar" 27 muxVars["quux_param"] = "quux" 28 request := httptest.NewRequest("GET", "http://foo.example/bar/baz/quux", &body) 29 request = mux.SetURLVars(request, muxVars) 30 31 errJson := HandleURLParameters("/{bar_param}/not_param/{quux_param}", request, []string{}) 32 require.Equal(t, true, errJson == nil) 33 assert.Equal(t, "/YmFy/baz/cXV1eA==", request.URL.Path) 34 }) 35 36 t.Run("with_literal", func(t *testing.T) { 37 muxVars := make(map[string]string) 38 muxVars["bar_param"] = "bar" 39 request := httptest.NewRequest("GET", "http://foo.example/bar/baz", &body) 40 request = mux.SetURLVars(request, muxVars) 41 42 errJson := HandleURLParameters("/{bar_param}/not_param/", request, []string{"bar_param"}) 43 require.Equal(t, true, errJson == nil) 44 assert.Equal(t, "/bar/baz", request.URL.Path) 45 }) 46 47 t.Run("with_hex", func(t *testing.T) { 48 muxVars := make(map[string]string) 49 muxVars["hex_param"] = "0x626172" 50 request := httptest.NewRequest("GET", "http://foo.example/0x626172/baz", &body) 51 request = mux.SetURLVars(request, muxVars) 52 53 errJson := HandleURLParameters("/{hex_param}/not_param/", request, []string{}) 54 require.Equal(t, true, errJson == nil) 55 assert.Equal(t, "/YmFy/baz", request.URL.Path) 56 }) 57 } 58 59 func TestHandleQueryParameters(t *testing.T) { 60 var body bytes.Buffer 61 62 t.Run("regular_params", func(t *testing.T) { 63 request := httptest.NewRequest("GET", "http://foo.example?bar=bar&baz=baz", &body) 64 65 errJson := HandleQueryParameters(request, []QueryParam{{Name: "bar"}, {Name: "baz"}}) 66 require.Equal(t, true, errJson == nil) 67 query := request.URL.Query() 68 v, ok := query["bar"] 69 require.Equal(t, true, ok, "query param not found") 70 require.Equal(t, 1, len(v), "wrong number of query param values") 71 assert.Equal(t, "bar", v[0]) 72 v, ok = query["baz"] 73 require.Equal(t, true, ok, "query param not found") 74 require.Equal(t, 1, len(v), "wrong number of query param values") 75 assert.Equal(t, "baz", v[0]) 76 }) 77 78 t.Run("hex_and_enum_params", func(t *testing.T) { 79 request := httptest.NewRequest("GET", "http://foo.example?hex=0x626172&baz=baz", &body) 80 81 errJson := HandleQueryParameters(request, []QueryParam{{Name: "hex", Hex: true}, {Name: "baz", Enum: true}}) 82 require.Equal(t, true, errJson == nil) 83 query := request.URL.Query() 84 v, ok := query["hex"] 85 require.Equal(t, true, ok, "query param not found") 86 require.Equal(t, 1, len(v), "wrong number of query param values") 87 assert.Equal(t, "YmFy", v[0]) 88 v, ok = query["baz"] 89 require.Equal(t, true, ok, "query param not found") 90 require.Equal(t, 1, len(v), "wrong number of query param values") 91 assert.Equal(t, "BAZ", v[0]) 92 }) 93 } 94 95 func TestIsRequestParam(t *testing.T) { 96 tests := []struct { 97 s string 98 b bool 99 }{ 100 {"", false}, 101 {"{", false}, 102 {"}", false}, 103 {"{}", false}, 104 {"{x}", true}, 105 {"{very_long_parameter_name_with_underscores}", true}, 106 } 107 for _, tt := range tests { 108 b := isRequestParam(tt.s) 109 assert.Equal(t, tt.b, b) 110 } 111 } 112 113 func TestNormalizeQueryValues(t *testing.T) { 114 input := make(map[string][]string) 115 input["key"] = []string{"value1", "value2,value3,value4", "value5"} 116 117 normalizeQueryValues(input) 118 require.Equal(t, 5, len(input["key"])) 119 assert.Equal(t, "value1", input["key"][0]) 120 assert.Equal(t, "value2", input["key"][1]) 121 assert.Equal(t, "value3", input["key"][2]) 122 assert.Equal(t, "value4", input["key"][3]) 123 assert.Equal(t, "value5", input["key"][4]) 124 }