Skip to content
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
31 changes: 17 additions & 14 deletions packages/cacheable-request/test/cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// biome-ignore-all lint/suspicious/noImplicitAnyLet: legacy format
// biome-ignore-all lint/suspicious/noExplicitAny: legacy format
import { Agent, request } from "node:http";
import url from "node:url";
import util, { promisify as pm } from "node:util";
import { gunzip, gzip } from "node:zlib";
import delay from "delay";
import getStream from "get-stream";
import { Keyv } from "keyv";
import { afterAll, beforeAll, expect, test } from "vitest";
import CacheableRequest, { type CacheValue, onResponse } from "../src/index.js";
import CacheableRequest, {
type CacheValue,
onResponse,
parseWithWhatwg,
} from "../src/index.js";
import createTestServer from "./create-test-server/index.mjs";

const testTimeout = 10_000;
Expand Down Expand Up @@ -403,8 +406,8 @@ test("request options path query is passed through", async () => {
const cacheableRequest = new CacheableRequest(request);
const cacheableRequestHelper = promisify(cacheableRequest.request());
const argumentString = `${s.url}/echo?foo=bar`;
const argumentUrl = new url.URL(argumentString);
const urlObject = url.parse(argumentString);
const argumentUrl = new URL(argumentString);
const urlObject = parseWithWhatwg(argumentString);
const argumentOptions = {
hostname: urlObject.hostname,
port: urlObject.port,
Expand All @@ -429,7 +432,7 @@ test("Setting opts.cache to false bypasses cache for a single request", async ()
const cache = new Map();
const cacheableRequest = new CacheableRequest(request, cache);
const cacheableRequestHelper = promisify(cacheableRequest.request());
const options = url.parse(s.url + endpoint);
const options = parseWithWhatwg(s.url + endpoint);
const optionsNoCache = { cache: false, ...options };
const firstResponse: any = await cacheableRequestHelper(options);
const secondResponse: any = await cacheableRequestHelper(options);
Expand All @@ -456,7 +459,7 @@ test("TTL is passed to cache", async () => {
};
const cacheableRequest = new CacheableRequest(request, cache);
const cacheableRequestHelper = promisify(cacheableRequest.request());
const options = { strictTtl: true, ...url.parse(s.url + endpoint) };
const options = { strictTtl: true, ...parseWithWhatwg(s.url + endpoint) };
await cacheableRequestHelper(options);
});
test("TTL is not passed to cache if strictTtl is false", async () => {
Expand All @@ -474,7 +477,7 @@ test("TTL is not passed to cache if strictTtl is false", async () => {
};
const cacheableRequest = new CacheableRequest(request, cache);
const cacheableRequestHelper = promisify(cacheableRequest.request());
const options = { strictTtl: false, ...url.parse(s.url + endpoint) };
const options = { strictTtl: false, ...parseWithWhatwg(s.url + endpoint) };
await cacheableRequestHelper(options);
});
test("Setting opts.maxTtl will limit the TTL", async () => {
Expand All @@ -493,7 +496,7 @@ test("Setting opts.maxTtl will limit the TTL", async () => {
const cacheableRequest = new CacheableRequest(request, cache);
const cacheableRequestHelper = promisify(cacheableRequest.request());
const options = {
...url.parse(s.url + endpoint),
...parseWithWhatwg(s.url + endpoint),
maxTtl: 1000,
};
await cacheableRequestHelper(options);
Expand All @@ -514,7 +517,7 @@ test("Setting opts.maxTtl when opts.strictTtl is true will use opts.maxTtl if it
const cacheableRequest = new CacheableRequest(request, cache);
const cacheableRequestHelper = promisify(cacheableRequest.request());
const options = {
...url.parse(s.url + endpoint),
...parseWithWhatwg(s.url + endpoint),
strictTtl: true,
maxTtl: 1000,
};
Expand All @@ -536,7 +539,7 @@ test("Setting opts.maxTtl when opts.strictTtl is true will use remote TTL if it'
const cacheableRequest = new CacheableRequest(request, cache);
const cacheableRequestHelper = promisify(cacheableRequest.request());
const options = {
...url.parse(s.url + endpoint),
...parseWithWhatwg(s.url + endpoint),
strictTtl: true,
maxTtl: 100_000,
};
Expand Down Expand Up @@ -649,7 +652,7 @@ test("ability to force refresh", async () => {
const cache = new Map();
const cacheableRequest = new CacheableRequest(request, cache);
const cacheableRequestHelper = promisify(cacheableRequest.request());
const options = url.parse(s.url + endpoint);
const options = parseWithWhatwg(s.url + endpoint);
const firstResponse: any = await cacheableRequestHelper(options);
const secondResponse: any = await cacheableRequestHelper({
...options,
Expand All @@ -664,7 +667,7 @@ test("checks status codes when comparing cache & response", async () => {
const cache = new Map();
const cacheableRequest = new CacheableRequest(request, cache);
const cacheableRequestHelper = promisify(cacheableRequest.request());
const options = url.parse(s.url + endpoint);
const options = parseWithWhatwg(s.url + endpoint);
const firstResponse: any = await cacheableRequestHelper(options);
const secondResponse: any = await cacheableRequestHelper(options);
expect(firstResponse.body).toBe("received 502");
Expand All @@ -676,7 +679,7 @@ test("304 responses with forceRefresh do not clobber cache", async () => {
const cache = new Map();
const cacheableRequest = new CacheableRequest(request, cache);
const cacheableRequestHelper = promisify(cacheableRequest.request());
const options = url.parse(s.url + endpoint);
const options = parseWithWhatwg(s.url + endpoint);

const firstResponse: any = await cacheableRequestHelper(options);
const secondResponse: any = await cacheableRequestHelper({
Expand Down Expand Up @@ -743,7 +746,7 @@ test("socket within keepAlive Agent has been free'd after cache revalidation", a
const agent = new Agent({
keepAlive: true,
});
const options = { agent, ...url.parse(s.url + endpoint) };
const options = { agent, ...parseWithWhatwg(s.url + endpoint) };
try {
expect(Object.keys(agent.freeSockets)).toHaveLength(0);
// biome-ignore lint/correctness/noUnusedVariables: legacy
Expand Down
37 changes: 18 additions & 19 deletions packages/cacheable-request/test/cacheable-request-instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import EventEmitter from "node:events";
import { request } from "node:http";
import stream from "node:stream";
import url from "node:url";
import { KeyvSqlite } from "@keyv/sqlite";
import getStream from "get-stream";
import { afterAll, beforeAll, expect, test } from "vitest";
import CacheableRequest from "../src/index.js";
import CacheableRequest, { parseWithWhatwg } from "../src/index.js";
import { CacheError, RequestError } from "../src/types.js";
import createTestServer from "./create-test-server/index.mjs";

Expand All @@ -30,7 +29,7 @@ test("cacheableRequest is a class", () => {
});
test("cacheableRequest returns an event emitter", () => {
const cacheableRequest = new CacheableRequest(request).request();
const returnValue = cacheableRequest(url.parse(s.url), () => true).on(
const returnValue = cacheableRequest(parseWithWhatwg(s.url), () => true).on(
"request",
(request_: any) => request_.end(),
);
Expand All @@ -39,7 +38,7 @@ test("cacheableRequest returns an event emitter", () => {

test("cacheableRequest passes requests through if no cache option is set", () => {
const cacheableRequest = new CacheableRequest(request).request();
cacheableRequest(url.parse(s.url), async (response: any) => {
cacheableRequest(parseWithWhatwg(s.url), async (response: any) => {
const body = await getStream(response);
expect(body).toBe("hi");
}).on("request", (request_: any) => request_.end());
Expand All @@ -53,14 +52,14 @@ test("cacheableRequest accepts url as string", () => {
});
test("cacheableRequest accepts url as URL", () => {
const cacheableRequest = new CacheableRequest(request).request();
cacheableRequest(new url.URL(s.url), async (response: any) => {
cacheableRequest(new URL(s.url), async (response: any) => {
const body = await getStream(response);
expect(body).toBe("hi");
}).on("request", (request_: any) => request_.end());
});
test("cacheableRequest handles no callback parameter", () => {
const cacheableRequest = new CacheableRequest(request).request();
cacheableRequest(url.parse(s.url)).on("request", (request_: any) => {
cacheableRequest(parseWithWhatwg(s.url)).on("request", (request_: any) => {
request_.end();
request_.on("response", (response: any) => {
expect(response.statusCode).toBe(200);
Expand All @@ -69,7 +68,7 @@ test("cacheableRequest handles no callback parameter", () => {
});
test("cacheableRequest emits response event for network responses", () => {
const cacheableRequest = new CacheableRequest(request).request();
cacheableRequest(url.parse(s.url))
cacheableRequest(parseWithWhatwg(s.url))
.on("request", (request_: any) => request_.end())
.on("response", (response: any) => {
expect(response.fromCache).toBeFalsy();
Expand All @@ -96,7 +95,7 @@ test("cacheableRequest emits CacheError if cache adapter connection errors", ()
request,
new KeyvSqlite("sqlite://non/existent/database.sqlite"),
).request();
cacheableRequest(url.parse(s.url))
cacheableRequest(parseWithWhatwg(s.url))
.on("error", (error: any) => {
expect(error instanceof CacheError).toBeTruthy();
if (error.code === "SQLITE_CANTOPEN") {
Expand All @@ -117,7 +116,7 @@ test("cacheableRequest emits CacheError if cache.get errors", async () => {
clear: store.clear.bind(store),
};
const cacheableRequest = new CacheableRequest(request, cache).request();
cacheableRequest(url.parse(s.url))
cacheableRequest(parseWithWhatwg(s.url))
.on("error", (error: any) => {
expect(error instanceof CacheError).toBeTruthy();
expect(error.message).toBe(errorMessage);
Expand All @@ -136,7 +135,7 @@ test("cacheableRequest emits CacheError if cache.set errors", () => {
clear: store.clear.bind(store),
};
const cacheableRequest = new CacheableRequest(request, cache).request();
cacheableRequest(url.parse(s.url))
cacheableRequest(parseWithWhatwg(s.url))
.on("error", (error: any) => {
expect(error instanceof CacheError).toBeTruthy();
expect(error.message).toBe(errorMessage);
Expand Down Expand Up @@ -181,7 +180,7 @@ test("cacheableRequest emits CacheError if cache.delete errors", () => {
});
test("cacheableRequest emits Error if request function throws", () => {
const cacheableRequest = new CacheableRequest(request).request();
const options: any = url.parse(s.url);
const options: any = parseWithWhatwg(s.url);
options.headers = { invalid: "💣" };
cacheableRequest(options)
.on("error", (error: any) => {
Expand All @@ -200,7 +199,7 @@ test("cacheableRequest does not cache response if request is aborted before rece
const cacheableRequest = new CacheableRequest(request).request();

// biome-ignore lint/style/noNonNullAssertion: legacy
const options = url.parse(s.url!);
const options = parseWithWhatwg(s.url!);
options.path = "/delay-start";
cacheableRequest(options).on("request", (request_: any) => {
request_.end();
Expand Down Expand Up @@ -231,7 +230,7 @@ test("cacheableRequest does not cache response if request is aborted after recei
const cacheableRequest = new CacheableRequest(request).request();

// biome-ignore lint/style/noNonNullAssertion: legacy
const options = url.parse(s.url!);
const options = parseWithWhatwg(s.url!);
options.path = "/delay-partial";
cacheableRequest(options).on("request", (request_: any) => {
setTimeout(() => {
Expand All @@ -253,7 +252,7 @@ test("cacheableRequest makes request even if initial DB connection fails (when o
request,
new KeyvSqlite("sqlite://non/existent/database.sqlite"),
).request();
const options: any = url.parse(s.url);
const options: any = parseWithWhatwg(s.url);
options.automaticFailover = true;
cacheableRequest(options, (response_: any) => {
expect(response_.statusCode).toBe(200);
Expand All @@ -279,7 +278,7 @@ test("cacheableRequest makes request even if current DB connection fails (when o
},
};
const cacheableRequest = new CacheableRequest(request, cache).request();
const options: any = url.parse(s.url);
const options: any = parseWithWhatwg(s.url);
options.automaticFailover = true;
cacheableRequest(options, (response_: any) => {
expect(response_.statusCode).toBe(200);
Expand All @@ -305,7 +304,7 @@ test("cacheableRequest hashes request body as cache key", async () => {
},
};
const cacheableRequest = new CacheableRequest(request, cache).request();
const options: any = url.parse(s.url);
const options: any = parseWithWhatwg(s.url);
options.body = "hello";
options.method = "POST";
cacheableRequest(options, (response_: any) => {
Expand All @@ -332,7 +331,7 @@ test("cacheableRequest skips cache for streamed body", () => {
},
};
const cacheableRequest = new CacheableRequest(request, cache).request();
const options: any = url.parse(s.url);
const options: any = parseWithWhatwg(s.url);
options.body = new stream.PassThrough();
options.method = "POST";
cacheableRequest(options, (response_: any) => {
Expand All @@ -347,7 +346,7 @@ test("cacheableRequest skips cache for streamed body", () => {

test("cacheableRequest makes request and cancelled)", async () => {
const cacheableRequest = new CacheableRequest(request, new Map()).request();
const options: any = url.parse(s.url);
const options: any = parseWithWhatwg(s.url);
cacheableRequest(options, (response_: any) => {
expect(response_.statusCode).toBe(400);
})
Expand All @@ -359,7 +358,7 @@ test("cacheableRequest makes request and cancelled)", async () => {

test("cacheableRequest emits CacheError if request cancels", () => {
const cacheableRequest = new CacheableRequest(request).request();
const options: any = url.parse(s.url);
const options: any = parseWithWhatwg(s.url);
options.headers = { invalid: "💣" };
cacheableRequest(options)
.on("error", (error: any) => {
Expand Down
Loading