-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
traverse_test.go
72 lines (54 loc) · 2.75 KB
/
traverse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package vulcain
import (
"net/url"
"testing"
"github.com/dunglas/httpsfv"
"github.com/stretchr/testify/assert"
)
func TestUnescape(t *testing.T) {
assert.Equal(t, "~1/0*/", unescape("~01~10~2/"))
}
func TestUrlRewriter(t *testing.T) {
n := &node{}
n.importPointers(preload, httpsfv.List{httpsfv.NewItem("/foo/*"), httpsfv.NewItem("/bar/baz")})
n.importPointers(fields, httpsfv.List{httpsfv.NewItem("/foo/*"), httpsfv.NewItem("/baz/bar")})
u, _ := url.Parse("/test")
urlRewriter(u, n)
assert.Equal(t, "/test?fields=%22%2Ffoo%2F%2A%22%2C+%22%2Fbaz%2Fbar%22&preload=%22%2Ffoo%2F%2A%22%2C+%22%2Fbar%2Fbaz%22", u.String())
}
func urlRewriteRelationHandler(n *node, v string) string {
u, _ := url.Parse(v)
urlRewriter(u, n)
return u.String()
}
func TestTraverseJSONFields(t *testing.T) {
n := &node{}
n.importPointers(fields, httpsfv.List{httpsfv.NewItem("/notexist"), httpsfv.NewItem("/bar")})
result := New().traverseJSON([]byte(`{"foo": "f", "bar": "b"}`), n, true, urlRewriteRelationHandler)
assert.Equal(t, `{"bar":"b"}`, string(result))
}
func TestTraverseJSONFieldsRewriteURL(t *testing.T) {
n := &node{}
n.importPointers(fields, httpsfv.List{httpsfv.NewItem("/foo/*/bar")})
result := New().traverseJSON([]byte(`{"foo": ["/a", "/b"]}`), n, true, urlRewriteRelationHandler)
assert.Equal(t, `{"foo":["/a?fields=%22%2Fbar%22","/b?fields=%22%2Fbar%22"]}`, string(result))
}
func TestTraverseJSONPreload(t *testing.T) {
n := &node{}
n.importPointers(preload, httpsfv.List{httpsfv.NewItem("/notexist"), httpsfv.NewItem("/bar")})
result := New().traverseJSON([]byte(`{"foo": "/foo", "bar": "/bar"}`), n, false, urlRewriteRelationHandler)
assert.Equal(t, `{"foo": "/foo", "bar": "/bar"}`, string(result))
}
func TestTraverseJSONPreloadRewriteURL(t *testing.T) {
n := &node{}
n.importPointers(preload, httpsfv.List{httpsfv.NewItem("/foo/*/rel"), httpsfv.NewItem("/bar/baz")})
result := New().traverseJSON([]byte(`{"foo": ["/a", "/b"], "bar": "/bar"}`), n, false, urlRewriteRelationHandler)
assert.Equal(t, `{"foo": ["/a?preload=%22%2Frel%22", "/b?preload=%22%2Frel%22"], "bar": "/bar?preload=%22%2Fbaz%22"}`, string(result))
}
func TestTraverseJSONPreloadAndFieldsRewriteURL(t *testing.T) {
n := &node{}
n.importPointers(preload, httpsfv.List{httpsfv.NewItem("/notexist"), httpsfv.NewItem("/foo/*/rel"), httpsfv.NewItem("/bar/baz"), httpsfv.NewItem("/baz")})
n.importPointers(fields, httpsfv.List{httpsfv.NewItem("/foo/*"), httpsfv.NewItem("/bar/baz"), httpsfv.NewItem("/notexist")})
result := New().traverseJSON([]byte(`{"foo": ["/a", "/b"], "bar": "/bar", "baz": "/baz"}`), n, true, urlRewriteRelationHandler)
assert.Equal(t, `{"foo":["/a?preload=%22%2Frel%22","/b?preload=%22%2Frel%22"],"bar":"/bar?fields=%22%2Fbaz%22\u0026preload=%22%2Fbaz%22"}`, string(result))
}