Exemple #1
0
        public async Task SendServiceAsync_OnBadRequest(string content, string expectedMessage)
        {
            var handler  = new MockDelegatingHandler();
            var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = content == null ? null : new StringContent(content, Encoding.UTF8, "application/json")
            };

            handler.Responses.Add(response);

            var options = new DatasyncClientOptions
            {
                HttpPipeline   = new HttpMessageHandler[] { handler },
                InstallationId = "hijack",
                UserAgent      = "hijack"
            };
            var client  = new WrappedHttpClient(Endpoint, options);
            var request = new ServiceRequest {
                Method = HttpMethod.Get, UriPathAndQuery = "/tables/movies/"
            };
            var exception = await Assert.ThrowsAsync <DatasyncInvalidOperationException>(() => client.WrappedSendAsync(request));

            Assert.Same(response, exception.Response);
            Assert.StartsWith(expectedMessage, exception.Message);

            // Check that the right headers were applied to the request
            Assert.Single(handler.Requests);
            var actual = handler.Requests[0];

            Assert.Equal(options.UserAgent, actual.Headers.UserAgent.ToString());
            AssertEx.HasHeader(actual.Headers, "X-ZUMO-VERSION", options.UserAgent);
            AssertEx.HasHeader(actual.Headers, "ZUMO-API-VERSION", "3.0.0");
            AssertEx.HasHeader(actual.Headers, "X-ZUMO-INSTALLATION-ID", options.InstallationId);
        }
Exemple #2
0
        public async Task SendHttpAsync_OnSuccessfulRequest_WithAuth()
        {
            var handler = new MockDelegatingHandler();
            var options = new DatasyncClientOptions
            {
                HttpPipeline   = new HttpMessageHandler[] { handler },
                InstallationId = "hijack",
                UserAgent      = "hijack"
            };
            var requestor = () => Task.FromResult(ValidAuthenticationToken);
            var client    = new WrappedHttpClient(Endpoint, new GenericAuthenticationProvider(requestor, "X-ZUMO-AUTH"), options);
            var response  = new HttpResponseMessage(HttpStatusCode.OK);

            handler.Responses.Add(response);
            var request = new HttpRequestMessage(HttpMethod.Get, "");

            var actualResponse = await client.WrappedSendAsync(request).ConfigureAwait(false);

            Assert.Same(response, actualResponse);      // We get the provided response back.

            // Check that the right headers were applied to the request
            Assert.Single(handler.Requests);
            var actual = handler.Requests[0];

            Assert.Equal(options.UserAgent, actual.Headers.UserAgent.ToString());
            AssertEx.HasHeader(actual.Headers, "X-ZUMO-VERSION", options.UserAgent);
            AssertEx.HasHeader(actual.Headers, "ZUMO-API-VERSION", "3.0.0");
            AssertEx.HasHeader(actual.Headers, "X-ZUMO-AUTH", ValidAuthenticationToken.Token);
            AssertEx.HasHeader(actual.Headers, "X-ZUMO-INSTALLATION-ID", options.InstallationId);
        }
Exemple #3
0
        public void CtorStringOptions_Null_Throws()
        {
            const string          endpoint = null;
            DatasyncClientOptions options  = null;

            Assert.Throws <ArgumentNullException>(() => new DatasyncClient(endpoint, options));
        }
Exemple #4
0
        public async Task SendServiceAsync_OnSuccessfulRequest()
        {
            var handler  = new MockDelegatingHandler();
            var response = new HttpResponseMessage(HttpStatusCode.OK);

            handler.Responses.Add(response);

            var options = new DatasyncClientOptions
            {
                HttpPipeline   = new HttpMessageHandler[] { handler },
                InstallationId = "hijack",
                UserAgent      = "hijack"
            };
            var client = new WrappedHttpClient(Endpoint, options);

            var request = new ServiceRequest {
                Method = HttpMethod.Get, UriPathAndQuery = "/tables/movies/", EnsureResponseContent = false
            };
            var actualResponse = await client.WrappedSendAsync(request).ConfigureAwait(false);

            Assert.Equal(200, actualResponse.StatusCode);

            // Check that the right headers were applied to the request
            Assert.Single(handler.Requests);
            var actual = handler.Requests[0];

            Assert.Equal(options.UserAgent, actual.Headers.UserAgent.ToString());
            AssertEx.HasHeader(actual.Headers, "X-ZUMO-VERSION", options.UserAgent);
            AssertEx.HasHeader(actual.Headers, "ZUMO-API-VERSION", "3.0.0");
            AssertEx.HasHeader(actual.Headers, "X-ZUMO-INSTALLATION-ID", options.InstallationId);
        }
Exemple #5
0
        public DatasyncClient_Tests() : base()
        {
            var store = new Mock <IOfflineStore>();

            clientOptions = new DatasyncClientOptions {
                OfflineStore = store.Object
            };
        }
Exemple #6
0
        public void CtorUriOptions_Valid_SetsEndpoint(EndpointTestCase testcase)
        {
            var options = new DatasyncClientOptions();
            var client  = new DatasyncClient(new Uri(testcase.BaseEndpoint), options);

            Assert.Equal(testcase.NormalizedEndpoint, client.Endpoint.ToString());
            Assert.Same(options, client.ClientOptions);
            Assert.NotNull(client.HttpClient);
        }
Exemple #7
0
        public void InstallationId_CanBeOverridden()
        {
            var options = new DatasyncClientOptions {
                InstallationId = "hijack"
            };
            var client = new DatasyncClient(Endpoint, options);

            Assert.Equal("hijack", client.InstallationId);
        }
Exemple #8
0
        public void CtorUriAuthOptions_Valid_SetsEndpoint(EndpointTestCase testcase)
        {
            var options      = new DatasyncClientOptions();
            var authProvider = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken), "X-ZUMO-AUTH");
            var client       = new DatasyncClient(new Uri(testcase.BaseEndpoint), authProvider, options);

            Assert.Equal(testcase.NormalizedEndpoint, client.Endpoint.ToString());
            Assert.Same(options, client.ClientOptions);
            Assert.NotNull(client.HttpClient);
        }
Exemple #9
0
        public async Task SendHttpAsync_ThrowsTimeout_WhenOperationCanceled()
        {
            var handler = new TimeoutDelegatingHandler();
            var options = new DatasyncClientOptions {
                HttpPipeline = new HttpMessageHandler[] { handler }
            };
            var client  = new WrappedHttpClient(Endpoint, options);
            var request = new HttpRequestMessage(HttpMethod.Get, "");

            await Assert.ThrowsAsync <TimeoutException>(() => client.WrappedSendAsync(request)).ConfigureAwait(false);
        }
Exemple #10
0
        public void CreatePipeline_CB_ThrowsArgumentException()
        {
            var b = new MockDelegatingHandler();
            var c = new HttpClientHandler();

            var options = new DatasyncClientOptions {
                HttpPipeline = new HttpMessageHandler[] { c, b }
            };

            // Act
            Assert.Throws <ArgumentException>(() => _ = new WrappedHttpClient(Endpoint, options));
        }
Exemple #11
0
        public void CreatePipeline_NoHandlers_CreatesPipeline()
        {
            var options = new DatasyncClientOptions {
                HttpPipeline = Array.Empty <HttpMessageHandler>()
            };

            // Act
            var client = new WrappedHttpClient(Endpoint, options);

            // Assert
            Assert.IsAssignableFrom <HttpClientHandler>(client.HttpHandler);
        }
        public void Ctor_CreateTable_WhenArgsCorrect()
        {
            var store   = new MockOfflineStore();
            var options = new DatasyncClientOptions {
                OfflineStore = store
            };
            var client = new DatasyncClient(Endpoint, options);
            var table  = new OfflineTable("movies", client);

            Assert.Same(client, table.ServiceClient);
            Assert.Equal("movies", table.TableName);
        }
Exemple #13
0
 public async Task SendServiceAsync_ThrowsTimeout_WhenOperationCanceled()
 {
     var handler = new TimeoutDelegatingHandler();
     var options = new DatasyncClientOptions {
         HttpPipeline = new HttpMessageHandler[] { handler }
     };
     var client  = new WrappedHttpClient(Endpoint, options);
     var request = new ServiceRequest {
         Method = HttpMethod.Get, UriPathAndQuery = "/tables/movies/"
     };
     await Assert.ThrowsAsync <TimeoutException>(() => client.WrappedSendAsync(request)).ConfigureAwait(false);
 }
Exemple #14
0
        public void SerializerSettings_CopiedToSerializer()
        {
            var settings = new DatasyncSerializerSettings {
                CamelCasePropertyNames = true
            };
            var options = new DatasyncClientOptions {
                SerializerSettings = settings
            };
            var client = new DatasyncClient(Endpoint, options);

            Assert.Same(settings, client.Serializer.SerializerSettings);
        }
Exemple #15
0
        public void CreatePipeline_C_CreatesPipeline()
        {
            var c = new HttpClientHandler();

            var options = new DatasyncClientOptions {
                HttpPipeline = new HttpMessageHandler[] { c }
            };

            // Act
            var client = new WrappedHttpClient(Endpoint, options);

            // Assert
            Assert.Same(c, client.HttpHandler);
        }
Exemple #16
0
        public void CreatePipeline_NoHandlersWithAuth_CreatesPipeline()
        {
            var options = new DatasyncClientOptions {
                HttpPipeline = Array.Empty <HttpMessageHandler>()
            };

            // Act
            var authProvider = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken), "X-ZUMO-AUTH");
            var client       = new WrappedHttpClient(Endpoint, authProvider, options);

            // Assert
            Assert.Same(authProvider, client.HttpHandler);
            Assert.IsAssignableFrom <HttpClientHandler>(authProvider.InnerHandler);
        }
Exemple #17
0
        public void CreatePipeline_CBWithAuth_ThrowsArgumentException()
        {
            var b = new MockDelegatingHandler();
            var c = new HttpClientHandler();

            var options = new DatasyncClientOptions {
                HttpPipeline = new HttpMessageHandler[] { c, b }
            };

            // Act
            var authProvider = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken), "X-ZUMO-AUTH");

            Assert.Throws <ArgumentException>(() => _ = new WrappedHttpClient(Endpoint, authProvider, options));
        }
Exemple #18
0
        public void CreatePipeline_B_CreatesPipeline()
        {
            var b = new MockDelegatingHandler();

            var options = new DatasyncClientOptions {
                HttpPipeline = new HttpMessageHandler[] { b }
            };

            // Act
            var client = new WrappedHttpClient(Endpoint, options);

            // Assert
            Assert.Same(b, client.HttpHandler);
            Assert.IsAssignableFrom <HttpClientHandler>(b.InnerHandler);
        }
Exemple #19
0
        public void Ctor_ValidEndpoint_CreatesClient(EndpointTestCase testcase)
        {
            var options = new DatasyncClientOptions();
            var client  = new WrappedHttpClient(new Uri(testcase.NormalizedEndpoint), options);

            Assert.Equal(testcase.NormalizedEndpoint, client.WrappedEndpoint);
            Assert.NotNull(client.HttpHandler);
            Assert.NotNull(client.HttpClient);

            // DefaultRequestHeaders must contain the list of headers
            Assert.StartsWith("Datasync/", client.HttpClient.DefaultRequestHeaders.UserAgent.ToString());
            Assert.Equal(client.HttpClient.DefaultRequestHeaders.UserAgent.ToString(),
                         client.HttpClient.DefaultRequestHeaders.GetValues("X-ZUMO-VERSION").FirstOrDefault());
            AssertEx.HasHeader(client.HttpClient.DefaultRequestHeaders, "ZUMO-API-VERSION", "3.0.0");
            AssertEx.HasHeader(client.HttpClient.DefaultRequestHeaders, "X-ZUMO-INSTALLATION-ID", Platform.InstallationId);
            Assert.Equal(testcase.NormalizedEndpoint, client.HttpClient.BaseAddress.ToString());
        }
Exemple #20
0
        public void CreatePipeline_ABC_CreatesPipeline()
        {
            var a = new MockDelegatingHandler();
            var b = new MockDelegatingHandler();
            var c = new HttpClientHandler();

            var options = new DatasyncClientOptions {
                HttpPipeline = new HttpMessageHandler[] { a, b, c }
            };

            // Act
            var client = new WrappedHttpClient(Endpoint, options);

            // Assert
            Assert.Same(a, client.HttpHandler);
            Assert.Same(b, a.InnerHandler);
            Assert.Same(c, b.InnerHandler);
        }
Exemple #21
0
        public void CreatePipeline_BCWithAuth_CreatesPipeline()
        {
            var b = new MockDelegatingHandler();
            var c = new HttpClientHandler();

            var options = new DatasyncClientOptions {
                HttpPipeline = new HttpMessageHandler[] { b, c }
            };

            // Act
            var authProvider = new GenericAuthenticationProvider(() => Task.FromResult(ValidAuthenticationToken), "X-ZUMO-AUTH");
            var client       = new WrappedHttpClient(Endpoint, authProvider, options);

            // Assert
            Assert.Same(authProvider, client.HttpHandler);
            Assert.Same(b, authProvider.InnerHandler);
            Assert.Same(c, b.InnerHandler);
        }
Exemple #22
0
        public void Ctor_ValidEndpoint_CustomOptions_CreatesClient(EndpointTestCase testcase)
        {
            var options = new DatasyncClientOptions {
                HttpPipeline = null, InstallationId = "test-int-id", UserAgent = "test-user-agent"
            };
            var client = new WrappedHttpClient(new Uri(testcase.NormalizedEndpoint), options);

            Assert.Equal(testcase.NormalizedEndpoint, client.WrappedEndpoint);
            Assert.NotNull(client.HttpHandler);
            Assert.NotNull(client.HttpClient);

            // DefaultRequestHeaders must contain the list of headers
            Assert.Equal("test-user-agent", client.HttpClient.DefaultRequestHeaders.UserAgent.ToString());
            AssertEx.HasHeader(client.HttpClient.DefaultRequestHeaders, "X-ZUMO-VERSION", "test-user-agent");
            AssertEx.HasHeader(client.HttpClient.DefaultRequestHeaders, "ZUMO-API-VERSION", "3.0.0");
            AssertEx.HasHeader(client.HttpClient.DefaultRequestHeaders, "X-ZUMO-INSTALLATION-ID", "test-int-id");
            Assert.Equal(testcase.NormalizedEndpoint, client.HttpClient.BaseAddress.ToString());
        }
Exemple #23
0
        public async Task SendServiceAsync_OnSuccessfulRequest_WithAuth()
        {
            var handler  = new MockDelegatingHandler();
            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("{}", Encoding.UTF8, "application/json")
            };

            handler.Responses.Add(response);

            var options = new DatasyncClientOptions
            {
                HttpPipeline   = new HttpMessageHandler[] { handler },
                InstallationId = "hijack",
                UserAgent      = "hijack"
            };
            var requestor = () => Task.FromResult(ValidAuthenticationToken);
            var client    = new WrappedHttpClient(Endpoint, new GenericAuthenticationProvider(requestor, "X-ZUMO-AUTH"), options);
            var request   = new ServiceRequest {
                Method = HttpMethod.Get, UriPathAndQuery = "/tables/movies/"
            };

            var actualResponse = await client.WrappedSendAsync(request).ConfigureAwait(false);

            Assert.Equal(200, actualResponse.StatusCode);
            Assert.Equal("{}", actualResponse.Content);
            Assert.True(actualResponse.HasContent);

            // Check that the right headers were applied to the request
            Assert.Single(handler.Requests);
            var actual = handler.Requests[0];

            Assert.Equal(options.UserAgent, actual.Headers.UserAgent.ToString());
            AssertEx.HasHeader(actual.Headers, "X-ZUMO-VERSION", options.UserAgent);
            AssertEx.HasHeader(actual.Headers, "ZUMO-API-VERSION", "3.0.0");
            AssertEx.HasHeader(actual.Headers, "X-ZUMO-AUTH", ValidAuthenticationToken.Token);
            AssertEx.HasHeader(actual.Headers, "X-ZUMO-INSTALLATION-ID", options.InstallationId);
        }
Exemple #24
0
        public async Task SendServiceAsync_Throws_OnFailedRequest()
        {
            var handler  = new MockDelegatingHandler();
            var response = new HttpResponseMessage(HttpStatusCode.BadRequest);

            handler.Responses.Add(response);

            var options = new DatasyncClientOptions
            {
                HttpPipeline   = new HttpMessageHandler[] { handler },
                InstallationId = "hijack",
                UserAgent      = "hijack"
            };
            var client = new WrappedHttpClient(Endpoint, options);

            var request = new ServiceRequest {
                Method = HttpMethod.Get, UriPathAndQuery = "/tables/movies/", EnsureResponseContent = false
            };
            var exception = await Assert.ThrowsAsync <DatasyncInvalidOperationException>(() => client.WrappedSendAsync(request)).ConfigureAwait(false);

            Assert.NotNull(exception.Request);
            Assert.NotNull(exception.Response);
            Assert.Equal(HttpStatusCode.BadRequest, exception.Response.StatusCode);
        }
Exemple #25
0
 internal WrappedHttpClient(Uri endpoint, AuthenticationProvider provider, DatasyncClientOptions options) : base(endpoint, provider, options)
 {
 }
        /// <summary>
        /// Create a new <see cref="ServiceHttpClient"/> that communicates
        /// with the provided Datasync service endpoint.
        /// </summary>
        /// <param name="endpoint">The endpoint of the Datasync service.</param>
        /// <param name="authenticationProvider">The authentication provider to use (if any)</param>
        /// <param name="clientOptions">The client options to use in configuring the HTTP client.</param>
        internal ServiceHttpClient(Uri endpoint, AuthenticationProvider authenticationProvider, DatasyncClientOptions clientOptions)
        {
            Arguments.IsValidEndpoint(endpoint, nameof(endpoint));
            Arguments.IsNotNull(clientOptions, nameof(clientOptions));

            Endpoint       = endpoint;
            InstallationId = clientOptions.InstallationId ?? Platform.InstallationId;

            roothandler = CreatePipeline(clientOptions.HttpPipeline ?? Array.Empty <HttpMessageHandler>());
            if (authenticationProvider != null)
            {
                authenticationProvider.InnerHandler = roothandler;
                roothandler = authenticationProvider;
            }
            client = new HttpClient(roothandler)
            {
                BaseAddress = Endpoint
            };
            client.DefaultRequestHeaders.Add(ServiceHeaders.ProtocolVersion, ProtocolVersion);
            if (!string.IsNullOrWhiteSpace(clientOptions.UserAgent))
            {
                client.DefaultRequestHeaders.TryAddWithoutValidation(ServiceHeaders.UserAgent, clientOptions.UserAgent);
                client.DefaultRequestHeaders.Add(ServiceHeaders.InternalUserAgent, clientOptions.UserAgent);
            }
            if (clientOptions.InstallationId == null || !string.IsNullOrWhiteSpace(clientOptions.InstallationId))
            {
                client.DefaultRequestHeaders.Add(ServiceHeaders.InstallationId, InstallationId);
            }
        }
Exemple #27
0
 internal WrappedHttpClient(Uri endpoint, DatasyncClientOptions options) : base(endpoint, options)
 {
 }
 /// <summary>
 /// Create a new <see cref="ServiceHttpClient"/> that communicates
 /// with the provided Datasync service endpoint.
 /// </summary>
 /// <param name="endpoint">The endpoint of the Datasync service.</param>
 /// <param name="clientOptions">The client options to use in configuring the HTTP client.</param>
 internal ServiceHttpClient(Uri endpoint, DatasyncClientOptions clientOptions) : this(endpoint, null, clientOptions)
 {
 }
Exemple #29
0
        public void Ctor_InvalidEndpoint_Throws(string endpoint, bool isRelative = false)
        {
            var options = new DatasyncClientOptions();

            Assert.Throws <UriFormatException>(() => new WrappedHttpClient(isRelative ? new Uri(endpoint, UriKind.Relative) : new Uri(endpoint), options));
        }