Skip to content

Commit 073c4df

Browse files
author
Marcus Rosén
committed
Removed handling of QueryStringParameters
Updates as per comments
1 parent c1664b9 commit 073c4df

File tree

2 files changed

+19
-92
lines changed

2 files changed

+19
-92
lines changed

core/request.go

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -118,36 +118,6 @@ func (r *RequestAccessor) ProxyEventToHTTPRequest(req events.APIGatewayProxyRequ
118118
decodedBody = base64Body
119119
}
120120

121-
queryString := ""
122-
if len(req.QueryStringParameters) > 0 {
123-
queryString = "?"
124-
queryCnt := 0
125-
for q := range req.QueryStringParameters {
126-
if queryCnt > 0 {
127-
queryString += "&"
128-
}
129-
queryString += url.QueryEscape(q) + "=" + url.QueryEscape(req.QueryStringParameters[q])
130-
queryCnt++
131-
}
132-
}
133-
if len(req.MultiValueQueryStringParameters) > 0 {
134-
queryDelim := "&"
135-
queryStart := false
136-
if queryString == "" {
137-
queryDelim = "?"
138-
queryStart = true
139-
}
140-
for q, l := range req.MultiValueQueryStringParameters {
141-
for _, v := range l {
142-
queryString += queryDelim + url.QueryEscape(q) + "=" + url.QueryEscape(v)
143-
if queryStart {
144-
queryDelim = "&"
145-
queryStart = false
146-
}
147-
}
148-
}
149-
}
150-
151121
path := req.Path
152122
if r.stripBasePath != "" && len(r.stripBasePath) > 1 {
153123
if strings.HasPrefix(path, r.stripBasePath) {
@@ -161,12 +131,24 @@ func (r *RequestAccessor) ProxyEventToHTTPRequest(req events.APIGatewayProxyRequ
161131
if customAddress, ok := os.LookupEnv(CustomHostVariable); ok {
162132
serverAddress = customAddress
163133
}
164-
165134
path = serverAddress + path
166135

136+
if len(req.MultiValueQueryStringParameters) > 0 {
137+
queryString := ""
138+
for q, l := range req.MultiValueQueryStringParameters {
139+
for _, v := range l {
140+
if queryString != "" {
141+
queryString += "&"
142+
}
143+
queryString += url.QueryEscape(q) + "=" + url.QueryEscape(v)
144+
}
145+
}
146+
path += "?" + queryString
147+
}
148+
167149
httpRequest, err := http.NewRequest(
168150
strings.ToUpper(req.HTTPMethod),
169-
path+queryString,
151+
path,
170152
bytes.NewReader(decodedBody),
171153
)
172154

core/request_test.go

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ var _ = Describe("RequestAccessor tests", func() {
5858
})
5959

6060
qsRequest := getProxyRequest("/hello", "GET")
61-
qsRequest.QueryStringParameters = map[string]string{
62-
"hello": "1",
63-
"world": "2",
61+
qsRequest.MultiValueQueryStringParameters = map[string][]string{
62+
"hello": {"1"},
63+
"world": {"2", "3"},
6464
}
6565
It("Populates query string correctly", func() {
6666
httpReq, err := accessor.ProxyEventToHTTPRequest(qsRequest)
@@ -73,65 +73,10 @@ var _ = Describe("RequestAccessor tests", func() {
7373
Expect(query["hello"]).ToNot(BeNil())
7474
Expect(query["world"]).ToNot(BeNil())
7575
Expect(1).To(Equal(len(query["hello"])))
76-
Expect(1).To(Equal(len(query["world"])))
77-
Expect("1").To(Equal(query["hello"][0]))
78-
Expect("2").To(Equal(query["world"][0]))
79-
})
80-
81-
mvqsRequest := getProxyRequest("/hello", "GET")
82-
mvqsRequest.MultiValueQueryStringParameters = map[string][]string{
83-
"hello": {"1", "2"},
84-
"world": {"3", "4"},
85-
}
86-
It("Populates multi value query string correctly", func() {
87-
httpReq, err := accessor.ProxyEventToHTTPRequest(mvqsRequest)
88-
Expect(err).To(BeNil())
89-
Expect("/hello").To(Equal(httpReq.URL.Path))
90-
Expect("GET").To(Equal(httpReq.Method))
91-
92-
query := httpReq.URL.Query()
93-
Expect(2).To(Equal(len(query)))
94-
Expect(query["hello"]).ToNot(BeNil())
95-
Expect(query["world"]).ToNot(BeNil())
96-
Expect(2).To(Equal(len(query["hello"])))
9776
Expect(2).To(Equal(len(query["world"])))
9877
Expect("1").To(Equal(query["hello"][0]))
99-
Expect("2").To(Equal(query["hello"][1]))
100-
Expect("3").To(Equal(query["world"][0]))
101-
Expect("4").To(Equal(query["world"][1]))
102-
})
103-
104-
qsAndMVQSRequest := getProxyRequest("/hello", "GET")
105-
qsAndMVQSRequest.QueryStringParameters = map[string]string{
106-
"hello1": "1",
107-
"world1": "2",
108-
}
109-
qsAndMVQSRequest.MultiValueQueryStringParameters = map[string][]string{
110-
"hello2": {"3", "4"},
111-
"world2": {"5", "6"},
112-
}
113-
It("Populates query string and multi value query string correctly", func() {
114-
httpReq, err := accessor.ProxyEventToHTTPRequest(qsAndMVQSRequest)
115-
Expect(err).To(BeNil())
116-
Expect("/hello").To(Equal(httpReq.URL.Path))
117-
Expect("GET").To(Equal(httpReq.Method))
118-
119-
query := httpReq.URL.Query()
120-
Expect(4).To(Equal(len(query)))
121-
Expect(query["hello1"]).ToNot(BeNil())
122-
Expect(query["world1"]).ToNot(BeNil())
123-
Expect(query["hello2"]).ToNot(BeNil())
124-
Expect(query["world2"]).ToNot(BeNil())
125-
Expect(1).To(Equal(len(query["hello1"])))
126-
Expect(1).To(Equal(len(query["world1"])))
127-
Expect(2).To(Equal(len(query["hello2"])))
128-
Expect(2).To(Equal(len(query["world2"])))
129-
Expect("1").To(Equal(query["hello1"][0]))
130-
Expect("2").To(Equal(query["world1"][0]))
131-
Expect("3").To(Equal(query["hello2"][0]))
132-
Expect("4").To(Equal(query["hello2"][1]))
133-
Expect("5").To(Equal(query["world2"][0]))
134-
Expect("6").To(Equal(query["world2"][1]))
78+
Expect("2").To(Equal(query["world"][0]))
79+
Expect("3").To(Equal(query["world"][1]))
13580
})
13681

13782
basePathRequest := getProxyRequest("/app1/orders", "GET")

0 commit comments

Comments
 (0)