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

RequestTest.pudu

Pudu34 lines2.6 KB

GitHub ↗
1/** @Test.Request.Suite — requests built one decision at a time */2module PuduLangHttpClient.RequestTest34import Std.Http as Http5import Std.Io as Io6import Std.Test as Test7import PuduLangHttpClient.Content as Content8import PuduLangHttpClient.Domain.Version as Version9import PuduLangHttpClient.Options as Options10import PuduLangHttpClient.Request as Request1112/// Runs the suite.13fn main() -> Int {14  let tenant = Options.textKey("tenant")15  let shaped = Request.get("/items").withHeader("x-a", "1").withHeader("X-A", "2").setHeader("x-b", "3").removeHeader("x-a").withVersion(Http.Http10, Version.Exact).withOption(&tenant, "acme").withBearer("t0k").accepting("application/json")16  let posted = Request.post("/items", Content.plain("x"))17  let checks = Test.suite("Request", &[18      Test.equals("a new request is HTTP/1.1 or lower with nothing else", &Request.create(Http.Options, "*"), &Request.Request{method: Http.Options, uri: "*", version: Http.Http11, versionPolicy: Version.OrLower, headers: [], content: None, options: Options.empty(), receiver: None}),19      Test.equals("each verb names its method", &[Request.get("/").method, Request.head("/").method, Request.delete("/").method, posted.method, Request.put("/", Content.empty()).method, Request.patch("/", Content.empty()).method], &[Http.Get, Http.Head, Http.Delete, Http.Post, Http.Put, Http.Patch]),20      Test.equals("a verb with a body carries it", &(posted.content, Request.put("/", Content.plain("p")).content, Request.patch("/", Content.plain("q")).content), &(Some(Content.plain("x")), Some(Content.plain("p")), Some(Content.plain("q")))),21      Test.equals("headers are added, set, and removed", &shaped.headers, &[("x-b", "3"), ("authorization", "Bearer t0k"), ("accept", "application/json")]),22      Test.equals("the version and policy are kept", &(shaped.version, shaped.versionPolicy), &(Http.Http10, Version.Exact)),23      Test.equals("an option reads back", &Request.option(&shaped, &tenant), &Some("acme")),24      Test.equals("a header is found among the request's own and then its body's", &(Request.header(&posted.withHeader("Accept", "*/*"), "accept"), Request.header(&posted, "Content-Type"), Request.header(&Request.get("/"), "content-type")), &(Some("*/*"), Some("text/plain; charset=utf-8"), None)),25      Test.equals("content replaces content", &Request.get("/").withContent(Content.plain("a")).withContent(Content.plain("b")).content, &Some(Content.plain("b"))),26      Test.that("a receiver is attached", Request.get("/").withReceiver(|_piece: Bytes| true).receiver != None)27    ])28  let ran = Test.run(&checks)29  for failure in Test.failuresOf(&ran) {30    let _reported = Io.writeErrorLine(failure)31  }32  Test.report(&ran)33}34