diff options
author | 2023-03-01 19:31:16 -0800 | |
---|---|---|
committer | 2023-03-01 19:31:16 -0800 | |
commit | 706a3e8169ae27b1b5c3694d46b593f220c41b80 (patch) | |
tree | 4130aa2d116ba036e26e26ff501e7361d9fe22b9 | |
parent | 7c81d97684e39e1fb37bef7f39ea13c936f6b99a (diff) | |
download | bun-706a3e8169ae27b1b5c3694d46b593f220c41b80.tar.gz bun-706a3e8169ae27b1b5c3694d46b593f220c41b80.tar.zst bun-706a3e8169ae27b1b5c3694d46b593f220c41b80.zip |
Add a test for https request in node:http
-rw-r--r-- | src/bun.js/http.exports.js | 29 | ||||
-rw-r--r-- | test/bun.js/node-http.test.ts | 16 |
2 files changed, 28 insertions, 17 deletions
diff --git a/src/bun.js/http.exports.js b/src/bun.js/http.exports.js index 9ea6fec8c..a7f67f0e9 100644 --- a/src/bun.js/http.exports.js +++ b/src/bun.js/http.exports.js @@ -8,7 +8,7 @@ const setTimeout = globalThis.setTimeout; const fetch = Bun.fetch; const nop = () => {}; -const __DEBUG__ = process.env.BUN_JS_DEBUG; +const __DEBUG__ = process.env.__DEBUG__; const debug = __DEBUG__ ? (...args) => console.log("node:http", ...args) : nop; const kEmptyObject = Object.freeze(Object.create(null)); @@ -1018,8 +1018,7 @@ export class ClientRequest extends OutgoingMessage { } const defaultAgent = options._defaultAgent || Agent.globalAgent; - const protocol = (this.#protocol = options.protocol || defaultAgent.protocol); - const expectedProtocol = defaultAgent.protocol; + const protocol = (this.#protocol = options.protocol ||= defaultAgent.protocol); if (options.path) { const path = String(options.path); @@ -1030,14 +1029,14 @@ export class ClientRequest extends OutgoingMessage { } } - if (protocol !== expectedProtocol) { + // Since we don't implement Agent, we don't need this + if (protocol !== "http:" && protocol !== "https:" && protocol) { + const expectedProtocol = defaultAgent?.protocol ?? "http:"; throw new Error(`Protocol mismatch. Expected: ${expectedProtocol}. Got: ${protocol}`); // throw new ERR_INVALID_PROTOCOL(protocol, expectedProtocol); } - const defaultPort = options.defaultPort || (this.#agent && this.#agent.defaultPort); - - this.#port = options.port = options.port || defaultPort || 80; + this.#port = options.port || options.defaultPort || this.#agent?.defaultPort || 80; const host = (this.#host = options.host = @@ -1102,7 +1101,8 @@ export class ClientRequest extends OutgoingMessage { this.once("response", cb); } - debug(`new ClientRequest: ${this.#method} ${this.#protocol}//${this.#host}:${this.#port}${this.#path}`); + __DEBUG__ && + debug(`new ClientRequest: ${this.#method} ${this.#protocol}//${this.#host}:${this.#port}${this.#path}`); // if ( // method === "GET" || @@ -1194,13 +1194,13 @@ export class ClientRequest extends OutgoingMessage { } setSocketKeepAlive(enable = true, initialDelay = 0) { - debug(`${NODE_HTTP_WARNING}\n`, "WARN: ClientRequest.setSocketKeepAlive is a no-op"); + __DEBUG__ && debug(`${NODE_HTTP_WARNING}\n`, "WARN: ClientRequest.setSocketKeepAlive is a no-op"); } } function urlToHttpOptions(url) { var { protocol, hostname, hash, search, pathname, href, port, username, password } = url; - const options = { + return { protocol, hostname: typeof hostname === "string" && StringPrototypeStartsWith.call(hostname, "[") @@ -1211,14 +1211,9 @@ function urlToHttpOptions(url) { pathname, path: `${pathname || ""}${search || ""}`, href, + port: port ? Number(port) : protocol === "https:" ? 443 : protocol === "http:" ? 80 : undefined, + auth: username || password ? `${decodeURIComponent(username)}:${decodeURIComponent(password)}` : undefined, }; - if (port !== "") { - options.port = Number(port); - } - if (username || password) { - options.auth = `${decodeURIComponent(username)}:${decodeURIComponent(password)}`; - } - return options; } function validateHost(host, name) { diff --git a/test/bun.js/node-http.test.ts b/test/bun.js/node-http.test.ts index 57f4b475b..40102cd9a 100644 --- a/test/bun.js/node-http.test.ts +++ b/test/bun.js/node-http.test.ts @@ -163,6 +163,22 @@ describe("node:http", () => { req.end(); }); + it("should make a https:// GET request when passed string as first arg", done => { + const req = request("https://example.com", res => { + let data = ""; + res.setEncoding("utf8"); + res.on("data", chunk => { + data += chunk; + }); + res.on("end", () => { + expect(data).toContain("This domain is for use in illustrative examples in documents"); + done(); + }); + res.on("error", err => done(err)); + }); + req.end(); + }); + it("should make a POST request when provided POST method, even without a body", done => { const req = request({ host: "localhost", port: serverPort, method: "POST" }, res => { let data = ""; |