Skip to content

Commit

Permalink
added: tests for dns-resolvers.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiIgna committed Jan 22, 2025
1 parent 7e58d04 commit d6b7576
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/dns-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ export async function dnsRecordsGoogle(name: string, type: string = 'A'): Promis
const records: DnsRecord[] = (json.Answer || []).map((record: any) => {
const type = dnsTypeNumbers[record.type] || String(record.type)
let data = record.data
let name = record.name

if (['CNAME', 'NS'].includes(type) && data.endsWith('.')) {
data = data.slice(0, -1)
}

return {
name: record.name,
type,
ttl: record.TTL,
data,
if (name.endsWith('.')) {
name = name.slice(0, -1)
}

return { name, type, ttl: record.TTL, data }
})

return records
Expand Down
4 changes: 2 additions & 2 deletions src/subdomains.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// A, AAAA, CNAME records
// Common A, AAAA, CNAME records
export const subdomainsRecords: String[] = [
'admin',
'analytics',
Expand Down Expand Up @@ -37,7 +37,7 @@ export const subdomainsRecords: String[] = [
'webmail',
];

// common found TXT records
// Common TXT records
export const txtRecords: String[] = [
'_amazonses',
'_dmarc',
Expand Down
51 changes: 51 additions & 0 deletions test/dns-resolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { strict as assert } from 'node:assert'
import test from 'node:test'
import { isIPv4 } from 'node:net'

import { dnsRecordsCloudflare, dnsRecordsGoogle } from '../src/dns-resolvers.ts'

test('Cloudflare DNS resolver - A', async () => {
const aRecords = await dnsRecordsCloudflare('cloudflare.com', 'A')

assert.notEqual(aRecords.length, 0)

assert.equal(aRecords[0].name, 'cloudflare.com')
assert.equal(aRecords[0].type, 'A')
assert.ok(Number.isSafeInteger(aRecords[0].ttl))
assert.ok(isIPv4(aRecords[0].data))
})

test('Cloudflare DNS resolver - TXT', async () => {
const txtRecords = await dnsRecordsCloudflare('cloudflare.com', 'txt')

assert.notEqual(txtRecords.length, 0)

assert.equal(txtRecords[0].name, 'cloudflare.com')
assert.equal(txtRecords[0].type, 'TXT')
assert.ok(Number.isSafeInteger(txtRecords[0].ttl))
assert.ok(txtRecords[0].data)
})

test('Google DNS resolver - A', async () => {
const aRecords = await dnsRecordsGoogle('google.com', 'A')

console.log(aRecords)

assert.notEqual(aRecords.length, 0)

assert.equal(aRecords[0].name, 'google.com')
assert.equal(aRecords[0].type, 'A')
assert.ok(Number.isSafeInteger(aRecords[0].ttl))
assert.ok(isIPv4(aRecords[0].data))
})

test('Google DNS resolver - TXT', async () => {
const txtRecords = await dnsRecordsGoogle('google.com', 'txt')

assert.notEqual(txtRecords.length, 0)

assert.equal(txtRecords[0].name, 'google.com')
assert.equal(txtRecords[0].type, 'TXT')
assert.ok(Number.isSafeInteger(txtRecords[0].ttl))
assert.ok(txtRecords[0].data)
})

0 comments on commit d6b7576

Please sign in to comment.