public async Task ServiceSendsRequestWithPutVerb()
 {
     using (new MockServer(MockApiClientOptions.TestPort, string.Empty, (req, rsp, prm) =>
     {
         TestOutputHelper.WriteLine($"HttpMethod: {req.HttpMethod}");
         Assert.Equal("PUT", req.HttpMethod);
     }))
     {
         var client  = CreateClient();
         var service = new MockApiService(client);
         await service.MockPut();
     }
 }
        public async Task ServiceAllowMultipleDisposible()
        {
            using (new MockServer(MockApiClientOptions.TestPort, string.Empty, (req, rsp, prm) =>
            {
                TestOutputHelper.WriteLine($"HttpMethod: {req.HttpMethod}");
                Assert.Equal("GET", req.HttpMethod);
            }))
            {
                var client  = CreateClient();
                var service = new MockApiService(client);
                await service.MockDisposibleCall();

                await service.MockDisposibleCall();

                await service.MockDisposibleCall();
            }
        }
        public async Task AllowAnnonymousDoesNotInvokeAuthHandler()
        {
            using (new MockServer(MockApiClientOptions.TestPort, string.Empty, (req, rsp, prm) =>
            {
                Assert.DoesNotContain("Authorization", req.Headers.AllKeys);
            }))
            {
                var authHandler = new MockAuthenticationHandler();
                var client      = CreateClient(authHandler);

                Assert.False(authHandler.DidGetToken);
                var service = new MockApiService(client);
                await service.MockAnnonymousCall();

                Assert.False(authHandler.DidGetToken);
            }
        }
        public async Task AuthenticatedCallInvokesAuthHandler()
        {
            using (new MockServer(MockApiClientOptions.TestPort, string.Empty, (req, rsp, prm) =>
            {
                foreach (var key in req.Headers.AllKeys)
                {
                    TestOutputHelper.WriteLine(key);
                }
                Assert.Contains("Authorization", req.Headers.AllKeys);
            }))
            {
                var authHandler = new MockAuthenticationHandler();
                var client      = CreateClient(authHandler);

                Assert.False(authHandler.DidGetToken);
                var service = new MockApiService(client);
                await service.MockGet();

                Assert.True(authHandler.DidGetToken);
            }
        }
        public async Task IncludesExpectedHeaders()
        {
            using (new MockServer(MockApiClientOptions.TestPort, string.Empty, (req, rsp, prm) =>
            {
                var appInfo = new MockAppInfo();
                var deviceInfo = new MockDeviceInfo();
                foreach (var key in req.Headers.AllKeys)
                {
                    TestOutputHelper.WriteLine($"{key}: {req.Headers.Get(key)}");
                }
                Assert.Contains("Authorization", req.Headers.AllKeys);
                Assert.Equal($"BEARER {MockAuthenticationHandler.Token}", req.Headers.Get("Authorization"));

                Assert.Contains("X-MobileAppVer", req.Headers.AllKeys);
                Assert.Equal(appInfo.VersionString, req.Headers.Get("X-MobileAppVer"));
                Assert.Contains("X-DeviceModel", req.Headers.AllKeys);
                Assert.Equal(deviceInfo.Model, req.Headers["X-DeviceModel"]);
                Assert.Contains("X-DeviceManufacturer", req.Headers.AllKeys);
                Assert.Equal(deviceInfo.Manufacturer, req.Headers["X-DeviceManufacturer"]);
                Assert.Contains("X-DeviceName", req.Headers.AllKeys);
                Assert.Equal(deviceInfo.Name, req.Headers["X-DeviceName"]);
                Assert.Contains("X-DevicePlatform", req.Headers.AllKeys);
                Assert.Equal(deviceInfo.Platform.ToString(), req.Headers["X-DevicePlatform"]);
                Assert.Contains("X-DeviceIdiom", req.Headers.AllKeys);
                Assert.Equal(deviceInfo.Idiom.ToString(), req.Headers["X-DeviceIdiom"]);
                Assert.Contains("User-Agent", req.Headers.AllKeys);
                Assert.Equal($"{appInfo.Name}/{appInfo.VersionString}", req.Headers["User-Agent"]);

                Assert.Contains("Cache-Control", req.Headers.AllKeys);
                Assert.Equal("no-cache", req.Headers["Cache-Control"]);

                Assert.Contains("Accept", req.Headers.AllKeys);
                Assert.Equal("application/json", req.Headers["Accept"]);
            }))
            {
                var client  = CreateClient();
                var service = new MockApiService(client);
                await service.MockGet();
            }
        }
        public async Task RetriesBadRequest(int statusCode)
        {
            int count = 0;

            using (new MockServer(MockApiClientOptions.TestPort, string.Empty, (req, rsp, prm) =>
            {
                if (count++ < 2)
                {
                    rsp.StatusCode = statusCode;
                }
            }))
            {
                var authHandler = new MockAuthenticationHandler();
                var client      = CreateClient(authHandler);
                var service     = new MockApiService(client);
                var result      = await service.MockGet();

                Assert.True(authHandler.DidGetToken);
                Assert.True(result.IsSuccessStatusCode);
                Assert.Equal(1, authHandler.GetTokenCount);
                Assert.Equal(3, count);
            }
        }