public void TestInvalidGetExceptions([ValueSource(nameof(protocols))] string protocol, [Values(true, false)] bool async) { var request = new WebRequest($"{protocol}://{invalid_get_url}") { Method = HttpMethod.Get, AllowInsecureRequests = true }; Exception finishedException = null; request.Failed += exception => finishedException = exception; if (async) { Assert.ThrowsAsync <HttpRequestException>(request.PerformAsync); } else { Assert.Throws <HttpRequestException>(request.Perform); } Assert.IsTrue(request.Completed); Assert.IsTrue(request.Aborted); Assert.IsTrue(request.ResponseString == null); Assert.IsNotNull(finishedException); }
public void TestGetBinaryData([Values(true, false)] bool async, [Values(true, false)] bool chunked) { const int bytes_count = 65536; const int chunk_size = 1024; string endpoint = chunked ? "stream-bytes" : "bytes"; WebRequest request = new WebRequest($"{default_protocol}://{host}/{endpoint}/{bytes_count}") { Method = HttpMethod.Get, AllowInsecureRequests = true, }; if (chunked) { request.AddParameter("chunk_size", chunk_size.ToString()); } if (async) { Assert.DoesNotThrowAsync(request.PerformAsync); } else { Assert.DoesNotThrow(request.Perform); } Assert.IsTrue(request.Completed); Assert.IsFalse(request.Aborted); Assert.AreEqual(bytes_count, request.ResponseStream.Length); }
public void TestBadStatusCode([Values(true, false)] bool async) { var request = new WebRequest($"{default_protocol}://{host}/hidden-basic-auth/user/passwd") { AllowInsecureRequests = true, }; bool hasThrown = false; request.Failed += exception => hasThrown = exception != null; if (async) { Assert.ThrowsAsync <WebException>(request.PerformAsync); } else { Assert.Throws <WebException>(request.Perform); } Assert.IsTrue(request.Completed); Assert.IsTrue(request.Aborted); Assert.IsEmpty(request.ResponseString); Assert.IsTrue(hasThrown); }
public void TestInvalidGetExceptions(string protocol, bool async) { var request = new WebRequest($"{protocol}://{invalid_get_url}") { Method = HttpMethod.GET }; Exception finishedException = null; request.Failed += exception => finishedException = exception; if (async) { Assert.ThrowsAsync <HttpRequestException>(request.PerformAsync); } else { Assert.Throws <HttpRequestException>(request.Perform); } Assert.IsTrue(request.Completed); Assert.IsTrue(request.Aborted); Assert.IsTrue(request.ResponseString == null); Assert.IsNotNull(finishedException); }
public void TestGetBinaryData(bool async, bool chunked) { const int bytes_count = 65536; const int chunk_size = 1024; string endpoint = chunked ? "stream-bytes" : "bytes"; WebRequest request = new WebRequest($"http://httpbin.org/{endpoint}/{bytes_count}") { Method = HttpMethod.GET }; if (chunked) { request.AddParameter("chunk_size", chunk_size.ToString()); } if (async) { Assert.DoesNotThrowAsync(request.PerformAsync); } else { Assert.DoesNotThrow(request.Perform); } Assert.IsTrue(request.Completed); Assert.IsFalse(request.Aborted); Assert.AreEqual(bytes_count, request.ResponseStream.Length); }
private static void uploadBuild(string version) { if (!canGitHub || string.IsNullOrEmpty(CodeSigningCertificate)) { return; } write("Publishing to GitHub..."); var req = new JsonWebRequest <GitHubRelease>($"{GitHubApiEndpoint}") { Method = HttpMethod.Post, }; GitHubRelease targetRelease = getLastGithubRelease(); if (targetRelease == null || targetRelease.TagName != version) { write($"- Creating release {version}...", ConsoleColor.Yellow); req.AddRaw(JsonConvert.SerializeObject(new GitHubRelease { Name = version, Draft = true, })); req.AuthenticatedBlockingPerform(); targetRelease = req.ResponseObject; } else { write($"- Adding to existing release {version}...", ConsoleColor.Yellow); } var assetUploadUrl = targetRelease.UploadUrl.Replace("{?name,label}", "?name={0}"); foreach (var a in Directory.GetFiles(releases_folder).Reverse()) //reverse to upload RELEASES first. { if (Path.GetFileName(a).StartsWith('.')) { continue; } write($"- Adding asset {a}...", ConsoleColor.Yellow); var upload = new WebRequest(assetUploadUrl, Path.GetFileName(a)) { Method = HttpMethod.Post, Timeout = 240000, ContentType = "application/octet-stream", }; upload.AddRaw(File.ReadAllBytes(a)); upload.AuthenticatedBlockingPerform(); } openGitHubReleasePage(); }
public static WorkingBeatmap GetBeatmap(int beatmapId, bool verbose = false, bool forceDownload = true, IReporter reporter = null) { string fileLocation = Path.Combine(AppSettings.BEATMAPS_PATH, beatmapId.ToString()) + ".osu"; if ((forceDownload || !File.Exists(fileLocation)) && AppSettings.ALLOW_DOWNLOAD) { Stream stream; if (verbose) { reporter?.Verbose($"Downloading {beatmapId}."); } stream = GetBeatmapByBid(beatmapId); if (stream == null) { var req = new WebRequest(string.Format(AppSettings.DOWNLOAD_PATH, beatmapId)) { AllowInsecureRequests = true, }; req.Failed += _ => { if (verbose) { reporter?.Error($"Failed to download {beatmapId}."); } }; req.Finished += () => { if (verbose) { reporter?.Verbose($"{beatmapId} successfully downloaded."); } }; req.Perform(); RedisHelper.HSet("beatmap", beatmapId.ToString(), req.GetResponseData()); stream = req.ResponseStream; } if (AppSettings.SAVE_DOWNLOADED) { using (var fileStream = File.Create(fileLocation)) { stream.CopyTo(fileStream); stream.Seek(0, SeekOrigin.Begin); } } return(new LoaderWorkingBeatmap(stream)); } return(!File.Exists(fileLocation) ? null : new LoaderWorkingBeatmap(fileLocation)); }
private static void uploadBuild(string version) { if (string.IsNullOrEmpty(GitHubAccessToken) || string.IsNullOrEmpty(codeSigningCertPath)) { return; } write("Publishing to GitHub..."); write($"- Creating release {version}...", ConsoleColor.Yellow); var req = new JsonWebRequest <GitHubRelease>($"{GitHubApiEndpoint}") { Method = HttpMethod.POST, }; req.AddRaw(JsonConvert.SerializeObject(new GitHubRelease { Name = version, Draft = true, PreRelease = true })); req.AuthenticatedBlockingPerform(); var assetUploadUrl = req.ResponseObject.UploadUrl.Replace("{?name,label}", "?name={0}"); foreach (var a in Directory.GetFiles(ReleasesFolder).Reverse()) //reverse to upload RELEASES first. { write($"- Adding asset {a}...", ConsoleColor.Yellow); var upload = new WebRequest(assetUploadUrl, Path.GetFileName(a)) { Method = HttpMethod.POST, Timeout = 240000, ContentType = "application/octet-stream", }; upload.AddRaw(File.ReadAllBytes(a)); upload.AuthenticatedBlockingPerform(); } openGitHubReleasePage(); }
public void TestFailTimeout() { var request = new WebRequest("https://httpbin.org/delay/4") { Method = HttpMethod.GET, Timeout = 1000 }; Exception thrownException = null; request.Failed += e => thrownException = e; Assert.Throws <WebException>(request.Perform); Assert.IsTrue(request.Completed); Assert.IsTrue(request.Aborted); Assert.IsTrue(thrownException != null); Assert.AreEqual(WebRequest.MAX_RETRIES, request.RetryCount); Assert.AreEqual(typeof(WebException), thrownException.GetType()); }
public void TestFailTimeout() { var request = new WebRequest($"{default_protocol}://{host}/delay/4") { Method = HttpMethod.Get, AllowInsecureRequests = true, Timeout = 1000 }; Exception thrownException = null; request.Failed += e => thrownException = e; Assert.Throws <WebException>(request.Perform); Assert.IsTrue(request.Completed); Assert.IsTrue(request.Aborted); Assert.IsTrue(thrownException != null); Assert.AreEqual(WebRequest.MAX_RETRIES, request.RetryCount); Assert.AreEqual(typeof(WebException), thrownException.GetType()); }
public void TestNoContentPost([Values(true, false)] bool async) { var request = new WebRequest($"{default_protocol}://{host}/anything") { Method = HttpMethod.Post, AllowInsecureRequests = true, }; if (async) { Assert.DoesNotThrowAsync(request.PerformAsync); } else { Assert.DoesNotThrow(request.Perform); } var responseJson = JsonConvert.DeserializeObject <HttpBinPostResponse>(request.GetResponseString()); Assert.IsTrue(request.Completed); Assert.IsFalse(request.Aborted); Assert.AreEqual(0, responseJson?.Headers.ContentLength); }
public void TestBadStatusCode(bool async) { var request = new WebRequest("https://httpbin.org/hidden-basic-auth/user/passwd"); bool hasThrown = false; request.Failed += exception => hasThrown = exception != null; if (async) { Assert.ThrowsAsync <WebException>(request.PerformAsync); } else { Assert.Throws <WebException>(request.Perform); } Assert.IsTrue(request.Completed); Assert.IsTrue(request.Aborted); Assert.IsEmpty(request.ResponseString); Assert.IsTrue(hasThrown); }
public static void AuthenticatedBlockingPerform(this WebRequest r) { r.AddHeader("Authorization", $"token {GitHubAccessToken}"); r.Perform(); }
private static void UploadBuild(string version) { if (!CanGitHub) { return; } Write("Publishing to GitHub..."); var req = new JsonWebRequest <GitHubRelease>($"{GitHubApiEndpoint}releases") { Method = HttpMethod.Post, }; GitHubRelease targetRelease = GetLastGithubRelease(); if (targetRelease == null || targetRelease.TagName != version) { Write($"- Creating release {version}...", ConsoleColor.Yellow); // Get all of the previous PR's name string body = $"Version {version} for {GitHubRepoName} has been released! this now fixes and adds:\r\n"; var requests = GetPullRequests(); // Adds every pull requests after the last release requests.ForEach(pr => { body += $"- {pr.Title} | [{pr.User.Name}]({pr.User.Link})\r\n"; }); req.AddRaw(JsonConvert.SerializeObject(new GitHubRelease { Name = $"{GitHubRepoName} v{version}", TagName = version, Description = body, Draft = true, PublishedAt = DateTime.Now })); req.AuthenticatedBlockingPerform(); targetRelease = req.ResponseObject; } else { Write($"- Adding to existing release {version}...", ConsoleColor.Yellow); } var assetUploadUrl = targetRelease.UploadUrl.Replace("{?name,label}", "?name={0}"); foreach (var a in Directory.GetFiles(staging_folder).Reverse()) { if (Path.GetFileName(a).StartsWith('.')) { continue; } if (!Path.GetFileName(a).EndsWith(".nupkg")) { continue; } var upload = new WebRequest(assetUploadUrl, Path.GetFileName(a)) { Method = HttpMethod.Post, Timeout = 240000, ContentType = "application/octet-stream", }; upload.AddRaw(File.ReadAllBytes(a)); upload.AuthenticatedBlockingPerform(); } OpenGitHubReleasePage(); }