realmpath_test.gno
4.74 Kb ยท 153 lines
1package realmpath_test
2
3import (
4 "net/url"
5 "std"
6 "testing"
7
8 "gno.land/p/demo/uassert"
9 "gno.land/p/demo/urequire"
10 "gno.land/p/moul/realmpath"
11)
12
13func TestExample(t *testing.T) {
14 cd := std.ChainDomain()
15 testing.SetRealm(std.NewCodeRealm(cd + "/r/lorem/ipsum"))
16
17 // initial parsing
18 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")
25
26 // alter query
27 req.Query.Set("hey", "salut")
28 uassert.Equal(t, req.String(), "/r/lorem/ipsum:hello/world?baz=foobar&foo=bar&hey=salut")
29
30 // alter path
31 req.Path = "bye/ciao"
32 uassert.Equal(t, req.String(), "/r/lorem/ipsum:bye/ciao?baz=foobar&foo=bar&hey=salut")
33}
34
35func TestParse(t *testing.T) {
36 cd := std.ChainDomain()
37 testing.SetRealm(std.NewCodeRealm(cd + "/r/lorem/ipsum"))
38
39 tests := []struct {
40 rawPath string
41 realm string // optional
42 expectedPath string
43 expectedQuery url.Values
44 expectedString string
45 }{
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: 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 }
140
141 for _, tt := range tests {
142 t.Run(tt.rawPath, func(t *testing.T) {
143 req := realmpath.Parse(tt.rawPath)
144 req.Realm = tt.realm // set optional realm
145 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}