forked from akrylysov/algnhsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
35 lines (27 loc) · 821 Bytes
/
response.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
package algnhsa
import (
"encoding/base64"
"net/http/httptest"
"github.com/aws/aws-lambda-go/events"
)
const acceptAllContentType = "*/*"
func newAPIGatewayResponse(w *httptest.ResponseRecorder, binaryContentTypes map[string]bool) (events.APIGatewayProxyResponse, error) {
event := events.APIGatewayProxyResponse{}
// Set status code.
event.StatusCode = w.Code
// Set headers.
respHeaders := map[string]string{}
for k, v := range w.HeaderMap {
respHeaders[k] = v[0]
}
event.Headers = respHeaders
// Set body.
contentType := w.Header().Get("Content-Type")
if binaryContentTypes[acceptAllContentType] || binaryContentTypes[contentType] {
event.Body = base64.StdEncoding.EncodeToString(w.Body.Bytes())
event.IsBase64Encoded = true
} else {
event.Body = w.Body.String()
}
return event, nil
}