1package realmpath_test23import (4 "chain/runtime"5 "net/url"6 "testing"78 "gno.land/p/moul/realmpath/v0"9 "gno.land/p/nt/uassert/v0"10 "gno.land/p/nt/urequire/v0"11)1213func TestExample(t *testing.T) {14 cd := runtime.ChainDomain()15 testing.SetRealm(testing.NewCodeRealm(cd + "/r/lorem/ipsum"))1617 // initial parsing18 path := "hello/world?foo=bar&baz=foobar"19 req := realmpath.Parse(path)20 urequire.False(t, req == nil, "req should not be nil")21 uassert.Equal(t, req.Path, "hello/world")22 uassert.Equal(t, req.Query.Get("foo"), "bar")23 uassert.Equal(t, req.Query.Get("baz"), "foobar")24 uassert.Equal(t, req.String(), "/r/lorem/ipsum:hello/world?baz=foobar&foo=bar")2526 // alter query27 req.Query.Set("hey", "salut")28 uassert.Equal(t, req.String(), "/r/lorem/ipsum:hello/world?baz=foobar&foo=bar&hey=salut")2930 // alter path31 req.Path = "bye/ciao"32 uassert.Equal(t, req.String(), "/r/lorem/ipsum:bye/ciao?baz=foobar&foo=bar&hey=salut")33}3435func TestParse(t *testing.T) {36 cd := runtime.ChainDomain()37 testing.SetRealm(testing.NewCodeRealm(cd + "/r/lorem/ipsum"))3839 tests := []struct {40 rawPath string41 realm_XXX string // optional42 expectedPath string43 expectedQuery url.Values44 expectedString string45 }{46 {47 rawPath: "hello/world?foo=bar&baz=foobar",48 expectedPath: "hello/world",49 expectedQuery: url.Values{50 "foo": []string{"bar"},51 "baz": []string{"foobar"},52 },53 expectedString: "/r/lorem/ipsum:hello/world?baz=foobar&foo=bar",54 },55 {56 rawPath: "api/v1/resource?search=test&limit=10",57 expectedPath: "api/v1/resource",58 expectedQuery: url.Values{59 "search": []string{"test"},60 "limit": []string{"10"},61 },62 expectedString: "/r/lorem/ipsum:api/v1/resource?limit=10&search=test",63 },64 {65 rawPath: "singlepath",66 expectedPath: "singlepath",67 expectedQuery: url.Values{},68 expectedString: "/r/lorem/ipsum:singlepath",69 },70 {71 rawPath: "path/with/trailing/slash/",72 expectedPath: "path/with/trailing/slash/",73 expectedQuery: url.Values{},74 expectedString: "/r/lorem/ipsum:path/with/trailing/slash",75 },76 {77 rawPath: "emptyquery?",78 expectedPath: "emptyquery",79 expectedQuery: url.Values{},80 expectedString: "/r/lorem/ipsum:emptyquery",81 },82 {83 rawPath: "path/with/special/characters/?key=val%20ue&anotherKey=with%21special%23chars",84 expectedPath: "path/with/special/characters/",85 expectedQuery: url.Values{86 "key": []string{"val ue"},87 "anotherKey": []string{"with!special#chars"},88 },89 expectedString: "/r/lorem/ipsum:path/with/special/characters?anotherKey=with%21special%23chars&key=val+ue",90 },91 {92 rawPath: "path/with/empty/key?keyEmpty&=valueEmpty",93 expectedPath: "path/with/empty/key",94 expectedQuery: url.Values{95 "keyEmpty": []string{""},96 "": []string{"valueEmpty"},97 },98 expectedString: "/r/lorem/ipsum:path/with/empty/key?=valueEmpty&keyEmpty=",99 },100 {101 rawPath: "path/with/multiple/empty/keys?=empty1&=empty2",102 expectedPath: "path/with/multiple/empty/keys",103 expectedQuery: url.Values{104 "": []string{"empty1", "empty2"},105 },106 expectedString: "/r/lorem/ipsum:path/with/multiple/empty/keys?=empty1&=empty2",107 },108 {109 rawPath: "path/with/percent-encoded/%20space?query=hello%20world",110 expectedPath: "path/with/percent-encoded/%20space", // XXX: should we decode?111 expectedQuery: url.Values{112 "query": []string{"hello world"},113 },114 expectedString: "/r/lorem/ipsum:path/with/percent-encoded/%20space?query=hello+world",115 },116 {117 rawPath: "path/with/very/long/query?key1=value1&key2=value2&key3=value3&key4=value4&key5=value5&key6=value6",118 expectedPath: "path/with/very/long/query",119 expectedQuery: url.Values{120 "key1": []string{"value1"},121 "key2": []string{"value2"},122 "key3": []string{"value3"},123 "key4": []string{"value4"},124 "key5": []string{"value5"},125 "key6": []string{"value6"},126 },127 expectedString: "/r/lorem/ipsum:path/with/very/long/query?key1=value1&key2=value2&key3=value3&key4=value4&key5=value5&key6=value6",128 },129 {130 rawPath: "custom/realm?foo=bar&baz=foobar",131 realm_XXX: cd + "/r/foo/bar",132 expectedPath: "custom/realm",133 expectedQuery: url.Values{134 "foo": []string{"bar"},135 "baz": []string{"foobar"},136 },137 expectedString: "/r/foo/bar:custom/realm?baz=foobar&foo=bar",138 },139 }140141 for _, tt := range tests {142 t.Run(tt.rawPath, func(t *testing.T) {143 req := realmpath.Parse(tt.rawPath)144 req.Realm = tt.realm_XXX // set optional realm145 urequire.False(t, req == nil, "req should not be nil")146 uassert.Equal(t, req.Path, tt.expectedPath)147 urequire.Equal(t, len(req.Query), len(tt.expectedQuery))148 uassert.Equal(t, req.Query.Encode(), tt.expectedQuery.Encode())149 // XXX: uassert.Equal(t, req.Query, tt.expectedQuery)150 uassert.Equal(t, req.String(), tt.expectedString)151 })152 }153}154Signatures reconstructed verbatim from vm/qfuncs — interface params keep their inline definitions.