Skip to content

Commit 07bc08b

Browse files
authored
fix: Code scanning alerts (#228)
* fix: Code scanning alerts
1 parent c164b62 commit 07bc08b

3 files changed

Lines changed: 38 additions & 9 deletions

File tree

src/AzureEventGridSimulator.Tests/IntegrationTests/BasicTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async Task GivenAValidEvent_WhenPublished_ThenItShouldBeAccepted()
4242
);
4343

4444
// Act
45-
var jsonContent = new StringContent(json, Encoding.UTF8, "application/json");
45+
using var jsonContent = new StringContent(json, Encoding.UTF8, "application/json");
4646
var response = await client.PostAsync("/api/events", jsonContent);
4747

4848
// Assert

src/AzureEventGridSimulator.Tests/UnitTests/Retry/HttpEventDeliveryServiceTests.cs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
namespace AzureEventGridSimulator.Tests.UnitTests.Retry;
1414

1515
[Trait("Category", "unit")]
16-
public class HttpEventDeliveryServiceTests
16+
public class HttpEventDeliveryServiceTests : IDisposable
1717
{
1818
private readonly EventSchemaFormatterFactory _formatterFactory;
19+
private readonly List<HttpClient> _httpClients = [];
1920
private readonly ILogger<HttpEventDeliveryService> _logger;
2021

2122
public HttpEventDeliveryServiceTests()
@@ -27,6 +28,16 @@ public HttpEventDeliveryServiceTests()
2728
);
2829
}
2930

31+
public void Dispose()
32+
{
33+
foreach (var client in _httpClients)
34+
{
35+
client.Dispose();
36+
}
37+
38+
_httpClients.Clear();
39+
}
40+
3041
[Theory]
3142
[InlineData(HttpStatusCode.OK)]
3243
[InlineData(HttpStatusCode.Created)]
@@ -194,7 +205,7 @@ public async Task GivenDnsError_WhenDelivering_ThenReturnsNetworkError()
194205
result.ErrorMessage.ShouldContain("No such host");
195206
}
196207

197-
private static IHttpClientFactory CreateMockHttpClientFactory(
208+
private IHttpClientFactory CreateMockHttpClientFactory(
198209
HttpStatusCode statusCode = HttpStatusCode.OK,
199210
Exception throwException = null,
200211
Action responseAction = null,
@@ -208,6 +219,7 @@ private static IHttpClientFactory CreateMockHttpClientFactory(
208219
captureHeaders
209220
);
210221
var httpClient = new HttpClient(handler);
222+
_httpClients.Add(httpClient);
211223

212224
var factory = Substitute.For<IHttpClientFactory>();
213225
factory.CreateClient(Arg.Any<string>()).Returns(httpClient);
@@ -264,6 +276,7 @@ private class MockHttpMessageHandler : HttpMessageHandler
264276
private readonly Action<HttpRequestHeaders> _captureHeaders;
265277
private readonly Exception _exception;
266278
private readonly Action _responseAction;
279+
private readonly List<HttpResponseMessage> _responses = [];
267280
private readonly HttpStatusCode _statusCode;
268281

269282
public MockHttpMessageHandler(
@@ -292,13 +305,29 @@ CancellationToken cancellationToken
292305
throw _exception;
293306
}
294307

295-
return Task.FromResult(
296-
new HttpResponseMessage(_statusCode)
308+
var response = new HttpResponseMessage(_statusCode)
309+
{
310+
Content = new StringContent(""),
311+
ReasonPhrase = _statusCode.ToString(),
312+
};
313+
_responses.Add(response);
314+
315+
return Task.FromResult(response);
316+
}
317+
318+
protected override void Dispose(bool disposing)
319+
{
320+
if (disposing)
321+
{
322+
foreach (var response in _responses)
297323
{
298-
Content = new StringContent(""),
299-
ReasonPhrase = _statusCode.ToString(),
324+
response.Dispose();
300325
}
301-
);
326+
327+
_responses.Clear();
328+
}
329+
330+
base.Dispose(disposing);
302331
}
303332
}
304333
}

src/AzureEventGridSimulator/Infrastructure/Extensions/KestrelServerOptionsExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ this KestrelServerOptions options
3030
certificate = new X509Certificate2(certificateFile, certificatePassword);
3131
#endif
3232
}
33-
else if (certificateFileSpecified && !certificatePasswordSpecified)
33+
else if (certificateFileSpecified)
3434
{
3535
// The certificate file was specified but the password wasn't.
3636
throw new InvalidOperationException("A certificate with a password is required.");

0 commit comments

Comments
 (0)