예제 #1
0
        public async Task WhenCallingUrl_AddsDefaultAblyHeader()
        {
            var response = new HttpResponseMessage(HttpStatusCode.Accepted)
            {
                Content = new StringContent("Success")
            };
            var handler = new FakeHttpMessageHandler(response);
            var client  = new AblyHttpClient(new AblyHttpOptions(), handler);

            await client.Execute(new AblyRequest("/test", HttpMethod.Get));

            var values = handler.LastRequest.Headers.GetValues("X-Ably-Lib");

            values.Should().NotBeEmpty();
            values.First().Should().Be("dotnet-" + typeof(Defaults).Assembly.GetName().Version.ToString(3));
        }
예제 #2
0
        public async Task WhenCallingUrl_AddsDefaultAblyLibraryVersionHeader()
        {
            var response = new HttpResponseMessage(HttpStatusCode.Accepted)
            {
                Content = new StringContent("Success")
            };
            var handler = new FakeHttpMessageHandler(response);
            var client  = new AblyHttpClient(new AblyHttpOptions(), handler);

            await client.Execute(new AblyRequest("/test", HttpMethod.Get));

            var values = handler.LastRequest.Headers.GetValues("X-Ably-Version");

            values.Should().NotBeEmpty();
            values.First().Should().Be(Defaults.ProtocolVersion);
        }
예제 #3
0
        public async Task WhenCallingUrl_AddsDefaultAblyHeader()
        {
            var response = new HttpResponseMessage(HttpStatusCode.Accepted)
            {
                Content = new StringContent("Success")
            };
            var handler = new FakeHttpMessageHandler(response);
            var client  = new AblyHttpClient(new AblyHttpOptions(), handler);

            await client.Execute(new AblyRequest("/test", HttpMethod.Get));

            var values = handler.LastRequest.Headers.GetValues("X-Ably-Lib").ToArray();

            values.Should().NotBeEmpty();
            values.First().Should().StartWith("dotnet");
            values.First().Should().Be(Defaults.LibraryVersion);
            Defaults.LibraryVersion.Should().BeEquivalentTo($"dotnet.{IoC.PlatformId}-{Defaults.AssemblyVersion}");
        }
예제 #4
0
            public async Task ShouldOnlyRetryFallbackHostWhileTheTimeTakenIsLessThanHttpMaxRetryDuration()
            {
                var options = new ClientOptions(ValidKey)
                {
                    HttpMaxRetryDuration = TimeSpan.FromSeconds(21)
                };
                var client = new AblyRest(options);

                _response.StatusCode = HttpStatusCode.BadGateway;
                var handler = new FakeHttpMessageHandler(_response,
                                                         () =>
                {
                    //Tweak time to pretend 10 seconds have ellapsed
                    NowAddSeconds(10);
                });

                client.HttpClient.CreateInternalHttpClient(TimeSpan.FromSeconds(6), handler);

                var ex = await Assert.ThrowsAsync <AblyException>(() => MakeAnyRequest(client));

                handler.Requests.Count.Should().Be(3); //First attempt is with rest.ably.io
            }
예제 #5
0
        public async Task WhenCallingUrl_AddsRequestIdIfSetTrue()
        {
            var response = new HttpResponseMessage(HttpStatusCode.Accepted)
            {
                Content = new StringContent("Success")
            };
            var handler = new FakeHttpMessageHandler(response);
            var client  = new AblyHttpClient(new AblyHttpOptions {
                AddRequestIds = true
            }, handler);
            var ablyRequest = new AblyRequest("/test", HttpMethod.Get);

            ablyRequest.AddHeaders(new Dictionary <string, string> {
                { "request_id", "custom_request_id" }
            });
            await client.Execute(ablyRequest);

            var values = handler.LastRequest.Headers.GetValues("request_id").ToArray();

            values.Should().NotBeEmpty();
            values.First().Should().StartWith("custom_request_id");
        }
예제 #6
0
        public async Task WhenCallingUrlWithPostParamsAndEmptyBody_PassedTheParamsAsUrlEncodedValues()
        {
            var response = new HttpResponseMessage(HttpStatusCode.Accepted)
            {
                Content = new StringContent("Success")
            };
            var handler = new FakeHttpMessageHandler(response);
            var client  = new AblyHttpClient(new AblyHttpOptions(), handler);

            var ablyRequest = new AblyRequest("/test", HttpMethod.Post)
            {
                PostParameters = new Dictionary <string, string> {
                    { "test", "test" }, { "best", "best" }
                },
            };

            await client.Execute(ablyRequest);

            var content     = handler.LastRequest.Content;
            var formContent = content as FormUrlEncodedContent;

            formContent.Should().NotBeNull("Content should be of type FormUrlEncodedContent");
        }
예제 #7
0
            public async Task ShouldUseDefaultFallbackHostsIfNullArrayProvided()
            {
                List <string> attemptedList = new List <string>();

                var client = CreateClient(options =>
                {
                    options.FallbackHosts = null;
                });

                var handler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.BadGateway));

                client.HttpClient.CreateInternalHttpClient(TimeSpan.FromSeconds(6), handler);

                await Assert.ThrowsAsync <AblyException>(() => MakeAnyRequest(client));

                attemptedList.AddRange(handler.Requests.Select(x => x.RequestUri.Host).ToList());

                attemptedList.Count.Should().Be(3); // HttpMaxRetryCount defaults to 3
                attemptedList[0].Should().Be("rest.ably.io");
                attemptedList[1].Should().EndWith("ably-realtime.com");
                attemptedList[2].Should().EndWith("ably-realtime.com");
                attemptedList[1].Should().NotBe(attemptedList[2]);
            }
예제 #8
0
        private static AblyRealtime GetRealtimeClientWithFakeMessageHandler(ClientOptions options = null, FakeHttpMessageHandler fakeMessageHandler = null, IMobileDevice mobileDevice = null)
        {
            var clientOptions = options ?? new ClientOptions(ValidKey);

            clientOptions.SkipInternetCheck = true; // This is for the Unit tests
            var client = new AblyRealtime(clientOptions, mobileDevice);

            if (fakeMessageHandler != null)
            {
                client.RestClient.HttpClient.CreateInternalHttpClient(TimeSpan.FromSeconds(10), fakeMessageHandler);
            }

            return(client);
        }
예제 #9
0
        internal AblyRealtime GetClientWithFakeTransportAndMessageHandler(Action <ClientOptions> optionsAction = null, FakeHttpMessageHandler messageHandler = null)
        {
            var options = new ClientOptions(ValidKey)
            {
                TransportFactory = FakeTransportFactory
            };

            optionsAction?.Invoke(options);
            var client = GetRealtimeClientWithFakeMessageHandler(options, messageHandler);

            return(client);
        }