-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathresponse.go
More file actions
60 lines (51 loc) · 1.79 KB
/
Copy pathresponse.go
File metadata and controls
60 lines (51 loc) · 1.79 KB
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
package algnhsa
import (
"encoding/base64"
"net/http"
"net/http/httptest"
)
const (
acceptAllContentType = "*/*"
acceptAllContentEncoding = "*"
)
var canonicalSetCookieHeaderKey = http.CanonicalHeaderKey("Set-Cookie")
// lambdaResponse is a combined lambda response.
// It contains common fields from APIGatewayProxyResponse, APIGatewayV2HTTPResponse and ALBTargetGroupResponse.
type lambdaResponse struct {
StatusCode int `json:"statusCode"`
Headers map[string]string `json:"headers,omitempty"`
MultiValueHeaders map[string][]string `json:"multiValueHeaders,omitempty"`
Cookies []string `json:"cookies,omitempty"`
Body string `json:"body"`
IsBase64Encoded bool `json:"isBase64Encoded,omitempty"`
}
func newLambdaResponse(w *httptest.ResponseRecorder, opts *Options, requestType RequestType) (lambdaResponse, error) {
result := w.Result()
var resp lambdaResponse
var err error
switch requestType {
case RequestTypeAPIGatewayV1:
resp, err = newAPIGatewayV1Response(result)
case RequestTypeALB:
resp, err = newALBResponse(result)
case RequestTypeAPIGatewayV2:
resp, err = newAPIGatewayV2Response(result)
}
if err != nil {
return resp, err
}
resp.StatusCode = result.StatusCode
// Set body.
contentType := result.Header.Get("Content-Type")
contentEncoding := result.Header.Get("Content-Encoding")
if opts.binaryContentTypes.contains(acceptAllContentType) ||
opts.binaryContentTypes.contains(contentType) ||
opts.binaryContentEncodings.contains(acceptAllContentEncoding) ||
opts.binaryContentEncodings.contains(contentEncoding) {
resp.Body = base64.StdEncoding.EncodeToString(w.Body.Bytes())
resp.IsBase64Encoded = true
} else {
resp.Body = w.Body.String()
}
return resp, nil
}