Skip to content

Commit 2a487f5

Browse files
authored
Merge pull request awslabs#96 from youpy/header-set
Use `Header.Set` instead of `Header.Add`
2 parents b60516f + e56d569 commit 2a487f5

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

core/request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,13 @@ func addToHeader(req *http.Request, apiGwRequest events.APIGatewayProxyRequest)
213213
log.Println("Could not marshal stage variables for custom header")
214214
return nil, err
215215
}
216-
req.Header.Add(APIGwStageVarsHeader, string(stageVars))
216+
req.Header.Set(APIGwStageVarsHeader, string(stageVars))
217217
apiGwContext, err := json.Marshal(apiGwRequest.RequestContext)
218218
if err != nil {
219219
log.Println("Could not Marshal API GW context for custom header")
220220
return req, err
221221
}
222-
req.Header.Add(APIGwContextHeader, string(apiGwContext))
222+
req.Header.Set(APIGwContextHeader, string(apiGwContext))
223223
return req, nil
224224
}
225225

core/request_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ var _ = Describe("RequestAccessor tests", func() {
217217
// should fail because using header proxy method
218218
Expect(ok).To(BeFalse())
219219

220+
// overwrite existing context header
221+
contextRequestWithHeaders := getProxyRequest("orders", "GET")
222+
contextRequestWithHeaders.RequestContext = getRequestContext()
223+
contextRequestWithHeaders.Headers = map[string]string{core.APIGwContextHeader: `{"AccountID":"abc123"}`}
224+
httpReq, err = accessor.ProxyEventToHTTPRequest(contextRequestWithHeaders)
225+
Expect(err).To(BeNil())
226+
headerContext, err = accessor.GetAPIGatewayContext(httpReq)
227+
Expect(err).To(BeNil())
228+
Expect(headerContext.AccountID).To(Equal("x"))
229+
220230
httpReq, err = accessor.EventToRequestWithContext(context.Background(), contextRequest)
221231
Expect(err).To(BeNil())
222232
proxyContext, ok = core.GetAPIGatewayContextFromContext(httpReq.Context())
@@ -264,6 +274,16 @@ var _ = Describe("RequestAccessor tests", func() {
264274
Expect("value1").To(Equal(stageVars["var1"]))
265275
Expect("value2").To(Equal(stageVars["var2"]))
266276

277+
// overwrite existing stagevars header
278+
varsRequestWithHeaders := getProxyRequest("orders", "GET")
279+
varsRequestWithHeaders.StageVariables = getStageVariables()
280+
varsRequestWithHeaders.Headers = map[string]string{core.APIGwStageVarsHeader: `{"var1":"abc123"}`}
281+
httpReq, err = accessor.ProxyEventToHTTPRequest(varsRequestWithHeaders)
282+
Expect(err).To(BeNil())
283+
stageVars, err = accessor.GetAPIGatewayStageVars(httpReq)
284+
Expect(err).To(BeNil())
285+
Expect(stageVars["var1"]).To(Equal("value1"))
286+
267287
stageVars, ok := core.GetStageVarsFromContext(httpReq.Context())
268288
// not present in context
269289
Expect(ok).To(BeFalse())

0 commit comments

Comments
 (0)