예제 #1
0
        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();
            }
        }
예제 #2
0
        public async Task StopAsync_SetsStateToStopped()
        {
            var apiBuilder   = new ApiBuilder(Substitute.For <ISimulation>());
            var apiSimulator = new ApiSimulator(apiBuilder);

            await apiSimulator.StartAsync();

            try
            {
                await apiSimulator.StopAsync();

                apiSimulator.State.ShouldBe(SimulatorState.Stopped);
            }
            finally
            {
                await apiSimulator.StopAsync();
            }
        }
예제 #3
0
        public async Task StopAsync_DoesNotThrow_WhenCalledMultipleTimes()
        {
            var apiBuilder   = new ApiBuilder(Substitute.For <ISimulation>());
            var apiSimulator = new ApiSimulator(apiBuilder);

            await apiSimulator.StartAsync();

            try
            {
                await Task.Delay(150);

                await apiSimulator.StopAsync();

                await apiSimulator.StopAsync();
            }
            finally
            {
                await apiSimulator.StopAsync();
            }
        }
예제 #4
0
        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();
            }
        }
예제 #5
0
        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();
            }
        }
예제 #6
0
        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()
                );
        }
예제 #7
0
        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();
        }
예제 #8
0
        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();
        }