Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;

using Amazon.Lambda.Core;
Expand All @@ -25,20 +23,19 @@ private static async Task<string> 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 client.GetStringAsync("http://checkip.amazonaws.com/").ConfigureAwait(continueOnCapturedContext:false);

var msg = await stringTask;
return msg.Replace("\n","");
}

public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
{

string location = GetCallingIP().Result;
Dictionary<string, string> body = new Dictionary<string, string>
var location = await GetCallingIP();
var body = new Dictionary<string, string>
{
{ "message", "hello world" },
{ "location", location },
{ "location", location }
};

return new APIGatewayProxyResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;

using Newtonsoft.Json;
using Xunit;
using Amazon.Lambda.Core;
using Amazon.Lambda.TestUtilities;
using Amazon.Lambda.APIGatewayEvents;

Expand All @@ -29,37 +26,33 @@ private static async Task<string> GetCallingIP()
}

[Fact]
public void TestHelloWorldFunctionHandler()
public async Task TestHelloWorldFunctionHandler()
{
TestLambdaContext context;
APIGatewayProxyRequest request;
APIGatewayProxyResponse response;

request = new APIGatewayProxyRequest();
context = new TestLambdaContext();
var request = new APIGatewayProxyRequest();
var context = new TestLambdaContext();
string location = GetCallingIP().Result;
Dictionary<string, string> body = new Dictionary<string, string>
{
{ "message", "hello world" },
{ "location", location },
};

var ExpectedResponse = new APIGatewayProxyResponse
var expectedResponse = new APIGatewayProxyResponse
{
Body = JsonConvert.SerializeObject(body),
StatusCode = 200,
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
};

var function = new Function();
response = function.FunctionHandler(request, context);
var response = await function.FunctionHandler(request, context);

Console.WriteLine("Lambda Response: \n" + response.Body);
Console.WriteLine("Expected Response: \n" + ExpectedResponse.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);
Assert.Equal(expectedResponse.Body, response.Body);
Assert.Equal(expectedResponse.Headers, response.Headers);
Assert.Equal(expectedResponse.StatusCode, response.StatusCode);
}
}
}