-
Notifications
You must be signed in to change notification settings - Fork 87
/
id_test.go
57 lines (43 loc) · 1.27 KB
/
id_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
package jsonschema_test
import (
"testing"
"github.com/invopop/jsonschema"
"github.com/stretchr/testify/assert"
)
func TestID(t *testing.T) {
base := "https://invopop.com/schema"
id := jsonschema.ID(base)
assert.Equal(t, base, id.String())
id = id.Add("user")
assert.EqualValues(t, base+"/user", id)
id = id.Anchor("Name")
assert.EqualValues(t, base+"/user#Name", id)
id = id.Anchor("Title")
assert.EqualValues(t, base+"/user#Title", id)
id = id.Def("Name")
assert.EqualValues(t, base+"/user#/$defs/Name", id)
}
func TestIDValidation(t *testing.T) {
id := jsonschema.ID("https://invopop.com/schema/user")
assert.NoError(t, id.Validate())
id = "https://encoding/json"
if assert.Error(t, id.Validate()) {
assert.Contains(t, id.Validate().Error(), "hostname does not look valid")
}
id = "time"
if assert.Error(t, id.Validate()) {
assert.Contains(t, id.Validate().Error(), "hostname")
}
id = "http://invopop.com"
if assert.Error(t, id.Validate()) {
assert.Contains(t, id.Validate().Error(), "path")
}
id = "foor://invopop.com/schema/user"
if assert.Error(t, id.Validate()) {
assert.Contains(t, id.Validate().Error(), "schema")
}
id = "invopop.com\n/test"
if assert.Error(t, id.Validate()) {
assert.Contains(t, id.Validate().Error(), "invalid URL")
}
}