forked from lestrrat-go/jsref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsref_test.go
204 lines (179 loc) · 4.07 KB
/
jsref_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package jsref_test
import (
"encoding/json"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path/filepath"
"sort"
"strconv"
"testing"
"time"
"github.com/lestrrat/go-jsref"
"github.com/lestrrat/go-jsref/provider"
"github.com/stretchr/testify/assert"
)
func TestResolveMemory(t *testing.T) {
m := map[string]interface{}{
"foo": []interface{}{
"bar",
map[string]interface{}{
"$ref": "#/sub",
},
map[string]interface{}{
"$ref": "obj2#/sub",
},
},
"sub": "baz",
}
data := map[string]string{
"#/foo/0": "bar",
"#/foo/1": "baz",
"#/foo/2": "quux",
}
res := jsref.New()
mp := provider.NewMap()
mp.Set("obj2", map[string]string{"sub": "quux"})
res.AddProvider(mp)
ptrlist := make([]string, 0, len(data))
for ptr := range data {
ptrlist = append(ptrlist, ptr)
}
sort.Strings(ptrlist)
for _, ptr := range ptrlist {
expected := data[ptr]
v, err := res.Resolve(m, ptr)
if !assert.NoError(t, err, "Resolve(%s) should succeed", ptr) {
return
}
if !assert.Equal(t, v, expected, "Resolve(%s) resolves to '%s'", ptr, expected) {
return
}
}
}
func TestResolveFS(t *testing.T) {
dir, err := ioutil.TempDir("", "jsref-test-")
if !assert.NoError(t, err, "creating temporary directory should succeed") {
return
}
defer os.RemoveAll(dir)
path := filepath.Join(dir, "obj2")
f, err := os.Create(path)
if !assert.NoError(t, err, "creating %s file should succeed", path) {
return
}
f.Write([]byte(`{"sub":"quux"}`))
f.Close()
m := map[string]interface{}{
"foo": []interface{}{
"bar",
map[string]interface{}{
"$ref": "#/sub",
},
map[string]interface{}{
"$ref": "file:///obj2#/sub",
},
},
"sub": "baz",
}
data := map[string]string{
"#/foo/0": "bar",
"#/foo/1": "baz",
"#/foo/2": "quux",
}
res := jsref.New()
res.AddProvider(provider.NewFS(dir))
ptrlist := make([]string, 0, len(data))
for ptr := range data {
ptrlist = append(ptrlist, ptr)
}
sort.Strings(ptrlist)
for _, ptr := range ptrlist {
expected := data[ptr]
v, err := res.Resolve(m, ptr)
if !assert.NoError(t, err, "Resolve(%s) should succeed", ptr) {
return
}
if !assert.Equal(t, v, expected, "Resolve(%s) resolves to '%s'", ptr, expected) {
return
}
}
}
func TestResolveHTTP(t *testing.T) {
if b, _ := strconv.ParseBool(os.Getenv("JSREF_LIVE_TESTS")); !b {
t.Skip("JSREF_LIVE_TESTS is not available, skipping test")
}
cl := http.Client{
Transport: &http.Transport{
Dial: func(n, a string) (net.Conn, error) {
return net.DialTimeout(n, a, 2*time.Second)
},
},
}
const schemaURL = `http://json-schema.org/draft-04/schema#`
if _, err := cl.Get(schemaURL); err != nil {
t.Skip("JSON schema '" + schemaURL + "' unavailable, skipping test")
}
res := jsref.New()
hp := provider.NewHTTP()
res.AddProvider(hp)
m := map[string]interface{}{
"fetch": map[string]string{
"$ref": schemaURL,
},
}
ptr := "#/fetch"
v, err := res.Resolve(m, ptr)
if !assert.NoError(t, err, "Resolve(%s) should succeed", ptr) {
return
}
switch v.(type) {
case map[string]interface{}:
mv := v.(map[string]interface{})
if !assert.Equal(t, mv["id"], schemaURL, "Resolve("+schemaURL+") resolved to JSON schema") {
return
}
default:
t.Errorf("Expected map[string]interface{}")
}
}
func TestResolveRecursive(t *testing.T) {
var v interface{}
src := []byte(`
{
"foo": {
"type": "array",
"items": { "$ref": "#" }
}
}`)
if err := json.Unmarshal(src, &v); err != nil {
log.Printf("%s", err)
return
}
res := jsref.New()
_, err := res.Resolve(v, "#/foo") // "bar"
if !assert.NoError(t, err, "res.Resolve should succeed") {
return
}
}
func TestGHPR12(t *testing.T) {
// https://github.com/lestrrat/go-jsref/pull/2 gave me an example
// using "foo" as the JS pointer (could've been a typo)
// but it gave me weird results, so this is where I'm testing it
var v interface{}
src := []byte(`
{
"foo": "bar"
}`)
if err := json.Unmarshal(src, &v); err != nil {
log.Printf("%s", err)
return
}
res := jsref.New()
_, err := res.Resolve(v, "foo")
if !assert.NoError(t, err, "res.Resolve should fail") {
return
}
}