
PuduLangHttpClientTest.pudu
Pudu39 lines2.9 KB
1/** @Test.Root.Suite — failures, their descriptions, and their kinds */2module PuduLangHttpClientTest34import Std.Io as Io5import Std.Test as Test6import PuduLangHttpClient as HttpClient789const EVERY: Array[HttpClient.Failure] = [10 HttpClient.InvalidRequest("r"), HttpClient.NameResolution("n"), HttpClient.Connection("c"), HttpClient.SecureConnection("s"),11 HttpClient.ProxyTunnel("p"), HttpClient.Protocol("x"), HttpClient.ResponseEnded, HttpClient.ContentTooLarge(1),12 HttpClient.HeadersTooLarge(2), HttpClient.TooManyRedirects(3), HttpClient.NotPermitted("np"), HttpClient.VersionUnsupported("HTTP/2"),13 HttpClient.TimedOut(4), HttpClient.Cancelled("why"), HttpClient.Unsuccessful(404, "Not Found"), HttpClient.Unreadable("u"),14 HttpClient.Rejected("open"), HttpClient.Crashed("boom")15]161718fn main() -> Int {19 let checks = Test.suite("Root", &[20 Test.equals("transient failures are the ones a retry may cure", &EVERY.filter(|failure: HttpClient.Failure| HttpClient.isTransient(&failure)), &[HttpClient.NameResolution("n"), HttpClient.Connection("c"), HttpClient.ProxyTunnel("p"), HttpClient.Protocol("x"), HttpClient.ResponseEnded, HttpClient.TimedOut(4)]),21 Test.equals("only a cancellation is one", &EVERY.filter(|failure: HttpClient.Failure| HttpClient.isCancellation(&failure)), &[HttpClient.Cancelled("why")]),22 Test.equals("only a timeout is one", &EVERY.filter(|failure: HttpClient.Failure| HttpClient.isTimeout(&failure)), &[HttpClient.TimedOut(4)]),23 Test.equals("only an unsuccessful status reports its code", &EVERY.map(|failure: HttpClient.Failure| HttpClient.statusOf(&failure)).filter(|code: Option[Int]| code != None), &[Some(404)]),24 Test.equals("every failure is described in its own words", &EVERY.map(|failure: HttpClient.Failure| HttpClient.describe(&failure)), &[25 "The request is invalid: r", "The host name could not be resolved: n", "The connection failed: c", "The secured connection failed: s",26 "The proxy did not open a tunnel: p", "The response broke the protocol: x", "The connection closed before the response was complete.", "The response body is larger than 1 bytes.",27 "The response headers are larger than 2 bytes.", "The request was redirected more than 3 times.", "The destination is not permitted: np", "The request asks for HTTP/2, which the transport does not speak.",28 "The request timed out after 4 ms.", "The request was cancelled: why", "The response status 404 (Not Found) does not indicate success.", "The body could not be read: u",29 "The request was rejected: open", "A handler stopped its thread: boom"30 ]),31 Test.equals("an invalid outcome is an invalid request", &HttpClient.invalid("bad"), &Err[Int, HttpClient.Failure](HttpClient.InvalidRequest("bad")))32 ])33 let ran = Test.run(&checks)34 for failure in Test.failuresOf(&ran) {35 let _reported = Io.writeErrorLine(failure)36 }37 Test.report(&ran)38}39