github.com/mweagle/Sparta@v1.15.0/apigateway_test.go (about)

     1  package sparta
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  	"time"
     9  
    10  	spartaAPIGateway "github.com/mweagle/Sparta/aws/apigateway"
    11  	spartaAWSEvents "github.com/mweagle/Sparta/aws/events"
    12  	"github.com/sirupsen/logrus"
    13  )
    14  
    15  var randVal string
    16  
    17  func init() {
    18  	randVal = time.Now().UTC().String()
    19  }
    20  
    21  type testRequest struct {
    22  	Message string
    23  	Request spartaAWSEvents.APIGatewayRequest
    24  }
    25  
    26  func testAPIGatewayLambda(ctx context.Context,
    27  	gatewayEvent spartaAWSEvents.APIGatewayRequest) (interface{}, error) {
    28  	logger, loggerOk := ctx.Value(ContextKeyLogger).(*logrus.Logger)
    29  	if loggerOk {
    30  		logger.Info("Hello world structured log message")
    31  	}
    32  
    33  	// Return a message, together with the incoming input...
    34  	return spartaAPIGateway.NewResponse(http.StatusOK, &testRequest{
    35  		Message: fmt.Sprintf("Test %s", randVal),
    36  		Request: gatewayEvent,
    37  	}), nil
    38  }
    39  
    40  func TestAPIGatewayRequest(t *testing.T) {
    41  	requestBody := &testRequest{
    42  		Message: randVal,
    43  	}
    44  	mockRequest, mockRequestErr := spartaAWSEvents.NewAPIGatewayMockRequest("helloWorld",
    45  		http.MethodGet,
    46  		nil,
    47  		requestBody)
    48  	if mockRequestErr != nil {
    49  		t.Fatal(mockRequestErr)
    50  	}
    51  	resp, respErr := testAPIGatewayLambda(context.Background(), *mockRequest)
    52  	if respErr != nil {
    53  		t.Fatal(respErr)
    54  	} else {
    55  		t.Log(fmt.Sprintf("%#v", resp))
    56  	}
    57  }
    58  
    59  func TestAPIGateway(t *testing.T) {
    60  	stage := NewStage("v1")
    61  	apiGateway := NewAPIGateway("SpartaAPIGateway", stage)
    62  	lambdaFn, _ := NewAWSLambda(LambdaName(mockLambda1),
    63  		mockLambda1,
    64  		IAMRoleDefinition{})
    65  
    66  	// Register the function with the API Gateway
    67  	apiGatewayResource, _ := apiGateway.NewResource("/test", lambdaFn)
    68  	apiGatewayResource.NewMethod("GET", http.StatusOK)
    69  
    70  	testProvisionEx(t,
    71  		[]*LambdaAWSInfo{lambdaFn},
    72  		apiGateway,
    73  		nil,
    74  		nil,
    75  		false,
    76  		nil)
    77  }
    78  
    79  func TestAPIV2Gateway(t *testing.T) {
    80  	stage, _ := NewAPIV2Stage("v1")
    81  	apiGateway, _ := NewAPIV2(Websocket,
    82  		"sample",
    83  		"$request.body.message",
    84  		stage)
    85  	lambdaFn, _ := NewAWSLambda(LambdaName(mockLambda1),
    86  		mockLambda1,
    87  		IAMRoleDefinition{})
    88  	apiv2Route, _ := apiGateway.NewAPIV2Route("$connect",
    89  		lambdaFn)
    90  	apiv2Route.OperationName = "ConnectRoute"
    91  
    92  	lambdaFn2, _ := NewAWSLambda(LambdaName(mockLambda2),
    93  		mockLambda2,
    94  		IAMRoleDefinition{})
    95  	apiv2Route2, _ := apiGateway.NewAPIV2Route("$disconnect",
    96  		lambdaFn2)
    97  	apiv2Route2.OperationName = "DisconnectRoute"
    98  
    99  	testProvisionEx(t,
   100  		[]*LambdaAWSInfo{lambdaFn, lambdaFn2},
   101  		apiGateway,
   102  		nil,
   103  		nil,
   104  		false,
   105  		nil)
   106  }