Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-http-client

Source of release 0.5.2

0.5.2 / tests / origin / drivers / client.lucb

import client as http_client
from luce_std import net
import sync
import thread

var cancellation: sync.Cancellation

func cancel_later(raw: void*?):
    _ = raw
    thread.sleep(80)
    cancellation.request()

pub func main(arguments: str[]) -> i32!:
    if arguments.length < 2: return 2
    let port = arguments[1]
    var digits: u16
    var i: usize
    while i < port.length:
        let c = port.bytes[i]
        if c < '0' or c > '9': return 2
        digits = digits * 10 + (u16)(c - '0')
        i += 1
    var body: u8[4096]
    let hello = try http_client.get(digits, "/hello", body)
    assert(hello.status == 200)
    assert(hello.count == 5)
    assert(body[0] == 'h' and body[1] == 'e' and body[2] == 'l' and body[3] == 'l' and body[4] == 'o')
    let echoed = try http_client.post(digits, "/echo", b"ping-body", body)
    assert(echoed.status == 200)
    assert(echoed.count == 9)
    var extras: net.HttpField[2]
    extras[0] = net.HttpField(name = "Authorization", value = "Bearer secret")
    extras[1] = net.HttpField(name = "X-Forwarded-For", value = "8.8.8.8")
    let headers = try http_client.request(net.SocketAddress.loopback(digits), "GET", "/headers",
        "127.0.0.1", extras, 2, b"", body)
    assert(headers.status == 200)
    let text = (str)body[..<headers.count]
    assert(text == "Authorization=Bearer secret\nX-Forwarded-For=8.8.8.8\n")
    for target in ["/chunked", "/eof"]:
        let response = try http_client.get(digits, target, body)
        assert(response.status == 200 and response.count == 5)
        assert((str)body[..<response.count] == "hello")
    for target in ["/truncated", "/bad-chunk", "/oversized"]:
        var rejected = false
        _ = http_client.get(digits, target, body) catch failure:
            _ = failure
            rejected = true
            recover http_client.Result()
        assert(rejected)
    for target in ["/stall", "/drip"]:
        var timed_out = false
        _ = http_client.get(digits, target, body, timeout_ms = 150) catch failure:
            assert(failure.code == net.timed_out)
            timed_out = true
            recover http_client.Result()
        assert(timed_out)
    var immediate = false
    _ = http_client.get(digits, "/hello", body, timeout_ms = 0) catch failure:
        assert(failure.code == net.timed_out)
        immediate = true
        recover http_client.Result()
    assert(immediate)
    var overflow = false
    _ = http_client.get(digits, "/hello", body, timeout_ms = 18446744073709551615) catch failure:
        assert(failure.code == net.invalid_options)
        overflow = true
        recover http_client.Result()
    assert(overflow)
    var worker = try thread.spawn(cancel_later, none, name = "http-cancel")
    var cancelled = false
    _ = http_client.get(digits, "/stall", body, cancellation = &cancellation) catch failure:
        assert(failure.code == net.cancelled)
        cancelled = true
        recover http_client.Result()
    try worker.join()
    assert(cancelled)
    var precancelled = false
    _ = http_client.get(digits, "/hello", body, cancellation = &cancellation) catch failure:
        assert(failure.code == net.cancelled)
        precancelled = true
        recover http_client.Result()
    assert(precancelled)
    let after = try http_client.get(digits, "/hello", body)
    assert(after.status == 200 and after.count == 5)
    print("PASS HTTP/1.1 round-trip, absolute deadline, slow progress, cancellation and recovery")
    return 0