forked from aws/aws-sam-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_test.go
119 lines (101 loc) · 3.35 KB
/
event_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
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"github.com/awslabs/aws-sam-local/router"
"github.com/awslabs/goformation/cloudformation"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Event", func() {
Describe("PathParameters", func() {
var r *router.ServerlessRouter
BeforeEach(func() {
r = router.NewServerlessRouter(false)
})
Context("with path parameters on the route", func() {
function := &cloudformation.AWSServerlessFunction{
Runtime: "nodejs6.10",
Events: map[string]cloudformation.AWSServerlessFunction_EventSource{
"GetRequest": cloudformation.AWSServerlessFunction_EventSource{
Type: "Api",
Properties: &cloudformation.AWSServerlessFunction_Properties{
ApiEvent: &cloudformation.AWSServerlessFunction_ApiEvent{
Path: "/get",
Method: "get",
},
},
},
"GetRequestsWithParams": cloudformation.AWSServerlessFunction_EventSource{
Type: "Api",
Properties: &cloudformation.AWSServerlessFunction_Properties{
ApiEvent: &cloudformation.AWSServerlessFunction_ApiEvent{
Path: "/get/{parameter}",
Method: "get",
},
},
},
},
}
Context("and path parameters on the request", func() {
req, _ := http.NewRequest("GET", "/get/1", new(bytes.Buffer))
It("returns the parameters on the event", func() {
r.AddFunction(function, func(w http.ResponseWriter, r *http.Request) {
e, _ := NewEvent(r)
Expect(e.PathParameters).To(HaveKeyWithValue("parameter", "1"))
})
rec := httptest.NewRecorder()
r.Router().ServeHTTP(rec, req)
})
})
Context("and path parameters on the request", func() {
req, _ := http.NewRequest("GET", "/get/1", new(bytes.Buffer))
It("returns stage property with value \"prod\"", func() {
r.AddFunction(function, func(w http.ResponseWriter, r *http.Request) {
e, _ := NewEvent(r)
Expect(e.RequestContext.Stage).To(BeIdenticalTo("prod"))
})
rec := httptest.NewRecorder()
r.Router().ServeHTTP(rec, req)
})
})
Context("and no path parameters on the request", func() {
req, _ := http.NewRequest("GET", "/get", new(bytes.Buffer))
It("returns nil for PathParameters on the event", func() {
r.AddFunction(function, func(w http.ResponseWriter, r *http.Request) {
e, _ := NewEvent(r)
Expect(e.PathParameters).To(BeNil())
})
rec := httptest.NewRecorder()
r.Router().ServeHTTP(rec, req)
})
})
})
Context("with no parameters on the route", func() {
function := &cloudformation.AWSServerlessFunction{
Runtime: "nodejs6.10",
Events: map[string]cloudformation.AWSServerlessFunction_EventSource{
"GetRequest": cloudformation.AWSServerlessFunction_EventSource{
Type: "Api",
Properties: &cloudformation.AWSServerlessFunction_Properties{
ApiEvent: &cloudformation.AWSServerlessFunction_ApiEvent{
Path: "/get",
Method: "get",
},
},
},
},
}
req, _ := http.NewRequest("GET", "/get", new(bytes.Buffer))
It("returns nil for PathParameters on the event", func() {
r.AddFunction(function, func(w http.ResponseWriter, r *http.Request) {
e, _ := NewEvent(r)
Expect(e.PathParameters).To(BeNil())
})
rec := httptest.NewRecorder()
r.Router().ServeHTTP(rec, req)
})
})
})
})