add DNS service test
This commit is contained in:
parent
23bdef3fbc
commit
af903ef430
2 changed files with 18 additions and 2 deletions
|
@ -1,8 +1,14 @@
|
|||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { DnsService } from './dns.service';
|
||||
import * as dns from 'dns';
|
||||
|
||||
jest.mock('dns', () => ({
|
||||
lookup: jest.fn(),
|
||||
}));
|
||||
|
||||
describe('DnsService', () => {
|
||||
let service: DnsService;
|
||||
let dnsLookupMock: jest.Mock;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
|
@ -10,9 +16,20 @@ describe('DnsService', () => {
|
|||
}).compile();
|
||||
|
||||
service = module.get<DnsService>(DnsService);
|
||||
dnsLookupMock = (dns.lookup as unknown) as jest.Mock;
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
it('should return true for a valid URL', async () => {
|
||||
dnsLookupMock.mockImplementation((hostname, callback) => callback(null, '1.1.1.1'));
|
||||
expect(await service.isValidUrl('http://example.com')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for an invalid URL', async () => {
|
||||
dnsLookupMock.mockImplementation((hostname, callback) => callback(new Error('not found'), null));
|
||||
expect(await service.isValidUrl('http://invalid-url.com')).toBe(false);
|
||||
});
|
||||
});
|
|
@ -7,7 +7,6 @@ export class DnsService {
|
|||
private lookup = util.promisify(dns.lookup);
|
||||
|
||||
async isValidUrl(url: string): Promise<boolean> {
|
||||
console.log(url)
|
||||
try {
|
||||
const hostname = new URL(url).hostname;
|
||||
await this.lookup(hostname);
|
||||
|
|
Loading…
Reference in a new issue