public async Task SendAsync_NotFoundWithoutErrorBody()
        {
            using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://localhost"))
                using (var stringContent = new StringContent("test"))
                    using (var httpResponseMessage = new HttpResponseMessage())
                    {
                        httpResponseMessage.Content    = stringContent;
                        httpResponseMessage.StatusCode = HttpStatusCode.NotFound;

                        this.testHttpMessageHandler.AddResponseMapping(httpRequestMessage.RequestUri.ToString(), httpResponseMessage);

                        this.serializer.Setup(
                            serializer => serializer.DeserializeObject <ErrorResponse>(
                                It.IsAny <Stream>()))
                        .Returns((ErrorResponse)null);

                        try
                        {
                            await Assert.ThrowsAsync <ServiceException>(async() => await this.httpProvider.SendAsync(httpRequestMessage));
                        }
                        catch (ServiceException exception)
                        {
                            Assert.True(exception.IsMatch(ErrorConstants.Codes.ItemNotFound));
                            Assert.True(string.IsNullOrEmpty(exception.Error.Message));

                            throw;
                        }
                    }
        }
Exemple #2
0
        public async Task SendAsync_NotFoundWithoutErrorBody()
        {
            using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://localhost"))
                using (var stringContent = new StringContent("test"))
                    using (var httpResponseMessage = new HttpResponseMessage())
                    {
                        httpResponseMessage.Content    = stringContent;
                        httpResponseMessage.StatusCode = HttpStatusCode.NotFound;

                        this.testHttpMessageHandler.AddResponseMapping(httpRequestMessage.RequestUri.ToString(), httpResponseMessage);

                        this.serializer.Setup(
                            serializer => serializer.DeserializeObject <ErrorResponse>(
                                It.IsAny <Stream>()))
                        .Returns((ErrorResponse)null);

                        try
                        {
                            await this.httpProvider.SendAsync(httpRequestMessage);
                        }
                        catch (OneDriveException exception)
                        {
                            Assert.IsNotNull(exception.Error, "No error body returned.");
                            Assert.AreEqual(OneDriveErrorCode.ItemNotFound.ToString(), exception.Error.Code, "Incorrect error code returned.");
                            Assert.IsTrue(string.IsNullOrEmpty(exception.Error.Message), "Unexpected error message returned.");

                            throw;
                        }
                    }
        }
        public async Task PollForOperationCompletionAsync_OperationCompleted()
        {
            bool called = false;

            this.progress.Setup(
                mockProgress => mockProgress.Report(
                    It.IsAny <AsyncOperationStatus>()))
            .Callback <AsyncOperationStatus>(status => this.ProgressCallback(status, out called));

            this.serializer.Setup(serializer => serializer.DeserializeObject <AsyncOperationStatus>(It.IsAny <Stream>())).Returns(new AsyncOperationStatus());
            this.serializer.Setup(serializer => serializer.DeserializeObject <DerivedTypeClass>(It.IsAny <Stream>())).Returns(new DerivedTypeClass {
                Id = "id"
            });

            using (var redirectedResponseMessage = new HttpResponseMessage())
                using (var stringContent = new StringContent("content"))
                    using (var redirectedStringContent = new StringContent("content"))
                    {
                        this.httpResponseMessage.Content    = stringContent;
                        this.httpResponseMessage.StatusCode = HttpStatusCode.Accepted;
                        redirectedResponseMessage.Content   = redirectedStringContent;

                        this.httpProvider.Setup(provider =>
                                                provider.SendAsync(
                                                    It.Is <HttpRequestMessage>(requestMessage => requestMessage.RequestUri.ToString().Equals(AsyncMonitorTests.itemUrl))))
                        .Returns(Task.FromResult(redirectedResponseMessage));

                        var item = await this.asyncMonitor.PollForOperationCompletionAsync(this.progress.Object, CancellationToken.None);

                        Assert.True(called);
                        Assert.NotNull(item);
                        Assert.Equal("id", item.Id);
                    }
        }
Exemple #4
0
        protected async Task AuthenticateWithRefreshToken(AccountSession refreshedAccountSession)
        {
            using (var httpResponseMessage = new HttpResponseMessage())
                using (var responseStream = new MemoryStream())
                    using (var streamContent = new StreamContent(responseStream))
                    {
                        httpResponseMessage.Content = streamContent;

                        this.httpProvider.Setup(
                            provider => provider.SendAsync(
                                It.Is <HttpRequestMessage>(
                                    request => request.RequestUri.ToString().Equals(this.serviceInfo.TokenServiceUrl))))
                        .Returns(Task.FromResult <HttpResponseMessage>(httpResponseMessage));

                        this.serializer.Setup(
                            serializer => serializer.DeserializeObject <IDictionary <string, string> >(It.IsAny <Stream>()))
                        .Returns(new Dictionary <string, string>
                        {
                            { Constants.Authentication.AccessTokenKeyName, refreshedAccountSession.AccessToken },
                            { Constants.Authentication.RefreshTokenKeyName, refreshedAccountSession.RefreshToken },
                        });

                        var accountSession = await this.authenticationProvider.AuthenticateAsync();

                        Assert.IsNotNull(accountSession, "No account session returned.");
                        Assert.AreEqual(
                            refreshedAccountSession.AccessToken,
                            accountSession.AccessToken,
                            "Unexpected access token returned.");
                        Assert.AreEqual(
                            refreshedAccountSession.RefreshToken,
                            accountSession.RefreshToken,
                            "Unexpected refresh token returned.");
                        Assert.AreEqual(
                            refreshedAccountSession.AccessToken,
                            this.authenticationProvider.CurrentAccountSession.AccessToken,
                            "Unexpected cached access token.");
                        Assert.AreEqual(
                            refreshedAccountSession.RefreshToken,
                            this.authenticationProvider.CurrentAccountSession.RefreshToken,
                            "Unexpected cached refresh token.");
                    }
        }
Exemple #5
0
        public async Task PollForOperationCompletionAsync_OperationCompleted()
        {
            bool called = false;

            this.progress.Setup(
                mockProgress => mockProgress.Report(
                    It.IsAny <AsyncOperationStatus>()))
            .Callback <AsyncOperationStatus>(status => this.ProgressCallback(status, out called));

            this.serializer.Setup(serializer => serializer.DeserializeObject <AsyncOperationStatus>(It.IsAny <Stream>())).Returns(new AsyncOperationStatus());
            this.serializer.Setup(serializer => serializer.DeserializeObject <Item>(It.IsAny <Stream>())).Returns(new Item {
                Id = "id"
            });
            this.oneDriveClient.SetupGet(client => client.IsAuthenticated).Returns(false);

            using (var redirectedResponseMessage = new HttpResponseMessage())
                using (var stringContent = new StringContent("content"))
                    using (var redirectedStringContent = new StringContent("content"))
                    {
                        this.httpResponseMessage.Content    = stringContent;
                        this.httpResponseMessage.StatusCode = HttpStatusCode.Accepted;
                        redirectedResponseMessage.Content   = redirectedStringContent;

                        this.httpProvider.Setup(provider =>
                                                provider.SendAsync(
                                                    It.Is <HttpRequestMessage>(requestMessage => requestMessage.RequestUri.ToString().Equals(AsyncMonitorTests.itemUrl))))
                        .Returns(Task.FromResult(redirectedResponseMessage));

                        var item = await this.asyncMonitor.CompleteOperationAsync(this.progress.Object, CancellationToken.None);

                        Assert.IsTrue(called, "Progress not called");
                        Assert.IsNotNull(item, "No item returned.");
                        Assert.AreEqual("id", item.Id, "Unexpected item returned.");

                        this.oneDriveClient.Verify(client => client.AuthenticateAsync(), Times.Exactly(2));
                        this.authenticationProvider.Verify(
                            provider => provider.AppendAuthHeaderAsync(
                                It.Is <HttpRequestMessage>(message => message.RequestUri.ToString().Equals(AsyncMonitorTests.monitorUrl))),
                            Times.Once);

                        this.authenticationProvider.Verify(
                            provider => provider.AppendAuthHeaderAsync(
                                It.Is <HttpRequestMessage>(message => message.RequestUri.ToString().Equals(AsyncMonitorTests.itemUrl))),
                            Times.Once);
                    }
        }