Skip to content

Commit 59e85a5

Browse files
authored
Merge pull request #2 from WinterYukky/fix/string-is-output-as-is
fix: string is output as is
2 parents 3a42348 + d6f8a64 commit 59e85a5

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

custom_runtime_kit.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ func (a *AWSLambdaCustomRuntime) newContext(env *AWSLambdaRuntimeEnvironemnt, he
156156
}
157157

158158
func (a *AWSLambdaCustomRuntime) handleResponse(event *Context, value interface{}) error {
159-
body, _ := json.Marshal(value)
159+
var body []byte
160+
if v, ok := value.(string); ok {
161+
body = []byte(v)
162+
} else {
163+
body, _ = json.Marshal(value)
164+
}
160165
url := fmt.Sprintf("http://%v/2018-06-01/runtime/invocation/%v/response", a.awsLambdaRuntimeAPI, event.RequestID)
161166
req, _ := http.NewRequest("POST", url, bytes.NewReader(body))
162167
_, err := a.httpClient.Do(req)

custom_runtime_kit_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,33 @@ func TestAWSLambdaCustomRuntime_Invoke(t *testing.T) {
256256
want: `{"name":"WinterYukky"}`,
257257
wantErr: false,
258258
},
259+
{
260+
name: "String result is output as is",
261+
fields: fields{
262+
awsLambdaRuntimeAPI: "unit-test-runtime-api",
263+
httpClient: NewMockHTTPClient(MockHTTPClientProps{
264+
GetEvent: func(req *http.Request) (*http.Response, error) {
265+
if req.URL.String() != "http://unit-test-runtime-api/2018-06-01/runtime/invocation/next" {
266+
return nil, fmt.Errorf("get event URL is should http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/next, got %v", req.URL.String())
267+
}
268+
return &http.Response{
269+
Body: NewBody(`{"key1":"value1"}`),
270+
}, nil
271+
},
272+
Response: func(req *http.Request) (*http.Response, error) {
273+
return &http.Response{}, nil
274+
},
275+
}),
276+
runtime: &InstantRuntime{
277+
setup: func(env *AWSLambdaRuntimeEnvironemnt) error { return nil },
278+
invoke: func(event []byte, context *Context) (interface{}, error) {
279+
return `{"name":"WinterYukky"}`, nil
280+
},
281+
},
282+
},
283+
want: `{"name":"WinterYukky"}`,
284+
wantErr: false,
285+
},
259286
}
260287
for _, tt := range tests {
261288
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)