-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_collection_test.go
90 lines (81 loc) · 2.57 KB
/
resource_collection_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
package rest_test
import (
"reflect"
"sort"
"testing"
"github.com/ehsoc/rest"
)
func TestGetResources(t *testing.T) {
r := rest.ResourceCollection{}
r.Resource("car", func(r *rest.Resource) {
r.Resource("fiat", nil)
r.Resource("citroen", nil)
r.Resource("ford", nil)
})
rootRs := r.Resources()
rs := rootRs[0].Resources()
sort.Slice(rs, func(i, j int) bool {
return rs[i].Path() < rs[j].Path()
})
if len(rs) != 3 {
t.Errorf("got: %v want: %v", len(rs), 3)
}
assertStringEqual(t, rs[0].Path(), "citroen")
assertStringEqual(t, rs[1].Path(), "fiat")
assertStringEqual(t, rs[2].Path(), "ford")
}
func TestResource(t *testing.T) {
collection := rest.ResourceCollection{}
collection.Resource("find", func(r *rest.Resource) {
r.Resource("left", func(r *rest.Resource) {
})
r.Resource("right", func(r *rest.Resource) {
})
})
findNode := collection.Resources()[0]
if findNode.Path() != "find" {
t.Errorf("got : %v want: %v", findNode.Path(), "find")
}
if len(findNode.Resources()) != 2 {
t.Fatalf("expecting 2 sub nodes got: %v", len(findNode.Resources()))
}
directionResources := findNode.Resources()
sort.Slice(directionResources, func(i, j int) bool {
return directionResources[i].Path() < directionResources[j].Path()
})
if directionResources[0].Path() != "left" {
t.Errorf("got : %v want: %v", findNode.Resources()[0].Path(), "left")
}
if directionResources[1].Path() != "right" {
t.Errorf("got : %v want: %v", findNode.Resources()[1].Path(), "right")
}
}
func TestUriParamResource(t *testing.T) {
collection := rest.ResourceCollection{}
carIDParam := rest.NewURIParameter("carId", reflect.String)
mo := rest.NewMethodOperation(&OperationStub{}, rest.NewResponse(200))
collection.ResourceP(carIDParam, func(r *rest.Resource) {
r.Resource("left", func(r *rest.Resource) {
r.Get(mo, mustGetJSONContentType()).AddParameter(carIDParam)
})
r.Resource("right", func(r *rest.Resource) {
})
})
findNode := collection.Resources()[0]
if findNode.Path() != "{carId}" {
t.Errorf("got : %v want: %v", findNode.Path(), "{carId}")
}
if len(findNode.Resources()) != 2 {
t.Fatalf("expecting 2 sub nodes got: %v", len(findNode.Resources()))
}
directionResources := findNode.Resources()
sort.Slice(directionResources, func(i, j int) bool {
return directionResources[i].Path() < directionResources[j].Path()
})
if directionResources[0].Path() != "left" {
t.Errorf("got : %v want: %v", findNode.Resources()[0].Path(), "left")
}
if directionResources[1].Path() != "right" {
t.Errorf("got : %v want: %v", findNode.Resources()[1].Path(), "right")
}
}