Repository navigation
Expand file tree
/
Copy pathiniterror_test.go
More file actions
72 lines (57 loc) · 2.48 KB
/
Copy pathiniterror_test.go
File metadata and controls
72 lines (57 loc) · 2.48 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
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package handler
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
"go.amzn.com/lambda/appctx"
"go.amzn.com/lambda/fatalerror"
"go.amzn.com/lambda/testdata"
)
// TestInitErrorHandler tests that API handler for
// initialization-time errors receives and passes
// information through to the Slicer unmodified.
func TestInitErrorHandler(t *testing.T) {
t.Run("GA", func(t *testing.T) { runTestInitErrorHandler(t) })
}
func runTestInitErrorHandler(t *testing.T) {
flowTest := testdata.NewFlowTest()
flowTest.ConfigureForInit()
handler := NewInitErrorHandler(flowTest.RegistrationService)
responseRecorder := httptest.NewRecorder()
appCtx := flowTest.AppCtx
// Error request, as submitted by custom runtime.
errorBody := []byte("My byte array is yours")
errorType := "ErrorType"
errorContentType := "application/MyBinaryType"
request := appctx.RequestWithAppCtx(httptest.NewRequest("POST", "/", bytes.NewReader(errorBody)), appCtx)
request.Header.Set("Content-Type", errorContentType)
request.Header.Set("Lambda-runtime-functioN-erroR-typE", errorType) // Headers are case-insensitive anyway !
// Submit !
handler.ServeHTTP(responseRecorder, request)
// Assertions
// Validate response sent to the runtime.
require.Equal(t, http.StatusAccepted, responseRecorder.Code, "Handler returned wrong status code: got %v expected %v",
responseRecorder.Code, http.StatusAccepted)
require.JSONEq(t, fmt.Sprintf("{\"status\":\"%s\"}\n", "OK"), responseRecorder.Body.String())
require.Equal(t, "application/json", responseRecorder.Header().Get("Content-Type"))
// Validate init error persisted in the application context.
errorResponse := flowTest.InteropServer.ErrorResponse
require.NotNil(t, errorResponse)
require.Nil(t, flowTest.InteropServer.Response)
// Slicer falls back to using ErrorMessage when error
// payload is not provided. This fallback is not part
// of the RAPID API spec and is not available to
// customers.
require.Equal(t, "", errorResponse.FunctionError.Message)
// Slicer falls back to using ErrorType when error
// payload is not provided. Customers can set error
// type via header to use this fallback.
require.Equal(t, fatalerror.RuntimeUnknown, errorResponse.FunctionError.Type)
// Payload is arbitrary data that customers submit - it's error response body.
require.Equal(t, errorBody, errorResponse.Payload)
}