Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node/http(s)): Set the url protocol by default #2480

Merged
merged 4 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export interface RequestOptions {

/** ClientRequest represents the http(s) request from the client */
class ClientRequest extends NodeWritable {
defaultProtocol = "http:";
body: null | ReadableStream = null;
controller: ReadableStreamDefaultController | null = null;
constructor(
Expand Down Expand Up @@ -173,9 +174,9 @@ class ClientRequest extends NodeWritable {
path,
port,
} = opts;
return `${protocol}//${auth ? `${auth}@` : ""}${host ?? hostname}${
port ? `:${port}` : ""
}${path}`;
return `${protocol ?? this.defaultProtocol}//${auth ? `${auth}@` : ""}${
host ?? hostname
}${port ? `:${port}` : ""}${path || ""}`;
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions node/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,25 @@ Deno.test("[node/http chunked response", async () => {
await promise;
}
});

Deno.test("[node/http] request default protocol", async () => {
const promise = deferred<void>();
const server = http.createServer((_, res) => {
res.end("ok");
});
server.listen(() => {
const req = http.request(
{ host: "localhost", port: server.address().port },
(res) => {
res.on("data", () => {});
res.on("end", () => {
server.close();
promise.resolve();
});
assertEquals(res.statusCode, 200);
},
);
req.end();
});
await promise;
});
1 change: 1 addition & 0 deletions node/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function get(...args: any[]) {
export const globalAgent = undefined;
/** HttpsClientRequest class loosely follows http.ClientRequest class API. */
class HttpsClientRequest extends ClientRequest {
override defaultProtocol = "https:";
override async _createCustomClient(): Promise<
DenoUnstable.HttpClient | undefined
> {
Expand Down