forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting_test.go
110 lines (95 loc) · 2.64 KB
/
routing_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
package internal_test
import (
"net/http"
. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Routing", func() {
Describe("Route", func() {
var route Route
Describe("CreatePath", func() {
BeforeEach(func() {
route = Route{
Method: "GET",
Path: "/a/path/:param/with/:many_things/:many/in/:it",
}
})
It("should return a url with all :entries populated by the passed in hash", func() {
Expect(route.CreatePath(Params{
"param": "1",
"many_things": "2",
"many": "a space",
"it": "4",
})).Should(Equal(`/a/path/1/with/2/a%20space/in/4`))
})
When("the hash is missing params", func() {
It("should error", func() {
_, err := route.CreatePath(Params{
"param": "1",
"many": "2",
"it": "4",
})
Expect(err).Should(HaveOccurred())
})
})
When("the hash has extra params", func() {
It("should totally not care", func() {
Expect(route.CreatePath(Params{
"param": "1",
"many_things": "2",
"many": "a space",
"it": "4",
"donut": "bacon",
})).Should(Equal(`/a/path/1/with/2/a%20space/in/4`))
})
})
Context("with a trailing slash", func() {
It("should work", func() {
route = Route{
Method: "GET",
Path: "/a/path/:param/",
}
Expect(route.CreatePath(Params{
"param": "1",
})).Should(Equal(`/a/path/1/`))
})
})
})
})
Describe("Router", func() {
var (
router *Router
routes map[string]Route
baseURL string
)
JustBeforeEach(func() {
router = NewRouter(routes, baseURL)
})
Describe("CreateRequest", func() {
When("the route exists", func() {
var badRouteName, routeName string
BeforeEach(func() {
routeName = "banana"
badRouteName = "orange"
baseURL = "https://foo.bar.baz/this/is"
routes = map[string]Route{
routeName: {Path: "/very/good/:name", Method: http.MethodGet},
badRouteName: {Path: "/very/bad", Method: http.MethodGet},
}
})
It("returns a request", func() {
request, err := router.CreateRequest(routeName, Params{"name": "Henry the 8th"}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(request.URL.String()).To(Equal("https://foo.bar.baz/this/is/very/good/Henry%2520the%25208th"))
})
})
When("the route does not exist", func() {
It("returns an error", func() {
_, err := router.CreateRequest("fake-route", nil, nil)
Expect(err).To(MatchError("no route exists with the name fake-route"))
})
})
})
})
})