예제 #1
0
        public async Task ProcessFileAsyncRetriesTwice()
        {
            // Arrange
            string content = "Hello world! This is a new test!";
            string path = "foo/qux/test.txt";
            int counter = 0;
            var handler = new TestMessageHandler(request =>
            {
                counter++;
                var response = new HttpResponseMessage();
                if (counter < 3)
                {
                    response.StatusCode = HttpStatusCode.Forbidden;
                }
                else
                {
                    response.Content = new StringContent(content);
                }
                return response;
            });
            var helper = CreateDropboxHelper(handler);

            // Act
            await helper.ProcessFileAsync(CreateHttpClient(handler), path, @"foo/qux/", DateTime.UtcNow);

            // Assert
            string expectedFile = Path.Combine(helper.Environment.RepositoryPath, "test.txt");
            Assert.Equal(content, File.ReadAllText(expectedFile));
            Assert.Equal(3, counter);
        }
예제 #2
0
        private static HttpClient CreateHttpClient(TestMessageHandler handler = null)
        {
            if (handler == null)
            {
                handler = new TestMessageHandler(HttpStatusCode.OK);
            }

            return(new HttpClient(handler)
            {
                BaseAddress = new Uri("http://site-does-not-exist.microsoft.com")
            });
        }
예제 #3
0
        public async Task ProcessFileAsyncWritesFileToSpecifiedPath()
        {
            // Arrange
            string content = "Hello world!";
            string path = "foo/bar/test.txt";
            var handler = new TestMessageHandler(content);
            var helper = CreateDropboxHelper(handler);

            // Act
            await helper.ProcessFileAsync(CreateHttpClient(handler), path, @"foo/bar/", DateTime.UtcNow);

            // Assert
            string expectedFile = Path.Combine(helper.Environment.RepositoryPath, "test.txt");
            Assert.Equal(content, File.ReadAllText(expectedFile));
        }
예제 #4
0
        public async Task ProcessFileAsyncWritesFileToSpecifiedPath()
        {
            // Arrange
            string content = "Hello world!";
            string path    = "foo/bar/test.txt";
            var    handler = new TestMessageHandler(content);
            var    helper  = CreateDropboxHelper(handler);

            // Act
            await helper.ProcessFileAsync(CreateHttpClient(handler), path, @"foo/bar/", DateTime.UtcNow);

            // Assert
            string expectedFile = Path.Combine(helper.Environment.RepositoryPath, "test.txt");

            Assert.Equal(content, File.ReadAllText(expectedFile));
        }
예제 #5
0
        public async Task ProcessFileAsyncThrowsIfFailedAfterThreeRetries()
        {
            // Arrange
            string path    = "foo/qux/test.txt";
            int    counter = 0;
            var    handler = new TestMessageHandler(_ =>
            {
                counter++;
                return(new HttpResponseMessage(HttpStatusCode.ExpectationFailed));
            });

            var helper = CreateDropboxHelper(handler);

            // Act
            await Assert.ThrowsAsync <HttpRequestException>(async() =>
                                                            await helper.ProcessFileAsync(CreateHttpClient(handler), path, @"foo/qux/", DateTime.UtcNow));

            // Assert
            Assert.Equal(3, counter);
        }
예제 #6
0
        public async Task ProcessFileAsyncThrowsIfFailedAfterThreeRetries()
        {
            // Arrange
            string path = "foo/qux/test.txt";
            int counter = 0;
            var handler = new TestMessageHandler(_ => 
            {
                counter++;
                return new HttpResponseMessage(HttpStatusCode.ExpectationFailed);
            });
          
            var helper = CreateDropboxHelper(handler);

            // Act
            await ExceptionAssert.ThrowsAsync<HttpRequestException>(async() =>
                await helper.ProcessFileAsync(CreateHttpClient(handler), path, @"foo/qux/", DateTime.UtcNow));

            // Assert
            Assert.Equal(3, counter);
        }
예제 #7
0
        public async Task GetDeltasPopulatesEntriesNode()
        {
            // Arrange
            const string DeltaPayload = @"{""reset"": true, ""cursor"": ""AAGWKUylpghsuMRcKQSdHSpvUW3uPVcIyGINt30oO36wDebzBoqtFFaiqzNCWV568-U_uZwdM1QGyzxYw3GxJRsCWv0G3BlOUiguFrttRsbpmA"", ""has_more"": false, ""entries"": [[""/foo/bar.txt"", {""revision"": 1, ""rev"": ""11357a5a5"", ""thumb_exists"": false, ""bytes"": 123641, ""modified"": ""Thu, 22 Aug 2013 22:50:24 +0000"", ""client_mtime"": ""Thu, 22 Aug 2013 22:50:24 +0000"", ""path"": ""/foo/bar.txt"", ""is_dir"": false, ""icon"": ""page_white"", ""root"": ""dropbox"", ""mime_type"": ""application/epub+zip"", ""size"": ""120.7 KB""}]]}";
            var          dropboxInfo  = new DropboxDeployInfo
            {
                Token = "Some-token",
                Path  = "/foo"
            };
            var handler = new TestMessageHandler(DeltaPayload, isJson: true);
            var helper  = CreateDropboxHelper(handler);

            // Act
            await helper.UpdateDropboxDeployInfo(dropboxInfo);

            // Assert
            Assert.Null(dropboxInfo.OldCursor);
            Assert.Equal("AAGWKUylpghsuMRcKQSdHSpvUW3uPVcIyGINt30oO36wDebzBoqtFFaiqzNCWV568-U_uZwdM1QGyzxYw3GxJRsCWv0G3BlOUiguFrttRsbpmA", dropboxInfo.NewCursor);
            Assert.Equal(1, dropboxInfo.Deltas.Count);
            Assert.Equal("/foo/bar.txt", dropboxInfo.Deltas[0].Path);
        }
예제 #8
0
        public async Task GetDeltasPopulatesEntriesNode()
        {
            // Arrange
            const string DeltaPayload = @"{""cursor"": ""AAGWKUylpghsuMRcKQSdHSpvUW3uPVcIyGINt30oO36wDebzBoqtFFaiqzNCWV568-U_uZwdM1QGyzxYw3GxJRsCWv0G3BlOUiguFrttRsbpmA"", ""has_more"": false, ""entries"": [{"".tag"": ""file"", ""path_display"": ""/foo/bar.txt"", ""server_modified"": ""2017-08-30T18:50:02Z""}]}";
            var          dropboxInfo  = new DropboxDeployInfo
            {
                Token = "Some-token",
                Path  = "/foo"
            };
            var handler = new TestMessageHandler(DeltaPayload, isJson: true);
            var helper  = CreateDropboxHelper(handler);

            // Act
            await helper.UpdateDropboxDeployInfo(dropboxInfo);

            // Assert
            Assert.Null(dropboxInfo.OldCursor);
            Assert.Equal("AAGWKUylpghsuMRcKQSdHSpvUW3uPVcIyGINt30oO36wDebzBoqtFFaiqzNCWV568-U_uZwdM1QGyzxYw3GxJRsCWv0G3BlOUiguFrttRsbpmA", dropboxInfo.NewCursor);
            Assert.Equal(1, dropboxInfo.Deltas.Count);
            Assert.Equal("/foo/bar.txt", dropboxInfo.Deltas[0].Path);
        }
예제 #9
0
        private static HttpClient CreateHttpClient(TestMessageHandler handler = null)
        {
            if (handler == null)
            {
                handler = new TestMessageHandler(HttpStatusCode.OK);
            }

            return new HttpClient(handler) { BaseAddress = new Uri("http://site-does-not-exist.microsoft.com") };
        }
예제 #10
0
        private DropboxHelper CreateDropboxHelper(TestMessageHandler handler = null)
        {
            string repositoryPath = Path.Combine(_testRoot, Path.GetRandomFileName());
            EnsureDirectory(repositoryPath);
            var env = new Mock<IEnvironment>();
            env.SetupGet(e => e.RepositoryPath).Returns(repositoryPath);
            var helper = new Mock<DropboxHelper>(Mock.Of<ITracer>(), Mock.Of<IDeploymentStatusManager>(), Mock.Of<IDeploymentSettingsManager>(), env.Object);
            helper.CallBase = true;
            helper.Object.Logger = Mock.Of<ILogger>();

            if (handler != null)
            {
                helper.Setup(h => h.CreateDropboxHttpClient(It.IsAny<DropboxDeployInfo>(), It.IsAny<DropboxEntryInfo>()))
                      .Returns(CreateHttpClient(handler));

                helper.Setup(h => h.CreateDropboxV2HttpClient(It.IsAny<string>(), It.IsAny<string>()))
                      .Returns(CreateHttpClient(handler));

                DropboxHelper.RetryWaitToAvoidRateLimit = TimeSpan.FromSeconds(1);
            }
            return helper.Object;
        }
예제 #11
0
        public async Task GetDeltasPopulatesEntriesNodeWhenHasMoreIsSet()
        {
            // Arrange
            const string DeltaPayload1 = @"{""reset"": true, ""cursor"": ""cursor1"", ""has_more"": true, ""entries"": [[""/foo/bar.txt"", {""revision"": 1, ""rev"": ""11357a5a5"", ""bytes"": 123641, ""modified"": ""Thu, 22 Aug 2013 22:50:24 +0000"", ""client_mtime"": ""Thu, 22 Aug 2013 22:50:24 +0000"", ""path"": ""/foo/bar.txt"", ""is_dir"": false}]]}";
            const string DeltaPayload2 = @"{""reset"": false, ""cursor"": ""cursor2"", ""has_more"": false, ""entries"": [[""/foo/bar.txt"", {""revision"": 1, ""rev"": ""11357a5a5"", ""bytes"": 123641, ""modified"": ""Thu, 22 Aug 2013 22:50:24 +0000"", ""client_mtime"": ""Thu, 22 Aug 2013 22:50:24 +0000"", ""path"": ""/foo/qux.txt"", ""is_dir"": false}]]}";
            var dropboxInfo = new DropboxDeployInfo
            {
                Token = "Some-token",
                Path = "/foo"
            };
            int i = 0;
            var handler = new TestMessageHandler(_ => 
            {
                var content = new StringContent(i++ == 0 ? DeltaPayload1 : DeltaPayload2, Encoding.UTF8, "application/json");
                return new HttpResponseMessage { Content = content };
            });
            var helper = CreateDropboxHelper(handler);

            // Act
            await helper.UpdateDropboxDeployInfo(dropboxInfo);

            // Assert
            Assert.Equal("cursor2", dropboxInfo.NewCursor);
            Assert.Equal(2, dropboxInfo.Deltas.Count);
            Assert.Equal("/foo/bar.txt", dropboxInfo.Deltas[0].Path);
            Assert.Equal("/foo/qux.txt", dropboxInfo.Deltas[1].Path);
        }
예제 #12
0
        public async Task GetDeltasPopulatesEntriesNode()
        {
            // Arrange
            const string DeltaPayload = @"{""reset"": true, ""cursor"": ""AAGWKUylpghsuMRcKQSdHSpvUW3uPVcIyGINt30oO36wDebzBoqtFFaiqzNCWV568-U_uZwdM1QGyzxYw3GxJRsCWv0G3BlOUiguFrttRsbpmA"", ""has_more"": false, ""entries"": [[""/foo/bar.txt"", {""revision"": 1, ""rev"": ""11357a5a5"", ""thumb_exists"": false, ""bytes"": 123641, ""modified"": ""Thu, 22 Aug 2013 22:50:24 +0000"", ""client_mtime"": ""Thu, 22 Aug 2013 22:50:24 +0000"", ""path"": ""/foo/bar.txt"", ""is_dir"": false, ""icon"": ""page_white"", ""root"": ""dropbox"", ""mime_type"": ""application/epub+zip"", ""size"": ""120.7 KB""}]]}";
            var dropboxInfo = new DropboxDeployInfo
            {
                Token = "Some-token",
                Path = "/foo"
            };
            var handler = new TestMessageHandler(DeltaPayload, isJson: true);
            var helper = CreateDropboxHelper(handler);

            // Act
            await helper.UpdateDropboxDeployInfo(dropboxInfo);

            // Assert
            Assert.Null(dropboxInfo.OldCursor);
            Assert.Equal("AAGWKUylpghsuMRcKQSdHSpvUW3uPVcIyGINt30oO36wDebzBoqtFFaiqzNCWV568-U_uZwdM1QGyzxYw3GxJRsCWv0G3BlOUiguFrttRsbpmA", dropboxInfo.NewCursor);
            Assert.Equal(1, dropboxInfo.Deltas.Count);
            Assert.Equal("/foo/bar.txt", dropboxInfo.Deltas[0].Path);
        }
예제 #13
0
        private DropboxHelper CreateDropboxHelper(TestMessageHandler handler = null)
        {
            string repositoryPath = Path.Combine(_testRoot, Path.GetRandomFileName());
            EnsureDirectory(repositoryPath);
            var env = new Mock<IEnvironment>();
            env.SetupGet(e => e.RepositoryPath).Returns(repositoryPath);
            var helper = new Mock<DropboxHelper>(Mock.Of<ITracer>(), Mock.Of<IDeploymentStatusManager>(), Mock.Of<IDeploymentSettingsManager>(), env.Object);
            helper.CallBase = true;
            helper.Object.Logger = Mock.Of<ILogger>();

            if (handler != null)
            {
                helper.Setup(h => h.CreateDropboxHttpClient())
                      .Returns(() => new HttpClient(handler) { BaseAddress = new Uri("http://site-does-not-exist.microsoft.com") });

                DropboxHelper.RetryWaitToAvoidRateLimit = TimeSpan.FromSeconds(1);
            }
            return helper.Object;
        }