private JObject GetCcs() { using (var client = ApiHttpClient.Create()) { JObject webserver = client.Get($"{Configuration.Instance().TEST_SERVER_URL}/api/webserver/"); return(Utils.FollowLink(client, webserver, "central_certificates")); } }
public void WebFileRange() { var physicalPath = Path.Combine(Configuration.TEST_ROOT_PATH, "web_file_range_test"); if (Directory.Exists(physicalPath)) { Directory.Delete(physicalPath, true); Directory.CreateDirectory(physicalPath); } JObject site = null; using (HttpClient client = ApiHttpClient.Create()) { try { var dirs = new List <string>() { "dir1", "dir2", "dir3", "dir4" }; var files = new List <string>() { "file1.txt", "file2.txt", "file3.txt", "file4.txt" }; Sites.EnsureNoSite(client, FILE_TEST_SITE_NAME); site = Sites.CreateSite(client, FILE_TEST_SITE_NAME, Utils.GetAvailablePort(), physicalPath); Assert.NotNull(site); foreach (var dir in dirs) { Directory.CreateDirectory(Path.Combine(physicalPath, dir)); } foreach (var file in files) { File.Create(Path.Combine(physicalPath, file)).Dispose(); } JObject folder = Utils.FollowLink(client, site, "files"); HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, Utils.GetLink(folder, "files")); req.Headers.Add("Range", "files=2-5"); var res = client.SendAsync(req).Result; Assert.True(res.Content.Headers.Contains("Content-Range")); Assert.True(res.Content.Headers.GetValues("Content-Range").First().Equals("2-5/8")); var children = JObject.Parse(res.Content.ReadAsStringAsync().Result)["files"].ToObject <IEnumerable <JObject> >(); Assert.True(children.Count() == 4); } finally { Directory.Delete(physicalPath, true); if (site != null) { client.Delete(Utils.Self(site)); } } } }
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")))); } } }
public void TruncateOnCompleteRange() { var size = 1024 * 1024 * 5; var truncateSize = size / 2; using (HttpClient client = ApiHttpClient.Create()) { JObject site = Sites.GetSite(client, "Default Web Site"); 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")))); } } } }
private JObject RenameSitesFile(HttpClient client, JObject site, JObject file, string newName) { var target = file; var originalName = target.Value <string>("name"); var rootVdir = Utils.FollowLink(client, site, "files"); var files = Utils.FollowLink(client, rootVdir, "files")["files"].ToObject <IEnumerable <JObject> >(); target = files.FirstOrDefault(f => f.Value <string>("name").Equals(originalName, StringComparison.OrdinalIgnoreCase)); Assert.NotNull(target); var targetFileInfo = Utils.FollowLink(client, Utils.FollowLink(client, target, "self").Value <JObject>("file_info"), "self"); var alteredName = newName; targetFileInfo["name"] = alteredName; targetFileInfo = client.Patch(Utils.Self(targetFileInfo), targetFileInfo); Assert.True(targetFileInfo != null); Assert.True(targetFileInfo.Value <string>("name").Equals(alteredName)); files = Utils.FollowLink(client, rootVdir, "files")["files"].ToObject <IEnumerable <JObject> >(); target = files.FirstOrDefault(f => f.Value <string>("name").Equals(alteredName, StringComparison.OrdinalIgnoreCase)); Assert.NotNull(target); return(Utils.FollowLink(client, target, "self")); }
public static JObject GetSite(HttpClient client, string name) { var site = client.Get(SITE_URL)["websites"] .ToObject <IEnumerable <JObject> >() .FirstOrDefault(p => p.Value <string>("name").Equals(name, StringComparison.OrdinalIgnoreCase)); return(site == null ? null : Utils.FollowLink(client, site, "self")); }
public static JObject GetAppPool(HttpClient client, string name) { var pool = client.Get(APP_POOLS_URL)["app_pools"] .ToObject <IEnumerable <JObject> >() .FirstOrDefault(p => p.Value <string>("name").Equals(name, StringComparison.OrdinalIgnoreCase)); return(pool == null ? null : Utils.FollowLink(client, pool, "self")); }
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()) { JObject site = Sites.GetSite(client, "Default Web Site"); 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")))); } } } }
public void CopyDirectory() { string startName = "copy_dir_test"; string destName = "copy_dir_dest"; var physicalPath = Path.Combine(Configuration.TEST_ROOT_PATH, startName); var destPhysicalPath = Path.Combine(Configuration.TEST_ROOT_PATH, destName); CreateTestDirectory(physicalPath); JObject site = null; using (HttpClient client = ApiHttpClient.Create()) { Sites.EnsureNoSite(client, FILE_TEST_SITE_NAME); site = Sites.CreateSite(client, FILE_TEST_SITE_NAME, Utils.GetAvailablePort(), physicalPath); try { var rootDir = Utils.FollowLink(client, site, "files"); var rootDirFileInfo = Utils.FollowLink(client, rootDir.Value <JObject>("file_info"), "self"); object copy = new { name = destName, parent = rootDirFileInfo.Value <JObject>("parent"), file = rootDirFileInfo }; var copyInfo = client.Post(Utils.GetLink(rootDirFileInfo, "copy"), copy); // // Wait for copy to finish HttpResponseMessage res = null; do { res = client.GetAsync(Utils.Self(copyInfo)).Result; } while (res.StatusCode == HttpStatusCode.OK); // Don't add code between copy end and verification so we can make sure files aren't being held Assert.True(VerifyTestDirectory(physicalPath)); Assert.True(VerifyTestDirectory(destPhysicalPath)); } finally { if (site != null) { Sites.DeleteSite(client, Utils.Self(site)); } if (Directory.Exists(physicalPath)) { Directory.Delete(physicalPath, true); } if (Directory.Exists(destPhysicalPath)) { Directory.Delete(destPhysicalPath, true); } } } }
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")))); } } } } }
public static JObject GetFile(HttpClient client, JObject docFeature, string fileName) { JArray files = GetFiles(client, docFeature); JObject file = files.FirstOrDefault(f => f.Value <string>("name").Equals(fileName, StringComparison.OrdinalIgnoreCase)) as JObject; if (file != null) { file = Utils.FollowLink(client, file, "self"); } return(file); }
public void MoveDirectory() { string startName = "move_dir_test"; string destName = "move_dir_dest"; var physicalPath = Path.Combine(Configuration.TEST_ROOT_PATH, startName); var destPhysicalPath = Path.Combine(Configuration.TEST_ROOT_PATH, destName); CreateTestDirectory(physicalPath); JObject site = null; using (HttpClient client = ApiHttpClient.Create()) { Sites.EnsureNoSite(client, FILE_TEST_SITE_NAME); site = Sites.CreateSite(client, FILE_TEST_SITE_NAME, Utils.GetAvailablePort(), physicalPath); try { var rootDir = Utils.FollowLink(client, site, "files"); var rootDirFileInfo = Utils.FollowLink(client, rootDir.Value <JObject>("file_info"), "self"); object move = new { name = destName, parent = rootDirFileInfo.Value <JObject>("parent"), file = rootDirFileInfo }; var moveInfo = client.Post(Utils.GetLink(rootDirFileInfo, "move"), move); // Wait for move to finish HttpResponseMessage res = null; while (res == null || res.StatusCode == HttpStatusCode.OK) { res = client.GetAsync(Utils.Self(moveInfo)).Result; Thread.Sleep(25); } Assert.True(!Directory.Exists(physicalPath)); Assert.True(VerifyTestDirectory(destPhysicalPath)); } finally { if (site != null) { Sites.DeleteSite(client, Utils.Self(site)); } if (Directory.Exists(destPhysicalPath)) { Directory.Delete(destPhysicalPath, true); } } } }
private JObject CreateWebFile(HttpClient client, JObject site, string fileName, string fileType = "file", bool deleteIfExists = true) { var physicalPath = site.Value <string>("physical_path"); physicalPath = Path.Combine(Environment.ExpandEnvironmentVariables(physicalPath), TEST_FILE_NAME); if (deleteIfExists) { if (File.Exists(physicalPath)) { File.Delete(physicalPath); } if (Directory.Exists(physicalPath)) { Directory.Delete(physicalPath); } } var rootDir = Utils.FollowLink(client, site, "files"); var rootDirFileInfo = Utils.FollowLink(client, rootDir.Value <JObject>("file_info"), "self"); object newFile = new { type = fileType, parent = rootDirFileInfo, name = fileName }; // Create web file var file = client.Post($"{Configuration.TEST_SERVER_URL}{FILES_PATH}", newFile); Assert.True(file != null); var siteFiles = Utils.FollowLink(client, rootDir, "files"); file = siteFiles.Value <JArray>("files").ToObject <IEnumerable <JObject> >().FirstOrDefault(f => f.Value <string>("name").Equals(file.Value <string>("name")) ); Assert.True(file != null); return(Utils.FollowLink(client, file, "self")); }
public static JObject GetCompressionFeature(HttpClient client, string siteName, string path) { if (path != null) { if (!path.StartsWith("/")) { throw new ArgumentException("path"); } } string content; if (!client.Get(COMPRESSION_URL + "?scope=" + siteName + path, out content)) { return(null); } var reference = Utils.ToJ(content); return(Utils.FollowLink(client, reference, "self")); }
public void CreatePatchRemoveProvider() { var TEST_PROVIDER_NAME = "Test Provider"; var TEST_PROVIDER_GUID = Guid.NewGuid().ToString("B"); var TEST_AREAS = new string[] { "test_area", "test_area2" }; var PATCH_PROVIDER_NAME = "Patch Provider"; var PATCH_PROVIDER_GUID = Guid.NewGuid().ToString("B"); var PATCH_AREAS = new string[] { "patch_area", "patch_area2" }; using (var client = ApiHttpClient.Create()) { var feature = GetFeature(client, null, null); var providersObj = Utils.FollowLink(client, feature, "providers"); var providers = providersObj.Value <JArray>("providers").ToObject <IEnumerable <JObject> >(); foreach (var p in providers) { if (p.Value <string>("name").Equals(TEST_PROVIDER_NAME, StringComparison.OrdinalIgnoreCase) || p.Value <string>("name").Equals(PATCH_PROVIDER_NAME, StringComparison.OrdinalIgnoreCase)) { Assert.True(client.Delete(Utils.Self(p))); break; } } var testProvider = new { name = TEST_PROVIDER_NAME, guid = TEST_PROVIDER_GUID, areas = TEST_AREAS, request_tracing = feature }; var patchProvider = new { name = PATCH_PROVIDER_NAME, guid = PATCH_PROVIDER_GUID, areas = PATCH_AREAS }; var jProvider = JObject.FromObject(testProvider); var pProvider = JObject.FromObject(patchProvider); string result; Assert.True(client.Post(Utils.GetLink(feature, "providers"), JsonConvert.SerializeObject(testProvider), out result)); JObject newProvider = null; try { newProvider = Utils.ToJ(result); CompareProviders(jProvider, newProvider); Assert.True(client.Patch(Utils.Self(newProvider), JsonConvert.SerializeObject(patchProvider), out result)); newProvider = Utils.ToJ(result); CompareProviders(pProvider, newProvider); } finally { Assert.True(client.Delete(Utils.Self(newProvider))); } } }
public void RangeUploadDownload() { using (HttpClient client = ApiHttpClient.Create()) { JObject site = Sites.GetSite(client, "Default Web Site"); // 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")))); } } }
public void CreatePatchRemoveRule() { var TEST_RULE_PATH = "test_rule*.path"; var TEST_RULE_STATUS_CODES = new string[] { "101", "244-245", "280-301", "340" }; var TEST_RULE_MIN_TIME = 100; var TEST_RULE_EVENT_SEVERITY = "error"; var TEST_RULE_PROVIDER_NAME = "ASP"; var TEST_RULE_ALLOWED_AREAS = new Dictionary <string, bool>() { }; var PATCH_PATH = "test_patch*.path"; var PATCH_RULE_STATUS_CODES = new string[] { "104-181", "333" }; var PATCH_RULE_MIN_TIME = 103; var PATCH_RULE_EVENT_SEVERITY = "criticalerror"; var PATCH_RULE_PROVIDER_NAME = "WWW Server"; var PATCH_RULE_ALLOWED_AREAS = new Dictionary <string, bool>() { { "Security", true }, { "Compression", true }, { "Module", true } }; using (var client = ApiHttpClient.Create()) { var feature = GetFeature(client, null, null); var rulesObj = Utils.FollowLink(client, feature, "rules"); var rules = rulesObj.Value <JArray>("rules").ToObject <IEnumerable <JObject> >(); var providersObj = Utils.FollowLink(client, feature, "providers"); var providers = providersObj.Value <JArray>("providers").ToObject <IEnumerable <JObject> >(); // Ensure rule with test path doesn't already exist foreach (var r in rules) { if (r.Value <string>("path").Equals(TEST_RULE_PATH, StringComparison.OrdinalIgnoreCase) || r.Value <string>("path").Equals(PATCH_PATH, StringComparison.OrdinalIgnoreCase)) { Assert.True(client.Delete(Utils.Self(r))); break; } } var testRule = new { path = TEST_RULE_PATH, status_codes = TEST_RULE_STATUS_CODES, min_request_execution_time = TEST_RULE_MIN_TIME, event_severity = TEST_RULE_EVENT_SEVERITY, traces = new [] { new { allowed_areas = TEST_RULE_ALLOWED_AREAS, provider = providers.FirstOrDefault(p => p.Value <string>("name").Equals(TEST_RULE_PROVIDER_NAME)), verbosity = "verbose" } }, request_tracing = feature }; var patchRule = new { path = PATCH_PATH, status_codes = PATCH_RULE_STATUS_CODES, min_request_execution_time = PATCH_RULE_MIN_TIME, event_severity = PATCH_RULE_EVENT_SEVERITY, traces = new[] { new { allowed_areas = PATCH_RULE_ALLOWED_AREAS, provider = providers.FirstOrDefault(p => p.Value <string>("name").Equals(PATCH_RULE_PROVIDER_NAME)), verbosity = "verbose" } }, }; var jRule = JObject.FromObject(testRule); var pRule = JObject.FromObject(patchRule); string result; Assert.True(client.Post(Utils.GetLink(feature, "rules"), JsonConvert.SerializeObject(testRule), out result)); JObject newRule = null; try { newRule = Utils.ToJ(result); CompareRules(jRule, newRule); Assert.True(client.Patch(Utils.Self(newRule), JsonConvert.SerializeObject(patchRule), out result)); newRule = Utils.ToJ(result); CompareRules(pRule, newRule); } finally { Assert.True(client.Delete(Utils.Self(newRule))); } } }
public static JArray GetFiles(HttpClient client, JObject docFeature) { docFeature = Utils.FollowLink(client, docFeature, "files"); return(docFeature.Value <JArray>("files")); }
private JObject GetHandler(HttpClient client, JObject feature, string handlerName) { var handlers = Utils.FollowLink(client, feature, "entries")["entries"].ToObject <IEnumerable <JObject> >(); return(handlers.FirstOrDefault(h => h.Value <string>("name").Equals(handlerName, StringComparison.OrdinalIgnoreCase))); }