Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-httpclient

Named HTTP clients for Pudu: a client factory, delegating handlers, pooled keep-alive connections, handler lifetimes, logging, and resilience

0.1.1Apache-2.01

InstallClose

WriterTest.pudu

Pudu42 lines2.7 KB

GitHub ↗
1/** @Test.Transport.Writer.Suite — requests written as the bytes that go on the wire */2module PuduLangHttpClient.Transport.WriterTest34import Std.Http as Http5import Std.Io as Io6import Std.Test as Test7import PuduLangHttpClient.Content as Content8import PuduLangHttpClient.Domain.Uri as Uri9import PuduLangHttpClient.Request as Request10import PuduLangHttpClient.Transport.Writer as Writer1112/// The text the writer produces for a request to its own endpoint.13fn written(request: &Request.Request, version: Http.Version, form: Writer.Form, extra: &Array[(Str, Str)]) -> Str {14  let endpoint = match Uri.endpointOf(request.uri) {15    case Some(found) => found16    case None => panic("no endpoint")17  }18  match Writer.render(request, &version, &endpoint, &form, extra).toText() {19    case Some(text) => text20    case None => ""21  }22}2324/// Runs the suite.25fn main() -> Int {26  let get = Request.get("http://api.test/v1/users?id=1#frag").withHeader("accept", "text/plain")27  let posted = Request.post("https://api.test:8443/x", Content.plain("héllo")).withHeader("content-length", "999")28  let checks = Test.suite("Transport.Writer", &[29      Test.equals("a get names its origin-form target, its host, and its headers", &written(&get, Http.Http11, Writer.OriginForm, &[]), &"GET /v1/users?id=1 HTTP/1.1\r\nhost: api.test\r\naccept: text/plain\r\n\r\n"),30      Test.equals("a body states its byte length and its own headers, replacing a stated length", &written(&posted, Http.Http11, Writer.OriginForm, &[]), &"POST /x HTTP/1.1\r\nhost: api.test:8443\r\ncontent-type: text/plain; charset=utf-8\r\ncontent-length: 6\r\n\r\nhéllo"),31      Test.equals("a put without a body states a length of zero", &written(&Request.create(Http.Put, "http://a/"), Http.Http11, Writer.OriginForm, &[]), &"PUT / HTTP/1.1\r\nhost: a\r\ncontent-length: 0\r\n\r\n"),32      Test.equals("a proxied request names its absolute target and carries the route's headers", &written(&Request.get("http://a:81/p#x"), Http.Http10, Writer.AbsoluteForm, &[("proxy-authorization", "Basic eA==")]), &"GET http://a:81/p HTTP/1.0\r\nhost: a:81\r\nproxy-authorization: Basic eA==\r\n\r\n"),33      Test.equals("a request naming its own host keeps it", &written(&Request.get("http://a/").withHeader("Host", "b"), Http.Http11, Writer.OriginForm, &[]), &"GET / HTTP/1.1\r\nHost: b\r\n\r\n"),34      Test.equals("a request framed by chunks states no length", &written(&Request.post("http://a/", Content.plain("x")).withHeader("transfer-encoding", "chunked"), Http.Http11, Writer.OriginForm, &[]), &"POST / HTTP/1.1\r\nhost: a\r\ntransfer-encoding: chunked\r\ncontent-type: text/plain; charset=utf-8\r\n\r\nx")35    ])36  let ran = Test.run(&checks)37  for failure in Test.failuresOf(&ran) {38    let _reported = Io.writeErrorLine(failure)39  }40  Test.report(&ran)41}42