Skip to content

test: dns existence from resource url #1309

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"config:generate": "deno run --allow-net --allow-write ./src/config/generate.ts",
"build": "deno run -R -W ./src/build.js",
"lint": "deno fmt --check ./src ./test",
"tests": "deno test --allow-read"
"tests": "deno test --allow-read --allow-env",
"tests:dns": "VALIDATE_DNS=1 deno test --allow-read --allow-env --allow-net"
},
"imports": {
"@std/path": "jsr:@std/path@^1.0.8",
Expand Down
22 changes: 21 additions & 1 deletion test/filters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@ import { expect } from "jsr:@std/expect";
import { readFileSync } from "node:fs";
import path from "node:path";
import process from "node:process";
import dns from "node:dns/promises";
import * as tldts from "npm:tldts";
import { parse } from "../src/assert.ts";

const VALIDATE_DNS = !!Deno.env.get("VALIDATE_DNS");
const cwd = process.cwd();

const hasDnsRecord = async (hostname: string) => {
for (const rr of ["A", "AAAA", "CNAME"]) {
if (
await dns.resolve(hostname, rr)
.catch((_error) => false as const)
) {
return;
}
}
throw new Error("RECORD_NOT_FOUND: " + hostname);
};

const doTest = (filePath: string) => {
const content = readFileSync(path.join(cwd, filePath), "utf8");

Expand All @@ -18,7 +33,7 @@ const doTest = (filePath: string) => {
continue;
}

Deno.test(filter, () => {
Deno.test(filter, async () => {
const parsed = parseFilter(filter);

// Is a valid filter
Expand All @@ -37,6 +52,11 @@ const doTest = (filePath: string) => {
expect((NetworkFilter.parse(filter)!).match(
Request.fromRawDetails({ url, type, sourceUrl: source }),
)).toBe(match);

if (VALIDATE_DNS && url) {
await expect(hasDnsRecord(tldts.parse(url)!.hostname!))
.resolves.not.toThrow();
}
}
});
}
Expand Down