From 179a106512eb22aff8d27753f1212e79f8111a79 Mon Sep 17 00:00:00 2001 From: HoJeong Go Date: Wed, 9 Apr 2025 18:26:29 +0900 Subject: [PATCH 1/9] test: dns existence from resource url --- test/filters.test.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/filters.test.ts b/test/filters.test.ts index d5f2e09..9f8b53c 100644 --- a/test/filters.test.ts +++ b/test/filters.test.ts @@ -3,8 +3,11 @@ 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 = typeof Deno.env.get('VALIDATE_DNS') !== "undefined"; const cwd = process.cwd(); const doTest = (filePath: string) => { @@ -18,7 +21,7 @@ const doTest = (filePath: string) => { continue; } - Deno.test(filter, () => { + Deno.test(filter, async () => { const parsed = parseFilter(filter); // Is a valid filter @@ -37,6 +40,12 @@ const doTest = (filePath: string) => { expect((NetworkFilter.parse(filter)!).match( Request.fromRawDetails({ url, type, sourceUrl: source }), )).toBe(match); + + if (VALIDATE_DNS === true && typeof url !== "undefined") { + const reply = await dns.lookup(tldts.parse(url)!.hostname!) + .catch(error => error); + expect(reply instanceof Error).toBe(false); + } } }); } From e9bcaf8ad47e5ee917230df69b5db72cb44db83b Mon Sep 17 00:00:00 2001 From: HoJeong Go Date: Wed, 9 Apr 2025 18:29:55 +0900 Subject: [PATCH 2/9] chore: add dns test command --- deno.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deno.json b/deno.json index 05a72bc..37506d0 100644 --- a/deno.json +++ b/deno.json @@ -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", + "tests:dns": "VALIDATE_DNS=1 deno test --allow-read --allow-env --allow-net" }, "imports": { "@std/path": "jsr:@std/path@^1.0.8", From 5def1c0e59014877586be39d990705d279c7fb90 Mon Sep 17 00:00:00 2001 From: HoJeong Go Date: Thu, 10 Apr 2025 14:31:35 +0900 Subject: [PATCH 3/9] Update test/filters.test.ts Co-authored-by: Krzysztof Modras <1228153+chrmod@users.noreply.github.com> --- test/filters.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/filters.test.ts b/test/filters.test.ts index 9f8b53c..e91e314 100644 --- a/test/filters.test.ts +++ b/test/filters.test.ts @@ -7,7 +7,7 @@ import dns from "node:dns/promises"; import * as tldts from "npm:tldts"; import { parse } from "../src/assert.ts"; -const VALIDATE_DNS = typeof Deno.env.get('VALIDATE_DNS') !== "undefined"; +const VALIDATE_DNS = !!Deno.env.get('VALIDATE_DNS'); const cwd = process.cwd(); const doTest = (filePath: string) => { From e3377fd4451a7de7609d10727b7bd9b0d42dd6ff Mon Sep 17 00:00:00 2001 From: HoJeong Go Date: Thu, 10 Apr 2025 14:31:46 +0900 Subject: [PATCH 4/9] Update test/filters.test.ts Co-authored-by: Krzysztof Modras <1228153+chrmod@users.noreply.github.com> --- test/filters.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/filters.test.ts b/test/filters.test.ts index e91e314..1d81b82 100644 --- a/test/filters.test.ts +++ b/test/filters.test.ts @@ -41,7 +41,7 @@ const doTest = (filePath: string) => { Request.fromRawDetails({ url, type, sourceUrl: source }), )).toBe(match); - if (VALIDATE_DNS === true && typeof url !== "undefined") { + if (VALIDATE_DNS && url) { const reply = await dns.lookup(tldts.parse(url)!.hostname!) .catch(error => error); expect(reply instanceof Error).toBe(false); From 68699b95303d90a2e226c7dd5d1214367d1223d6 Mon Sep 17 00:00:00 2001 From: HoJeong Go Date: Thu, 10 Apr 2025 15:13:22 +0900 Subject: [PATCH 5/9] feat: `hasDnsRecord` --- test/filters.test.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/test/filters.test.ts b/test/filters.test.ts index 9f8b53c..4221c0a 100644 --- a/test/filters.test.ts +++ b/test/filters.test.ts @@ -10,6 +10,15 @@ import { parse } from "../src/assert.ts"; const VALIDATE_DNS = typeof Deno.env.get('VALIDATE_DNS') !== "undefined"; const cwd = process.cwd(); +const hasDnsRecord = async (hostname: string) => { + for (const rr of ['A', 'AAAA', 'CNAME']) { + if (await dns.resolve(hostname, rr)) { + return; + } + } + throw new Error('RECORD_NOT_FOUND'); +} + const doTest = (filePath: string) => { const content = readFileSync(path.join(cwd, filePath), "utf8"); @@ -42,9 +51,8 @@ const doTest = (filePath: string) => { )).toBe(match); if (VALIDATE_DNS === true && typeof url !== "undefined") { - const reply = await dns.lookup(tldts.parse(url)!.hostname!) - .catch(error => error); - expect(reply instanceof Error).toBe(false); + await expect(hasDnsRecord(tldts.parse(url)!.hostname!)) + .resolves.not.toThrow(); } } }); From f49affbd51fa2006f75372793f154ff376318014 Mon Sep 17 00:00:00 2001 From: HoJeong Go Date: Thu, 10 Apr 2025 15:22:39 +0900 Subject: [PATCH 6/9] fix(lint): deno lint --- test/filters.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/filters.test.ts b/test/filters.test.ts index f26e825..a94127f 100644 --- a/test/filters.test.ts +++ b/test/filters.test.ts @@ -7,17 +7,17 @@ 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 VALIDATE_DNS = !!Deno.env.get("VALIDATE_DNS"); const cwd = process.cwd(); const hasDnsRecord = async (hostname: string) => { - for (const rr of ['A', 'AAAA', 'CNAME']) { + for (const rr of ["A", "AAAA", "CNAME"]) { if (await dns.resolve(hostname, rr)) { return; } } - throw new Error('RECORD_NOT_FOUND'); -} + throw new Error("RECORD_NOT_FOUND"); +}; const doTest = (filePath: string) => { const content = readFileSync(path.join(cwd, filePath), "utf8"); From 8bd6882f53d4cf15b89972d095211c4fd0773988 Mon Sep 17 00:00:00 2001 From: HoJeong Go Date: Thu, 10 Apr 2025 15:26:44 +0900 Subject: [PATCH 7/9] fix: permission --- deno.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deno.json b/deno.json index 37506d0..8153bc0 100644 --- a/deno.json +++ b/deno.json @@ -3,7 +3,7 @@ "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": { From 42930ca80c3fb625e45db522adb235b8c74dd39e Mon Sep 17 00:00:00 2001 From: HoJeong Go Date: Thu, 10 Apr 2025 15:31:43 +0900 Subject: [PATCH 8/9] test: skip unless found --- test/filters.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/filters.test.ts b/test/filters.test.ts index a94127f..f4633e5 100644 --- a/test/filters.test.ts +++ b/test/filters.test.ts @@ -12,7 +12,10 @@ const cwd = process.cwd(); const hasDnsRecord = async (hostname: string) => { for (const rr of ["A", "AAAA", "CNAME"]) { - if (await dns.resolve(hostname, rr)) { + if ( + await dns.resolve(hostname, rr) + .catch((_error) => false as const) + ) { return; } } From ef09a8ebd301488878c99e1a20dd7adb3722e5e2 Mon Sep 17 00:00:00 2001 From: HoJeong Go Date: Thu, 10 Apr 2025 15:32:44 +0900 Subject: [PATCH 9/9] chore: verbose error message --- test/filters.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/filters.test.ts b/test/filters.test.ts index f4633e5..a0b1379 100644 --- a/test/filters.test.ts +++ b/test/filters.test.ts @@ -19,7 +19,7 @@ const hasDnsRecord = async (hostname: string) => { return; } } - throw new Error("RECORD_NOT_FOUND"); + throw new Error("RECORD_NOT_FOUND: " + hostname); }; const doTest = (filePath: string) => {