async Task TestDeleteDirectory(CancellationToken cancellationToken)
        {
            //try to delete non-existent
            var TestDir = new ConfigurationFile
            {
                Path = "/TestDeleteDir"
            };

            await configurationClient.DeleteEmptyDirectory(TestDir, cancellationToken).ConfigureAwait(false);

            //try to delete non-empty
            var file = await configurationClient.Write(new ConfigurationFile
            {
                Content = Encoding.UTF8.GetBytes("Hello world!"),
                Path    = TestDir.Path + "/test.txt"
            }, cancellationToken).ConfigureAwait(false);

            Assert.IsTrue(FileExists(file));

            await Assert.ThrowsExceptionAsync <ConflictException>(() => configurationClient.DeleteEmptyDirectory(TestDir, cancellationToken)).ConfigureAwait(false);

            file.Content = null;
            await configurationClient.Write(file, cancellationToken).ConfigureAwait(false);

            await configurationClient.DeleteEmptyDirectory(TestDir, cancellationToken).ConfigureAwait(false);
        }
Exemple #2
0
        async Task TestDeleteDirectory(CancellationToken cancellationToken)
        {
            //try to delete non-existent
            var TestDir = new ConfigurationFileRequest
            {
                Path = "/TestDeleteDir"
            };

            await configurationClient.DeleteEmptyDirectory(TestDir, cancellationToken).ConfigureAwait(false);

            //try to delete non-empty
            const string TestString = "Hello world!";

            using var uploadMs = new MemoryStream(Encoding.UTF8.GetBytes(TestString));
            var file = await configurationClient.Write(new ConfigurationFileRequest
            {
                Path = TestDir.Path + "/test.txt"
            }, uploadMs, cancellationToken).ConfigureAwait(false);

            Assert.IsTrue(FileExists(file));
            Assert.IsNull(file.LastReadHash);

            var updatedFileTuple = await configurationClient.Read(file, cancellationToken).ConfigureAwait(false);

            var updatedFile = updatedFileTuple.Item1;

            Assert.IsNotNull(updatedFile.LastReadHash);
            using (var downloadMemoryStream = new MemoryStream())
            {
                using (var downloadStream = updatedFileTuple.Item2)
                {
                    var requestStream = downloadStream as CachedResponseStream;
                    Assert.IsNotNull(requestStream);
                    var response = (HttpResponseMessage)requestStream.GetType().GetField("response", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(requestStream);
                    Assert.AreEqual(response.Content.Headers.ContentType.MediaType, MediaTypeNames.Application.Octet);
                    await downloadStream.CopyToAsync(downloadMemoryStream);
                }
                Assert.AreEqual(TestString, Encoding.UTF8.GetString(downloadMemoryStream.ToArray()).Trim());
            }

            await ApiAssert.ThrowsException <ConflictException>(() => configurationClient.DeleteEmptyDirectory(TestDir, cancellationToken), ErrorCode.ConfigurationDirectoryNotEmpty).ConfigureAwait(false);

            file.FileTicket = null;
            await configurationClient.Write(new ConfigurationFileRequest
            {
                Path         = updatedFile.Path,
                LastReadHash = updatedFile.LastReadHash
            }, null, cancellationToken).ConfigureAwait(false);

            Assert.IsFalse(FileExists(file));

            await configurationClient.DeleteEmptyDirectory(TestDir, cancellationToken).ConfigureAwait(false);

            var tmp  = (TestDir.Path?.StartsWith('/') ?? false) ? '.' + TestDir.Path : TestDir.Path;
            var path = Path.Combine(instance.Path, "Configuration", tmp);

            Assert.IsFalse(Directory.Exists(path));
        }