Пример #1
0
        public void CreateRenameDirectory()
        {
            using (HttpClient client = ApiHttpClient.Create())
                using (TestSiteContainer container = new TestSiteContainer(client))
                {
                    JObject site        = Sites.GetSite(client, container.SiteName);
                    JObject target      = null;
                    string  updatedName = "updated_test_folder_name";

                    try {
                        var webFile      = CreateWebFile(client, site, TEST_FILE_NAME, "directory");
                        var physicalPath = Path.Combine(Environment.ExpandEnvironmentVariables(site.Value <string>("physical_path")), updatedName);

                        if (Directory.Exists(physicalPath))
                        {
                            Directory.Delete(physicalPath);
                        }

                        target = RenameSitesFile(client, site, webFile, updatedName);
                    }
                    finally {
                        Assert.True(client.Delete(Utils.Self(target.Value <JObject>("file_info"))));
                    }
                }
        }
Пример #2
0
        public void TruncateOnCompleteRange()
        {
            var size         = 1024 * 1024 * 5;
            var truncateSize = size / 2;

            using (HttpClient client = ApiHttpClient.Create())
                using (TestSiteContainer container = new TestSiteContainer(client))
                {
                    JObject site     = Sites.GetSite(client, container.SiteName);
                    var     webFile  = CreateWebFile(client, site, TEST_FILE_NAME);
                    var     fileInfo = Utils.FollowLink(client, webFile.Value <JObject>("file_info"), "self");

                    try {
                        Assert.True(MockUploadFile(client, fileInfo, size).Result);

                        fileInfo = Utils.FollowLink(client, fileInfo, "self");

                        Assert.True(fileInfo.Value <int>("size") == size);

                        Assert.True(MockUploadFile(client, fileInfo, truncateSize).Result);

                        fileInfo = Utils.FollowLink(client, fileInfo, "self");

                        Assert.True(fileInfo.Value <int>("size") == truncateSize);
                    }
                    finally {
                        if (webFile != null)
                        {
                            Assert.True(client.Delete(Utils.Self(webFile.Value <JObject>("file_info"))));
                        }
                    }
                }
        }
Пример #3
0
        public void CreateEditDeleteFile()
        {
            using (HttpClient client = ApiHttpClient.Create())
                using (TestSiteContainer container = new TestSiteContainer(client))
                {
                    JObject site = Sites.GetSite(client, container.SiteName);

                    // Create web file
                    var webFile = CreateWebFile(client, site, TEST_FILE_NAME);

                    Assert.True(webFile != null);

                    try {
                        //
                        // Get physical file info
                        var fileInfo = Utils.FollowLink(client, webFile.Value <JObject>("file_info"), "self");

                        // Update content of file
                        var testContent = "Microsoft.IIS.Administration.Test.Files";
                        var res         = client.PutAsync(Utils.GetLink(fileInfo, "content"), new StringContent(testContent)).Result;

                        Assert.True(res.StatusCode == HttpStatusCode.OK);

                        // Get updated content of file
                        string result = null;
                        Assert.True(client.Get(Utils.GetLink(fileInfo, "content"), out result));
                        Assert.True(result == testContent);

                        var downloadsHref = Utils.GetLink(fileInfo, "downloads");

                        var dl = new {
                            file = fileInfo
                        };

                        // Create download link for file
                        res = client.PostAsync(downloadsHref, new StringContent(JsonConvert.SerializeObject(dl), Encoding.UTF8, "application/json")).Result;
                        Assert.True(res.StatusCode == HttpStatusCode.Created);

                        IEnumerable <string> locationHeader;
                        Assert.True(res.Headers.TryGetValues("location", out locationHeader));
                        var location = locationHeader.First();

                        // Download file
                        Assert.True(client.Get($"{Configuration.TEST_SERVER_URL}{location}", out result));
                        Assert.True(result == testContent);

                        // Update file with empty content
                        res = client.PutAsync(Utils.GetLink(fileInfo, "content"), new ByteArrayContent(new byte[] { })).Result;

                        Assert.True(res.StatusCode == HttpStatusCode.OK);

                        // Assert file truncated
                        res = client.GetAsync(Utils.GetLink(fileInfo, "content")).Result;
                        Assert.True(res.Content.ReadAsByteArrayAsync().Result.Length == 0);
                    }
                    finally {
                        Assert.True(client.Delete(Utils.Self(webFile.Value <JObject>("file_info"))));
                    }
                }
        }
Пример #4
0
        public void UploadMultipleFiles()
        {
            var mockFileNames = new List <string>();

            for (var i = 0; i < 15; i++)
            {
                mockFileNames.Add($"{TEST_FILE_NAME}{i}");
            }


            using (HttpClient client = ApiHttpClient.Create())
                using (TestSiteContainer container = new TestSiteContainer(client))
                {
                    JObject site = Sites.GetSite(client, container.SiteName);

                    var webFiles  = new List <JObject>();
                    var fileInfos = new List <JObject>();

                    try {
                        foreach (var name in mockFileNames)
                        {
                            webFiles.Add(CreateWebFile(client, site, name));
                        }
                        foreach (var webFile in webFiles)
                        {
                            fileInfos.Add(Utils.FollowLink(client, webFile.Value <JObject>("file_info"), "self"));
                        }

                        var uploads = new List <Task <bool> >();
                        foreach (var fileInfo in fileInfos)
                        {
                            uploads.Add(MockUploadFile(client, fileInfo, 1024 * 1024 * 5));
                        }

                        Task.WaitAll(uploads.ToArray());

                        foreach (var upload in uploads)
                        {
                            Assert.True(upload.Result);
                        }
                    }
                    finally {
                        foreach (JObject webFile in webFiles)
                        {
                            if (webFile != null)
                            {
                                Assert.True(client.Delete(Utils.Self(webFile.Value <JObject>("file_info"))));
                            }
                        }
                    }
                }
        }
Пример #5
0
        public void RangeUploadDownload()
        {
            using (HttpClient client = ApiHttpClient.Create())
                using (TestSiteContainer container = new TestSiteContainer(client))
                {
                    JObject site = Sites.GetSite(client, container.SiteName);

                    // Create web file
                    var webFile = CreateWebFile(client, site, TEST_FILE_NAME);

                    try {
                        //
                        // Get physical file info
                        var fileInfo = Utils.FollowLink(client, webFile.Value <JObject>("file_info"), "self");

                        var chunkSize     = 1024 * 1024;
                        var totalFileSize = 1024 * 1024 * 10;
                        HttpRequestMessage  req;
                        HttpResponseMessage res;

                        for (var i = 0; i < totalFileSize; i += chunkSize)
                        {
                            req = new HttpRequestMessage(HttpMethod.Put, Utils.GetLink(fileInfo, "content"));

                            var currentChunkSize = totalFileSize - i < chunkSize ? totalFileSize - i : chunkSize;
                            var slice            = GetFileSlice(i, currentChunkSize);

                            req.Content = new ByteArrayContent(slice);

                            req.Content.Headers.Add("Content-Range", $"bytes {i}-{i + currentChunkSize - 1}/{totalFileSize}");

                            res = client.SendAsync(req).Result;

                            Assert.True(Globals.Success(res));
                        }

                        req = new HttpRequestMessage(HttpMethod.Get, Utils.GetLink(fileInfo, "content"));

                        res = client.SendAsync(req).Result;

                        Assert.True(Globals.Success(res));

                        var resultBytes = res.Content.ReadAsByteArrayAsync().Result;

                        Assert.True(resultBytes.SequenceEqual(GetFileSlice(0, totalFileSize)));

                        var download = new byte[totalFileSize];

                        //
                        // Range download
                        for (var i = 0; i < totalFileSize; i += chunkSize)
                        {
                            req = new HttpRequestMessage(HttpMethod.Get, Utils.GetLink(fileInfo, "content"));

                            var currentChunkSize = totalFileSize - i < chunkSize ? totalFileSize - i : chunkSize;

                            req.Headers.Add("Range", $"bytes={i}-{i + currentChunkSize - 1}");

                            res = client.SendAsync(req).Result;

                            Assert.True(Globals.Success(res));

                            resultBytes = res.Content.ReadAsByteArrayAsync().Result;
                            resultBytes.CopyTo(download, i);
                        }

                        Assert.True(download.SequenceEqual(GetFileSlice(0, totalFileSize)));
                    }
                    finally {
                        Assert.True(client.Delete(Utils.Self(webFile.Value <JObject>("file_info"))));
                    }
                }
        }
Пример #6
0
        public void CopyFile()
        {
            string  copyName    = "TEST_FILE_NAME_COPY.txt";
            string  testContent = "Test content for copying files.";
            JObject copyInfo    = null;

            using (HttpClient client = ApiHttpClient.Create())
                using (TestSiteContainer container = new TestSiteContainer(client))
                {
                    JObject site = Sites.GetSite(client, container.SiteName);

                    var webFile = CreateWebFile(client, site, TEST_FILE_NAME);

                    var physicalPath = Environment.ExpandEnvironmentVariables(webFile["file_info"].Value <string>("physical_path"));
                    File.WriteAllText(physicalPath, testContent);

                    try
                    {
                        var fileInfo = Utils.FollowLink(client, webFile.Value <JObject>("file_info"), "self");
                        var parent   = fileInfo.Value <JObject>("parent");

                        var copy = new
                        {
                            name   = copyName,
                            parent = parent,
                            file   = fileInfo
                        };

                        copyInfo = client.Post(Utils.GetLink(fileInfo, "copy"), copy);

                        Assert.NotNull(copyInfo);

                        //
                        // Wait for copy to finish
                        HttpResponseMessage res = null;
                        do
                        {
                            res = client.GetAsync(Utils.Self(copyInfo)).Result;
                        } while (res.StatusCode == HttpStatusCode.OK);

                        var copyParent       = new DirectoryInfo(physicalPath).Parent.FullName;
                        var copyPhysicalPath = Environment.ExpandEnvironmentVariables(copyInfo["file"].Value <string>("physical_path"));

                        Assert.True(copyPhysicalPath.Equals(Path.Combine(copyParent, copyName), StringComparison.OrdinalIgnoreCase));

                        var copyContent = File.ReadAllText(copyPhysicalPath);

                        Assert.Equal(copyContent, testContent);
                    }
                    finally
                    {
                        if (webFile != null && webFile["file_info"] != null)
                        {
                            Assert.True(client.Delete(Utils.Self(webFile.Value <JObject>("file_info"))));
                        }
                        if (copyInfo != null)
                        {
                            Assert.True(client.Delete(Utils.Self(copyInfo.Value <JObject>("file"))));
                        }
                    }
                }
        }