Files
scriptc/tests/corpus/2645-https-client-url-argument.ts
Chris Tate a7538fd87d URL-string and URL-object arguments to the http and https clients
- `http.get("http://host/path")` and its https spelling now compile. The declarations only described the options-record form, so the first thing a from-scratch client writes was a type error — the shape worked under --dynamic, where the engine does not consult our declarations, which is how it read as a hello-world failure.
- A URL object is accepted the same way, as its href through the same parse. An unparsable input is the WHATWG "Invalid URL" TypeError and a scheme that is not the calling module's is ERR_INVALID_PROTOCOL, both catchable, matching Node rather than silently upgrading the dial.
- `request` and `get` take one signature with a union first parameter instead of Node's two overloads: `tls ? https.request : http.request` unions two function types, and resolving a call on that union collapses each side to a single signature, so an overload set misresolves in either declaration order. The lowering dispatches on the argument's own type.
- Both rows run on the LLVM backend, and the tls CA-store entries joined it too — the omission there was stale, so 2557-tls-ca-store moved off the C lane with identical output.
- Corpus 2644 and 2645 cover both schemes against loopback servers, so neither needs the network.
2026-07-24 23:24:53 -05:00

57 lines
1.9 KiB
TypeScript

// The https client's URL-string argument, against a localhost TLS server
// so the test needs no network. The URL form takes no options, which
// means Node's defaults — the certificate IS verified — so the fixture
// installs the suite's CA as the default trust anchor first
// (tls.setDefaultCACertificates) and dials the name the cert carries.
// The scheme is checked against the MODULE: an http URL through the
// https client is ERR_INVALID_PROTOCOL, catchably, not a plain dial.
import * as fs from "node:fs";
import * as tls from "node:tls";
import * as https from "node:https";
const ca = fs.readFileSync("tests/fixtures/server/certs/ca.pem", "utf8");
tls.setDefaultCACertificates([ca]);
const server = https.createServer({
key: fs.readFileSync("tests/fixtures/server/certs/localhost-key.pem"),
cert: fs.readFileSync("tests/fixtures/server/certs/localhost.pem"),
}, (req, res) => {
res.end(`secure ${req.url}`);
});
server.listen(0, () => {
const port = server.address().port;
https.get(`https://localhost:${port}/one`, (res) => {
console.log("status", res.statusCode);
let body = "";
res.on("data", (c) => { body += c; });
res.on("end", () => {
console.log("body", body);
// the scheme is the calling module's, here the other way round
try {
https.get(`http://localhost:${port}/nope`, () => {});
} catch (e) {
console.log("scheme", (e as Error).message);
}
try {
https.get("not a url", () => {});
} catch (e) {
console.log("parse", (e as Error).message);
}
// request() is get() without the eager end()
const req = https.request(`https://localhost:${port}/two`, (res2) => {
let b2 = "";
res2.on("data", (c) => { b2 += c; });
res2.on("end", () => {
console.log("request", b2);
server.close();
});
});
req.end();
});
});
});