diff --git a/src/AzureEventGridSimulator.Tests/ActualSimulatorTests/ActualSimulatorFixture.cs b/src/AzureEventGridSimulator.Tests/ActualSimulatorTests/ActualSimulatorFixture.cs index 08071d8..27cef87 100644 --- a/src/AzureEventGridSimulator.Tests/ActualSimulatorTests/ActualSimulatorFixture.cs +++ b/src/AzureEventGridSimulator.Tests/ActualSimulatorTests/ActualSimulatorFixture.cs @@ -1,85 +1,87 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Xunit; - -namespace AzureEventGridSimulator.Tests.ActualSimulatorTests; - -public class ActualSimulatorFixture : IDisposable, IAsyncLifetime -{ - private const string SimulatorFileName = "AzureEventGridSimulator"; - private bool _disposed; - private string _simulatorExePath; - - private Process _simulatorProcess; - - public Task InitializeAsync() - { - var simulatorDirectory = Directory.GetCurrentDirectory(); - _simulatorExePath = Path.Combine(simulatorDirectory, $"{SimulatorFileName}.exe"); - - KillExistingSimulators(); - - _simulatorProcess = Process.Start(new ProcessStartInfo(_simulatorExePath) - { - WorkingDirectory = simulatorDirectory, - UseShellExecute = false, - RedirectStandardOutput = true, - CreateNoWindow = true, - Environment = { new KeyValuePair("ASPNETCORE_ENVIRONMENT", "Test") } - }); - - return Task.CompletedTask; - } - - public Task DisposeAsync() - { - Dispose(); - return Task.CompletedTask; - } - - public void Dispose() - { - if (!_disposed) - { - if (_simulatorProcess?.HasExited == false) - { - _simulatorProcess?.Kill(true); - _simulatorProcess?.WaitForExit(); - } - - _disposed = true; - GC.SuppressFinalize(this); - } - } - - private void KillExistingSimulators() - { - try - { - // Kill any existing instances of the test simulator that may still be hanging around. - // Note: there shouldn't be any unless something went wrong and the test runner didn't exit cleanly. - var simulatorProcesses = Process.GetProcesses() - .Where(o => o.ProcessName == SimulatorFileName) - .Where(o => string.Equals(o.MainModule?.FileName, _simulatorExePath, StringComparison.OrdinalIgnoreCase)) - .ToArray(); - - foreach (var process in simulatorProcesses) - { - process.Kill(); - } - } - catch - { - // - } - } - - ~ActualSimulatorFixture() - { - Dispose(); - } -} +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using Xunit; + +namespace AzureEventGridSimulator.Tests.ActualSimulatorTests; + +public class ActualSimulatorFixture : IDisposable, IAsyncLifetime +{ + private const string SimulatorFileName = "AzureEventGridSimulator"; + private bool _disposed; + private string _simulatorExePath; + + private Process _simulatorProcess; + + public async Task InitializeAsync() + { + var simulatorDirectory = Directory.GetCurrentDirectory(); + var executable = Path.GetFileNameWithoutExtension(typeof(Program).Assembly.Location); + _simulatorExePath = Path.Combine(simulatorDirectory, executable); + + KillExistingSimulators(); + + _simulatorProcess = Process.Start(new ProcessStartInfo(_simulatorExePath) + { + WorkingDirectory = simulatorDirectory, + UseShellExecute = false, + RedirectStandardOutput = true, + CreateNoWindow = true, + Environment = { new KeyValuePair("ASPNETCORE_ENVIRONMENT", "Test") } + }); + + await Task.Delay(1000); + } + + public Task DisposeAsync() + { + Dispose(); + return Task.CompletedTask; + } + + public void Dispose() + { + if (!_disposed) + { + if (_simulatorProcess?.HasExited == false) + { + _simulatorProcess?.Kill(true); + _simulatorProcess?.WaitForExit(); + } + + _disposed = true; + GC.SuppressFinalize(this); + } + } + + private void KillExistingSimulators() + { + try + { + // Kill any existing instances of the test simulator that may still be hanging around. + // Note: there shouldn't be any unless something went wrong and the test runner didn't exit cleanly. + var simulatorProcesses = Process.GetProcesses() + .Where(o => o.ProcessName == SimulatorFileName) + .Where(o => string.Equals(o.MainModule?.FileName, _simulatorExePath, StringComparison.OrdinalIgnoreCase)) + .ToArray(); + + foreach (var process in simulatorProcesses) + { + process.Kill(); + } + } + catch + { + // + } + } + + ~ActualSimulatorFixture() + { + Dispose(); + } +} diff --git a/src/AzureEventGridSimulator.Tests/ActualSimulatorTests/AzureMessagingEventGridTest.cs b/src/AzureEventGridSimulator.Tests/ActualSimulatorTests/AzureMessagingEventGridTest.cs index 4692810..d9307ea 100644 --- a/src/AzureEventGridSimulator.Tests/ActualSimulatorTests/AzureMessagingEventGridTest.cs +++ b/src/AzureEventGridSimulator.Tests/ActualSimulatorTests/AzureMessagingEventGridTest.cs @@ -1,99 +1,99 @@ -using System; -using System.Net; -using System.Threading.Tasks; -using Azure; -using Azure.Core; -using Azure.Messaging.EventGrid; -using Shouldly; -using Xunit; - -namespace AzureEventGridSimulator.Tests.ActualSimulatorTests; - -/// -/// Simple tests to check that we can send an event via Azure.Messaging.EventGrid library. -/// NOTE: These tests require (and automatically start) an actual instance of AzureEventGridSimulator.exe as there is no way to inject an HttpClient (from a WebApplicationFactory) -/// into Azure.Messaging.EventGrid. -/// -[Collection(nameof(ActualSimulatorFixtureCollection))] -[Trait("Category", "integration-actual")] -public class AzureMessagingEventGridTest -{ - // ReSharper disable once NotAccessedField.Local - private readonly ActualSimulatorFixture _actualSimulatorFixture; - - public AzureMessagingEventGridTest(ActualSimulatorFixture actualSimulatorFixture) - { - _actualSimulatorFixture = actualSimulatorFixture; - } - - [Fact] - public async Task GivenValidEvent_WhenUriContainsNonStandardPort_ThenItShouldBeAccepted() - { - var client = new EventGridPublisherClient( - new Uri("https://localhost:60101/api/events"), - new AzureKeyCredential("TheLocal+DevelopmentKey="), - new EventGridPublisherClientOptions - { Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); - - var response = await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", new { Id = 1, Foo = "Bar" })); - - response.Status.ShouldBe((int)HttpStatusCode.OK); - } - - [Fact] - public async Task GivenValidEvents_WhenUriContainsNonStandardPort_TheyShouldBeAccepted() - { - var client = new EventGridPublisherClient( - new Uri("https://localhost:60101/api/events"), - new AzureKeyCredential("TheLocal+DevelopmentKey="), - new EventGridPublisherClientOptions - { Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); - - var events = new[] - { - new EventGridEvent("/the/subject1", "The.Event.Type1", "v1", new { Id = 1, Foo = "Bar" }), - new EventGridEvent("/the/subject2", "The.Event.Type2", "v1", new { Id = 2, Foo = "Baz" }) - }; - - var response = await client.SendEventsAsync(events); - - response.Status.ShouldBe((int)HttpStatusCode.OK); - } - - [Fact] - public async Task GivenValidEvent_WhenUriContainsNonExistentPort_ThenItShouldNotBeAccepted() - { - var client = new EventGridPublisherClient( - new Uri("https://localhost:19999/api/events"), - new AzureKeyCredential("TheLocal+DevelopmentKey="), - new EventGridPublisherClientOptions - { Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); - - var exception = await Should.ThrowAsync(async () => - { - await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", - new { Id = 1, Foo = "Bar" })); - }); - - exception.Message.ShouldContain("actively refused"); - exception.Status.ShouldBe(0); - } - - [Fact] - public async Task GivenValidEvent_WhenKeyIsWrong_ThenItShouldNotBeAccepted() - { - var client = new EventGridPublisherClient( - new Uri("https://localhost:60101/api/events"), - new AzureKeyCredential("TheWrongLocal+DevelopmentKey="), - new EventGridPublisherClientOptions - { Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); - - var exception = await Should.ThrowAsync(async () => - { - await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", - new { Id = 1, Foo = "Bar" })); - }); - - exception.Status.ShouldBe((int)HttpStatusCode.Unauthorized); - } -} +using System; +using System.Net; +using System.Threading.Tasks; +using Azure; +using Azure.Core; +using Azure.Messaging.EventGrid; +using Shouldly; +using Xunit; + +namespace AzureEventGridSimulator.Tests.ActualSimulatorTests; + +/// +/// Simple tests to check that we can send an event via Azure.Messaging.EventGrid library. +/// NOTE: These tests require (and automatically start) an actual instance of AzureEventGridSimulator.exe as there is no way to inject an HttpClient (from a WebApplicationFactory) +/// into Azure.Messaging.EventGrid. +/// +[Collection(nameof(ActualSimulatorFixtureCollection))] +[Trait("Category", "integration-actual")] +public class AzureMessagingEventGridTest +{ + // ReSharper disable once NotAccessedField.Local + private readonly ActualSimulatorFixture _actualSimulatorFixture; + + public AzureMessagingEventGridTest(ActualSimulatorFixture actualSimulatorFixture) + { + _actualSimulatorFixture = actualSimulatorFixture; + } + + [Fact] + public async Task GivenValidEvent_WhenUriContainsNonStandardPort_ThenItShouldBeAccepted() + { + var client = new EventGridPublisherClient( + new Uri("https://localhost:60101/api/events"), + new AzureKeyCredential("TheLocal+DevelopmentKey="), + new EventGridPublisherClientOptions + { Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); + + var response = await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", new { Id = 1, Foo = "Bar" })); + + response.Status.ShouldBe((int)HttpStatusCode.OK); + } + + [Fact] + public async Task GivenValidEvents_WhenUriContainsNonStandardPort_TheyShouldBeAccepted() + { + var client = new EventGridPublisherClient( + new Uri("https://localhost:60101/api/events"), + new AzureKeyCredential("TheLocal+DevelopmentKey="), + new EventGridPublisherClientOptions + { Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); + + var events = new[] + { + new EventGridEvent("/the/subject1", "The.Event.Type1", "v1", new { Id = 1, Foo = "Bar" }), + new EventGridEvent("/the/subject2", "The.Event.Type2", "v1", new { Id = 2, Foo = "Baz" }) + }; + + var response = await client.SendEventsAsync(events); + + response.Status.ShouldBe((int)HttpStatusCode.OK); + } + + [Fact] + public async Task GivenValidEvent_WhenUriContainsNonExistentPort_ThenItShouldNotBeAccepted() + { + var client = new EventGridPublisherClient( + new Uri("https://localhost:19999/api/events"), + new AzureKeyCredential("TheLocal+DevelopmentKey="), + new EventGridPublisherClientOptions + { Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); + + var exception = await Should.ThrowAsync(async () => + { + await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", + new { Id = 1, Foo = "Bar" })); + }); + + exception.Message.ShouldContain("refused"); + exception.Status.ShouldBe(0); + } + + [Fact] + public async Task GivenValidEvent_WhenKeyIsWrong_ThenItShouldNotBeAccepted() + { + var client = new EventGridPublisherClient( + new Uri("https://localhost:60101/api/events"), + new AzureKeyCredential("TheWrongLocal+DevelopmentKey="), + new EventGridPublisherClientOptions + { Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); + + var exception = await Should.ThrowAsync(async () => + { + await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", + new { Id = 1, Foo = "Bar" })); + }); + + exception.Status.ShouldBe((int)HttpStatusCode.Unauthorized); + } +} diff --git a/src/AzureEventGridSimulator.Tests/AzureEventGridSimulator.Tests.csproj b/src/AzureEventGridSimulator.Tests/AzureEventGridSimulator.Tests.csproj index dbca616..5355dfa 100644 --- a/src/AzureEventGridSimulator.Tests/AzureEventGridSimulator.Tests.csproj +++ b/src/AzureEventGridSimulator.Tests/AzureEventGridSimulator.Tests.csproj @@ -7,10 +7,10 @@ - - + + - +