Пример #1
0
        //--- Methods ---

        /// <inheritdoc/>
        protected override async Task InitializeEpilogueAsync()
        {
            await base.InitializeEpilogueAsync();

            // NOTE (2019-04-15, bjorg): we initialize the invocation target directory after the function environment
            //  initialization so that 'CreateInvocationTargetInstance()' can access the environment variables if need be.

            // read optional api-gateway-mappings file
            _directory = new ApiGatewayInvocationTargetDirectory(CreateInvocationTargetInstance);
            if (File.Exists("api-mappings.json"))
            {
                var mappings = DeserializeJson <ApiGatewayInvocationMappings>(File.ReadAllText("api-mappings.json"));
                foreach (var mapping in mappings.Mappings)
                {
                    if (mapping.RestApi != null)
                    {
                        LogInfo($"Mapping REST API '{mapping.RestApi}' to {mapping.Method}");
                        _directory.Add(mapping.RestApi, mapping.Method);
                    }
                    else if (mapping.WebSocket != null)
                    {
                        LogInfo($"Mapping WebSocket route '{mapping.WebSocket}' to {mapping.Method}");
                        _directory.Add(mapping.WebSocket, mapping.Method);
                    }
                    else
                    {
                        throw new InvalidDataException("missing 'RestApi/WebSocket' property in 'api-mappings.json' file");
                    }
                }
            }
        }
Пример #2
0
        private void Test(string methodName, APIGatewayProxyRequest request, APIGatewayProxyResponse expectedResponse)
        {
            // Arrange
            var invocationTargetDirectory = new ApiGatewayInvocationTargetDirectory(type => (type == GetType()) ? this : Activator.CreateInstance(type, new[] { this }), new LambdaJsonSerializer());

            invocationTargetDirectory.Add("test", $"{GetType().Assembly.FullName}::{GetType().FullName}::{methodName}");

            // Act
            var found = invocationTargetDirectory.TryGetInvocationTarget("test", out var invocationTarget, out var isAsync);

            // Assert
            found.Should().Be(true);

            // Act
            var response = invocationTarget(request).GetAwaiter().GetResult();

            // Assert
            response.Should().BeEquivalentTo(expectedResponse);
        }