public async Task StartAsync_Throws_WhenPortInUse() { var tcpListener = new TcpListener(IPAddress.Loopback, 0); tcpListener.Start(); try { var port = ((IPEndPoint)tcpListener.LocalEndpoint).Port; var apiBuilder = new ApiBuilder(Substitute.For <ISimulation>()); apiBuilder.SetPort(port); var apiSimulator = new ApiSimulator(apiBuilder); try { await apiSimulator.StartAsync().ShouldThrowAsync <IOException>(); } finally { await apiSimulator.StopAsync(); } } finally { tcpListener.Stop(); } }
public async Task StartAsync_DoesNotThrow_WhenCalledMultipleTimes() { var apiBuilder = new ApiBuilder(Substitute.For <ISimulation>()); var apiSimulator = new ApiSimulator(apiBuilder); await apiSimulator.StartAsync(); try { await apiSimulator.StartAsync(); await Task.Delay(150); } finally { await apiSimulator.StopAsync(); } }
public async Task Dispose_SetsStateToStopped() { var apiBuilder = new ApiBuilder(Substitute.For <ISimulation>()); var apiSimulator = new ApiSimulator(apiBuilder); await apiSimulator.StartAsync(); apiSimulator.Dispose(); apiSimulator.State.ShouldBe(SimulatorState.Stopped); }
public void StartAsync_Fails_WhenHostCannotBeBuilt() { ILoggerProvider fakeLogger = Substitute.For <ILoggerProvider>(); fakeLogger.CreateLogger(Arg.Any <string>()).Returns(_ => throw new NotSupportedException()); var apiBuilder = new ApiBuilder(Substitute.For <ISimulation>()); var apiSimulator = new ApiSimulator(apiBuilder.SetLoggerProvider(fakeLogger)); apiSimulator.StartAsync().ShouldThrow <NotSupportedException>(); apiSimulator.State.ShouldBe(SimulatorState.Stopped); }
public async Task Dispose_DoesNotThrow_WhenSimulatorRunning() { var apiBuilder = new ApiBuilder(Substitute.For <ISimulation>()); var apiSimulator = new ApiSimulator(apiBuilder); await apiSimulator.StartAsync(); await Task.Delay(100); Action action = () => apiSimulator.Dispose(); action.ShouldNotThrow(); }
public async Task StartAsync_ReportsActiveLocation() { var apiBuilder = new ApiBuilder(Substitute.For <ISimulation>()); var apiSimulator = new ApiSimulator(apiBuilder); await apiSimulator.StartAsync(); try { apiSimulator.Location.ShouldNotBeNullOrEmpty(); } finally { await apiSimulator.StopAsync(); } }
public async Task StartAsync_ReportsActivePort() { var apiBuilder = new ApiBuilder(Substitute.For <ISimulation>()); var apiSimulator = new ApiSimulator(apiBuilder); await apiSimulator.StartAsync(); try { apiSimulator.Port.ShouldNotBe(0); } finally { await apiSimulator.StopAsync(); } }
public async Task StartAsync_SetsStateToRunning() { var apiBuilder = new ApiBuilder(Substitute.For <ISimulation>()); var apiSimulator = new ApiSimulator(apiBuilder); await apiSimulator.StartAsync(); try { apiSimulator.State.ShouldBe(SimulatorState.Running); } finally { await apiSimulator.StopAsync(); } }
public async Task Abort_SetsStateToStopped() { var apiBuilder = new ApiBuilder(Substitute.For <ISimulation>()); var apiSimulator = new ApiSimulator(apiBuilder); await apiSimulator.StartAsync(); try { apiSimulator.Abort(); apiSimulator.State.ShouldBe(SimulatorState.Stopped); } finally { await apiSimulator.StopAsync(); } }
public async Task ReceivedApiCalls_GetsAllRecordedApiCalls() { ApiBuilder apiBuilder = new ApiBuilder(Substitute.For <ISimulation>()) .AddHandler("GET /api/v2/books", _ => throw new Exception()); var apiSimulator = new ApiSimulator(apiBuilder); try { await apiSimulator.StartAsync(); var port = apiSimulator.Port; using var httpClient = new HttpClient(); var task1 = Task.Run(async() => { var request = new HttpRequestMessage(HttpMethod.Get, $"http://localhost:{port}/api/v2/books"); await httpClient.SendAsync(request); }); var task2 = Task.Run(async() => { var request = new HttpRequestMessage(HttpMethod.Patch, $"http://localhost:{port}/api/v2/books/32") { Content = new StringContent("{\"title\":\"abc\"}", Encoding.UTF8, "application/json") }; await httpClient.SendAsync(request); }); await Task.WhenAll(task1, task2); } finally { await apiSimulator.StopAsync(); } System.Collections.Generic.IReadOnlyCollection <ApiCall> apiCalls = apiSimulator.ReceivedApiCalls; ApiCall getCall = apiCalls.FirstOrDefault(call => call.Action == "GET /api/v2/books"); apiCalls.ShouldSatisfyAllConditions( () => getCall.ShouldNotBeNull(), () => getCall.Exception.ShouldNotBeNull(), () => apiCalls.Count.ShouldBe(2), () => apiCalls .Single(call => call.Action == "PATCH /api/v2/books/32") .ShouldNotBeNull() ); }
public async Task StartAsync_Fails_WhenPortAlreadyUsed() { var listener = new TcpListener(IPAddress.Loopback, 0); listener.Start(); try { var usedPort = ((IPEndPoint)listener.LocalEndpoint).Port; var apiBuilder = new ApiBuilder(Substitute.For <ISimulation>()); var apiSimulator = new ApiSimulator(apiBuilder.SetPort(usedPort)); await apiSimulator.StartAsync().ShouldThrowAsync <IOException>(); } finally { listener.Stop(); } }
public async Task StartAsync_UsesCertificate_WhenSpecified() { using X509Certificate2 certificate = TestCertificate.Find(); ApiBuilder apiBuilder = new ApiBuilder(Substitute.For <ISimulation>()).SetCertificate(certificate); var apiSimulator = new ApiSimulator(apiBuilder); await apiSimulator.StartAsync(); try { apiSimulator.Location.ShouldStartWith("https://"); } finally { await apiSimulator.StopAsync(); } certificate.Reset(); }
public void Request_InstantiatesMiddleware() { var apiBuilder = new ApiBuilder(Substitute.For <ISimulation>()); var apiSimulator = new ApiSimulator(apiBuilder); Func <Task> action = async() => { try { await apiSimulator.StartAsync(); var port = apiSimulator.Port; using var httpClient = new HttpClient(); var request = new HttpRequestMessage(HttpMethod.Get, $"http://localhost:{port}/version"); await httpClient.SendAsync(request); } finally { await apiSimulator.StopAsync(); } }; action.ShouldNotThrow(); }