func TestInvocationErrorHandlerSendsErrorCauseToXRayForContentTypeErrorCause(t *testing.T) {
	flowTest := testdata.NewFlowTest()
	flowTest.ConfigureForInit()
	flowTest.Runtime.Ready()
	handler := NewInvocationErrorHandler(flowTest.RegistrationService)
	responseRecorder := httptest.NewRecorder()
	appCtx := flowTest.AppCtx
	errorCause := json.RawMessage(`{"paths":[],"working_directory":"/foo/bar/baz","exceptions":[]}`)
	errorWithCause := errorWithCauseRequest{
		ErrorMessage: "foo",
		ErrorType:    "bar",
		ErrorCause:   errorCause,
	}
	requestBody, err := json.Marshal(errorWithCause)
	assert.NoError(t, err, "error while creating test request")
	invoke := &interop.Invoke{TraceID: "Root=TraceID;Parent=ParentID;Sampled=1", ID: "InvokeID"}
	request := httptest.NewRequest("POST", "/", bytes.NewReader(requestBody))
	request = addInvocationID(request, invoke.ID)
	request.Header.Set("Content-Type", errorWithCauseContentType)
	flowTest.ConfigureForInvoke(context.Background(), invoke)
	handler.ServeHTTP(responseRecorder, appctx.RequestWithAppCtx(request, appCtx))
	errorResponse := flowTest.InteropServer.ErrorResponse
	assert.NotNil(t, errorResponse)
	assert.Nil(t, flowTest.InteropServer.Response)
	assert.JSONEq(t, string(errorCause), string(errorResponse.ErrorCause))
}
////////////////////////////////////////////
/// Tests for error.cause Content-Type /////
////////////////////////////////////////////
package handler
import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
	"go.amzn.com/lambda/appctx"
	"go.amzn.com/lambda/interop"
	"go.amzn.com/lambda/rapi/model"
	"go.amzn.com/lambda/testdata"
	"github.com/go-chi/chi"
	"github.com/stretchr/testify/assert"
)
func TestInvocationErrorHandler(t *testing.T) {
	t.Run("GA", func(t *testing.T) { runTestInvocationErrorHandler(t) })
}
func addInvocationID(r *http.Request, invokeID string) *http.Request {
	rctx := chi.NewRouteContext()
	rctx.URLParams.Add("awsrequestid", invokeID)
	return r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
}
func runTestInvocationErrorHandler(t *testing.T) {
	flowTest := testdata.NewFlowTest()
	flowTest.ConfigureForInit()
	flowTest.Runtime.Ready()
	handler := NewInvocationErrorHandler(flowTest.RegistrationService)
	responseRecorder := httptest.NewRecorder()
	appCtx := flowTest.AppCtx
	invoke := &interop.Invoke{
		ID:                    "InvocationID1",
		InvokedFunctionArn:    "arn::dummy1",
		CognitoIdentityID:     "CognitoidentityID1",
		CognitoIdentityPoolID: "CognitoidentityPollID1",
		DeadlineNs:            "deadlinens1",
		ClientContext:         "clientcontext1",
		ContentType:           "image/png",
		Payload:               strings.NewReader("Payload1"),
	}
	flowTest.ConfigureForInvoke(context.Background(), invoke)
	errorBody := []byte("My byte array is yours")
	errorType := "ErrorType"
	errorContentType := "application/MyBinaryType"
	request := appctx.RequestWithAppCtx(httptest.NewRequest("POST", "/", bytes.NewReader(errorBody)), appCtx)
	request = addInvocationID(request, invoke.ID)
	request.Header.Set("Content-Type", errorContentType)
	request.Header.Set("Lambda-runtime-functioN-erroR-typE", errorType)  
	handler.ServeHTTP(responseRecorder, request)
	assert.Equal(t, http.StatusAccepted, responseRecorder.Code, "Handler returned wrong status code: got %v expected %v",
		responseRecorder.Code, http.StatusAccepted)
	assert.JSONEq(t, fmt.Sprintf("{\"status\":\"%s\"}\n", "OK"), responseRecorder.Body.String())
	assert.Equal(t, "application/json; charset=utf-8", responseRecorder.Header().Get("Content-Type"))
	errorResponse := flowTest.InteropServer.ErrorResponse
	assert.NotNil(t, errorResponse)
	assert.Nil(t, flowTest.InteropServer.Response)
	assert.Equal(t, "", errorResponse.ErrorMessage)
	assert.Equal(t, errorType, errorResponse.ErrorType)
	assert.Equal(t, errorBody, errorResponse.Payload)
}
func TestInvocationErrorHandlerRemovesErrorCauseFromResponse(t *testing.T) {
	flowTest := testdata.NewFlowTest()
	flowTest.ConfigureForInit()
	flowTest.Runtime.Ready()
	handler := NewInvocationErrorHandler(flowTest.RegistrationService)
	responseRecorder := httptest.NewRecorder()
	appCtx := flowTest.AppCtx
	invoke := &interop.Invoke{ID: "InvocationID1"}
	flowTest.ConfigureForInvoke(context.Background(), invoke)
	errMsg, errType := "foo", "foo"
	errorCause := json.RawMessage(`{"paths":[],"working_directory":[],"exceptions":[]}`)
	errorWithCause := errorWithCauseRequest{
		ErrorMessage: errMsg,
		ErrorType:    errType,
		ErrorCause:   errorCause,
	}
	requestBody, err := json.Marshal(errorWithCause)
	assert.NoError(t, err, "error while creating test request")
	errorContentType := errorWithCauseContentType
	request := appctx.RequestWithAppCtx(httptest.NewRequest("POST", "/", bytes.NewReader(requestBody)), appCtx)
	request = addInvocationID(request, invoke.ID)
	request.Header.Set("Content-Type", errorContentType)
	handler.ServeHTTP(responseRecorder, request)
	expectedResponsePayload := []byte(fmt.Sprintf(`{"errorMessage":"%s","errorType":"%s"}`, errMsg, errType))
	errorResponse := flowTest.InteropServer.ErrorResponse
	assert.NotNil(t, errorResponse)
	assert.Nil(t, flowTest.InteropServer.Response)
	assert.JSONEq(t, string(expectedResponsePayload), string(errorResponse.Payload))
}