using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Net.Http; using Newtonsoft.Json; using Xunit; using Amazon.Lambda.TestUtilities; using Amazon.Lambda.APIGatewayEvents; namespace ServerlessExample.Tests { public class FunctionTest { private static readonly HttpClient client = new HttpClient(); private static async Task GetCallingIP() { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Add("User-Agent", "AWS Lambda .Net Client"); var stringTask = client.GetStringAsync("http://checkip.amazonaws.com/").ConfigureAwait(continueOnCapturedContext:false); var msg = await stringTask; return msg.Replace("\n",""); } [Fact] public async Task TestHelloWorldFunctionHandler() { var request = new APIGatewayProxyRequest(); var context = new TestLambdaContext(); string location = GetCallingIP().Result; Dictionary body = new Dictionary { { "message", "hello world" }, { "location", location }, }; var expectedResponse = new APIGatewayProxyResponse { Body = JsonConvert.SerializeObject(body), StatusCode = 200, Headers = new Dictionary { { "Content-Type", "application/json" } } }; var function = new Function(); var response = await function.FunctionHandler(request, context); Console.WriteLine("Lambda Response: \n" + response.Body); Console.WriteLine("Expected Response: \n" + expectedResponse.Body); Assert.Equal(expectedResponse.Body, response.Body); Assert.Equal(expectedResponse.Headers, response.Headers); Assert.Equal(expectedResponse.StatusCode, response.StatusCode); } } }