Skip to content

Commit f2d404d

Browse files
Reduce number of api gateways used in integration tests/refactor integration tests (#2003)
1 parent 2365bd9 commit f2d404d

13 files changed

+541
-604
lines changed

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/ApiGatewayIntegrationTestFixture.cs

Lines changed: 179 additions & 105 deletions
Large diffs are not rendered by default.

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/ApiGatewayResponseExtensionsAdditionalTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public async Task ToHttpResponse_RestAPIGatewayV1DecodesBase64()
3636
var httpContext = new DefaultHttpContext();
3737
httpContext.Response.Body = new MemoryStream();
3838
await testResponse.ToHttpResponseAsync(httpContext, ApiGatewayEmulatorMode.Rest);
39-
var actualResponse = await _httpClient.PostAsync(_fixture.BinaryMediaTypeRestApiUrl, new StringContent(JsonSerializer.Serialize(testResponse)));
39+
40+
var baseUrl = _fixture.GetAppropriateBaseUrl(ApiGatewayType.RestWithBinarySupport);
41+
var url = _fixture.GetRouteUrl(baseUrl, TestRoutes.Ids.DecodeParseBinary);
42+
var actualResponse = await _httpClient.PostAsync(url, new StringContent(JsonSerializer.Serialize(testResponse)));
4043
await _fixture.ApiGatewayTestHelper.AssertResponsesEqual(actualResponse, httpContext.Response);
4144
Assert.Equal(200, (int)actualResponse.StatusCode);
4245
var content = await actualResponse.Content.ReadAsStringAsync();
@@ -56,7 +59,10 @@ public async Task ToHttpResponse_HttpV1APIGatewayV1DecodesBase64()
5659
var httpContext = new DefaultHttpContext();
5760
httpContext.Response.Body = new MemoryStream();
5861
await testResponse.ToHttpResponseAsync(httpContext, ApiGatewayEmulatorMode.HttpV1);
59-
var actualResponse = await _httpClient.PostAsync(_fixture.ParseAndReturnBodyHttpApiV1Url, new StringContent(JsonSerializer.Serialize(testResponse)));
62+
63+
var baseUrl = _fixture.GetAppropriateBaseUrl(ApiGatewayType.HttpV1);
64+
var url = _fixture.GetRouteUrl(baseUrl, TestRoutes.Ids.ParseAndReturnBody);
65+
var actualResponse = await _httpClient.PostAsync(url, new StringContent(JsonSerializer.Serialize(testResponse)));
6066

6167
await _fixture.ApiGatewayTestHelper.AssertResponsesEqual(actualResponse, httpContext.Response);
6268
Assert.Equal(200, (int)actualResponse.StatusCode);

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/ApiGatewayResponseExtensionsTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ public async Task IntegrationTest_APIGatewayV1_REST(string testName, ApiGatewayR
2727
{
2828
await RetryHelper.RetryOperation(async () =>
2929
{
30-
await RunV1Test(testCase, _fixture.ParseAndReturnBodyRestApiUrl, ApiGatewayEmulatorMode.Rest);
30+
var baseUrl = _fixture.GetAppropriateBaseUrl(ApiGatewayType.Rest);
31+
var url = _fixture.GetRouteUrl(baseUrl, TestRoutes.Ids.ParseAndReturnBody);
32+
await RunV1Test(testCase, url, ApiGatewayEmulatorMode.Rest);
3133
return true;
3234
});
3335
}
@@ -39,7 +41,9 @@ public async Task IntegrationTest_APIGatewayV1_HTTP(string testName, ApiGatewayR
3941
{
4042
await RetryHelper.RetryOperation(async () =>
4143
{
42-
await RunV1Test(testCase, _fixture.ParseAndReturnBodyHttpApiV1Url, ApiGatewayEmulatorMode.HttpV1);
44+
var baseUrl = _fixture.GetAppropriateBaseUrl(ApiGatewayType.HttpV1);
45+
var url = _fixture.GetRouteUrl(baseUrl, TestRoutes.Ids.ParseAndReturnBody);
46+
await RunV1Test(testCase, url, ApiGatewayEmulatorMode.HttpV1);
4347
return true;
4448
});
4549
}
@@ -51,9 +55,11 @@ public async Task IntegrationTest_APIGatewayV2(string testName, ApiGatewayRespon
5155
{
5256
await RetryHelper.RetryOperation(async () =>
5357
{
58+
var baseUrl = _fixture.GetAppropriateBaseUrl(ApiGatewayType.HttpV2);
59+
var url = _fixture.GetRouteUrl(baseUrl, TestRoutes.Ids.ParseAndReturnBody);
5460
var testResponse = testCase.Response as APIGatewayHttpApiV2ProxyResponse;
5561
Assert.NotNull(testResponse);
56-
var (actualResponse, httpTestResponse) = await _fixture.ApiGatewayTestHelper.ExecuteTestRequest(testResponse, _fixture.ParseAndReturnBodyHttpApiV2Url);
62+
var (actualResponse, httpTestResponse) = await _fixture.ApiGatewayTestHelper.ExecuteTestRequest(testResponse, url);
5763
await _fixture.ApiGatewayTestHelper.AssertResponsesEqual(actualResponse, httpTestResponse);
5864
await testCase.IntegrationAssertions(actualResponse, ApiGatewayEmulatorMode.HttpV2);
5965
return true;

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/BaseApiGatewayTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ protected async Task<HttpResponseMessage> TestEndpoint(string routeName, int api
140140
}
141141
}
142142

143-
protected async Task<(int lambdaPort, int apiGatewayPort)> GetFreePorts()
143+
protected (int lambdaPort, int apiGatewayPort) GetFreePorts()
144144
{
145145
var lambdaPort = GetFreePort();
146146
int apiGatewayPort;

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/BasicApiGatewayTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public BasicApiGatewayTests(ITestOutputHelper testOutputHelper) : base(testOutpu
2323
[RetryFact]
2424
public async Task TestLambdaToUpperV2()
2525
{
26-
var ports = await GetFreePorts();
26+
var ports = GetFreePorts();
2727
var lambdaPort = ports.lambdaPort;
2828
var apiGatewayPort = ports.apiGatewayPort;
2929

@@ -68,7 +68,7 @@ public async Task TestLambdaToUpperV2()
6868
[RetryFact]
6969
public async Task TestLambdaToUpperRest()
7070
{
71-
var ports = await GetFreePorts();
71+
var ports = GetFreePorts();
7272
var lambdaPort = ports.lambdaPort;
7373
var apiGatewayPort = ports.apiGatewayPort;
7474

@@ -113,7 +113,7 @@ public async Task TestLambdaToUpperRest()
113113
[RetryFact]
114114
public async Task TestLambdaToUpperV1()
115115
{
116-
var ports = await GetFreePorts();
116+
var ports = GetFreePorts();
117117
var lambdaPort = ports.lambdaPort;
118118
var apiGatewayPort = ports.apiGatewayPort;
119119

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/BinaryResponseTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public BinaryResponseTests(ITestOutputHelper testOutputHelper) : base(testOutput
2323
[RetryFact]
2424
public async Task TestLambdaBinaryResponse()
2525
{
26-
var ports = await GetFreePorts();
26+
var ports = GetFreePorts();
2727
var lambdaPort = ports.lambdaPort;
2828
var apiGatewayPort = ports.apiGatewayPort;
2929

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/EdgeCaseTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public EdgeCaseTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper
2323
[RetryFact]
2424
public async Task TestLambdaReturnString()
2525
{
26-
var ports = await GetFreePorts();
26+
var ports = GetFreePorts();
2727
var lambdaPort = ports.lambdaPort;
2828
var apiGatewayPort = ports.apiGatewayPort;
2929

@@ -64,7 +64,7 @@ public async Task TestLambdaReturnString()
6464
[RetryFact]
6565
public async Task TestLambdaWithNullEndpoint()
6666
{
67-
var ports = await GetFreePorts();
67+
var ports = GetFreePorts();
6868
var lambdaPort = ports.lambdaPort;
6969
var apiGatewayPort = ports.apiGatewayPort;
7070

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/Helpers/ApiGatewayHelper.cs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Amazon.ApiGatewayV2.Model;
88
using System.Net;
99
using Amazon.Runtime.Internal.Endpoints.StandardLibrary;
10+
using ConflictException = Amazon.ApiGatewayV2.Model.ConflictException;
1011

1112
namespace Amazon.Lambda.TestTool.IntegrationTests.Helpers
1213
{
@@ -130,7 +131,7 @@ public async Task<string> AddRouteToRestApi(string restApiId, string lambdaArn,
130131
}
131132
}
132133

133-
// Create the method for the final resource
134+
// Create the method and integration
134135
await _apiGatewayV1Client.PutMethodAsync(new PutMethodRequest
135136
{
136137
RestApiId = restApiId,
@@ -139,36 +140,35 @@ await _apiGatewayV1Client.PutMethodAsync(new PutMethodRequest
139140
AuthorizationType = "NONE"
140141
});
141142

142-
// Create the integration for the method
143143
await _apiGatewayV1Client.PutIntegrationAsync(new PutIntegrationRequest
144144
{
145145
RestApiId = restApiId,
146146
ResourceId = parentResourceId,
147147
HttpMethod = httpMethod,
148-
Type = APIGateway.IntegrationType.AWS_PROXY,
148+
Type = Amazon.APIGateway.IntegrationType.AWS_PROXY,
149149
IntegrationHttpMethod = "POST",
150150
Uri = $"arn:aws:apigateway:{_apiGatewayV1Client.Config.RegionEndpoint.SystemName}:lambda:path/2015-03-31/functions/{lambdaArn}/invocations"
151151
});
152152

153-
// Deploy the API
154-
var deploymentResponse = await _apiGatewayV1Client.CreateDeploymentAsync(new APIGateway.Model.CreateDeploymentRequest
153+
// Create and wait for deployment
154+
var deploymentResponse = await _apiGatewayV1Client.CreateDeploymentAsync(new Amazon.APIGateway.Model.CreateDeploymentRequest
155155
{
156156
RestApiId = restApiId,
157157
StageName = "test"
158158
});
159159

160-
var url = $"https://{restApiId}.execute-api.{_apiGatewayV1Client.Config.RegionEndpoint.SystemName}.amazonaws.com/test{route}";
161-
return url;
160+
return $"https://{restApiId}.execute-api.{_apiGatewayV1Client.Config.RegionEndpoint.SystemName}.amazonaws.com/test{route}";
162161
}
163162

164-
public async Task<string> AddRouteToHttpApi(string httpApiId, string lambdaArn, string version, string route = "/test", string routeKey = "ANY")
163+
public async Task<string> AddRouteToHttpApi(string httpApiId, string lambdaArn, string version, string route = "/test", string httpMethod = "ANY")
165164
{
166165
var createIntegrationResponse = await _apiGatewayV2Client.CreateIntegrationAsync(new CreateIntegrationRequest
167166
{
168167
ApiId = httpApiId,
169-
IntegrationType = ApiGatewayV2.IntegrationType.AWS_PROXY,
168+
IntegrationType = Amazon.ApiGatewayV2.IntegrationType.AWS_PROXY,
170169
IntegrationUri = lambdaArn,
171-
PayloadFormatVersion = version
170+
PayloadFormatVersion = version,
171+
IntegrationMethod = "POST"
172172
});
173173
string integrationId = createIntegrationResponse.IntegrationId;
174174

@@ -181,7 +181,7 @@ public async Task<string> AddRouteToHttpApi(string httpApiId, string lambdaArn,
181181
await _apiGatewayV2Client.CreateRouteAsync(new CreateRouteRequest
182182
{
183183
ApiId = httpApiId,
184-
RouteKey = $"{routeKey} {currentPath}",
184+
RouteKey = $"{httpMethod} {currentPath}",
185185
Target = $"integrations/{integrationId}"
186186
});
187187
}
@@ -192,11 +192,32 @@ await _apiGatewayV2Client.CreateRouteAsync(new CreateRouteRequest
192192
await _apiGatewayV2Client.CreateRouteAsync(new CreateRouteRequest
193193
{
194194
ApiId = httpApiId,
195-
RouteKey = $"{routeKey} {route}",
195+
RouteKey = $"{httpMethod} {route}",
196196
Target = $"integrations/{integrationId}"
197197
});
198198
}
199199

200+
// Create and wait for deployment
201+
var deployment = await _apiGatewayV2Client.CreateDeploymentAsync(new Amazon.ApiGatewayV2.Model.CreateDeploymentRequest
202+
{
203+
ApiId = httpApiId
204+
});
205+
206+
// Create stage if it doesn't exist
207+
try
208+
{
209+
await _apiGatewayV2Client.CreateStageAsync(new Amazon.ApiGatewayV2.Model.CreateStageRequest
210+
{
211+
ApiId = httpApiId,
212+
StageName = "$default",
213+
AutoDeploy = true
214+
});
215+
}
216+
catch (Amazon.ApiGatewayV2.Model.ConflictException)
217+
{
218+
// Stage already exists, continue
219+
}
220+
200221
var url = $"https://{httpApiId}.execute-api.{_apiGatewayV2Client.Config.RegionEndpoint.SystemName}.amazonaws.com{route}";
201222
return url ;
202223
}

0 commit comments

Comments
 (0)