forked from akrylysov/algnhsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
37 lines (29 loc) · 1005 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
36
37
package algnhsa
import (
"encoding/base64"
"net/http/httptest"
)
const acceptAllContentType = "*/*"
type lambdaResponse struct {
StatusCode int `json:"statusCode"`
Headers map[string]string `json:"headers"`
MultiValueHeaders map[string][]string `json:"multiValueHeaders"`
Body string `json:"body"`
IsBase64Encoded bool `json:"isBase64Encoded,omitempty"`
}
func newLambdaResponse(w *httptest.ResponseRecorder, binaryContentTypes map[string]bool) (lambdaResponse, error) {
event := lambdaResponse{}
// Set status code.
event.StatusCode = w.Code
// Set headers.
event.MultiValueHeaders = w.Result().Header
// 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
}