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.GetResponseString()); Assert.IsTrue(hasThrown); }
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.GetResponseString() == null); Assert.IsNotNull(finishedException); }
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); }