Skip to content

Commit

Permalink
cleanup & test
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrostorp committed Nov 21, 2023
1 parent aea641f commit caf1e35
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
15 changes: 15 additions & 0 deletions packages/wrangler/src/__tests__/dev.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,21 @@ describe("wrangler dev", () => {
});
});

describe("custom https certificates", () => {
it("should use `--https-key-path` and `--https-cert-path` command line arg, if provided", async () => {
writeWranglerToml({
main: "index.js",
});
fs.writeFileSync("index.js", `export default {};`);
await runWrangler("dev --https-key-path=foo-key.pem --https-cert-path=foo.pem");
expect((Dev as jest.Mock).mock.calls[0][0].httpsKeyPath).toEqual("foo-key.pem");
expect((Dev as jest.Mock).mock.calls[0][0].httpsCertPath).toEqual("foo.pem");
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
});

describe("ip", () => {
it("should default ip to *", async () => {
writeWranglerToml({
Expand Down
21 changes: 21 additions & 0 deletions packages/wrangler/src/__tests__/https-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ describe("getHttpsOptions()", () => {
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should use provided certificate key and cert if provided", async () => {

const CUSTOM_KEY = "CUSTOM PRIVATE KEY";
const CUSTOM_CERT = "CUSTOM PUBLIC KEY";
const keyPath = "custom-key.pem";
const certPath = "custom.pem";
fs.writeFileSync(keyPath, CUSTOM_KEY);
fs.writeFileSync(certPath, CUSTOM_CERT);

const result = await getHttpsOptions(keyPath, certPath);
const key = fs.readFileSync(keyPath, "utf8");
const cert = fs.readFileSync(certPath, "utf8");
expect(result.key).toEqual(key);
expect(result.cert).toEqual(cert);
expect(std.out).toMatchInlineSnapshot(
`"Using custom certificate at ${certPath}"`
);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
});

function mockStatSync(matcher: RegExp, stats: Partial<fs.Stats>) {
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/api/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export interface UnstableDevOptions {
bundle?: boolean; // Set to false to skip internal build steps and directly deploy script
inspectorPort?: number; // Port for devtools to connect to
localProtocol?: "http" | "https"; // Protocol to listen to requests on, defaults to http.
httpsKeyPath: string | undefined;
httpsCertPath: string | undefined;
httpsKeyPath?: string;
httpsCertPath?: string;
assets?: string; // Static assets to be served
site?: string; // Root folder of static assets for Workers Sites
siteInclude?: string[]; // Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded.
Expand Down
6 changes: 3 additions & 3 deletions packages/wrangler/src/https-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const ONE_DAY_IN_MS = 86400000;
* The certificates are self-signed and generated locally, and cached in the `CERT_ROOT` directory.
*/
export async function getHttpsOptions(
customHttpsKeyPath: string | undefined,
customHttpsCertPath: string | undefined
customHttpsKeyPath?: string,
customHttpsCertPath?: string
) {
const certDirectory = path.join(getGlobalWranglerConfigPath(), "local-cert");

Expand Down Expand Up @@ -63,7 +63,7 @@ export async function getHttpsOptions(
return { key, cert };
} else {
if (isCustomCert) {
logger.log("Using custom certificate at ", customHttpsKeyPath);
logger.log("Using custom certificate at", customHttpsCertPath);
}

return {
Expand Down

0 comments on commit caf1e35

Please sign in to comment.