UNPKG

@xyz/whois

Version:

A powerful TypeScript/JavaScript tool for comprehensive domain analysis, featuring detailed WHOIS data with registration dates, registrars, and domain status. Offers SSL certificate extraction (with PEM support), DNS records, and server details. Includes

137 lines (136 loc) 8.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../index"); // Using a real domain for testing const TEST_DOMAIN = 'example.com'; describe('fetchDomainInfo', () => { // Increase timeout for real network calls jest.setTimeout(30000); test('should return domain info for example.com', async () => { var _a, _b, _c, _d, _e, _f; const domainInfo = await (0, index_1.fetchDomainInfo)(TEST_DOMAIN); // Check the structure is correct expect(domainInfo).toHaveProperty('sslData'); expect(domainInfo).toHaveProperty('serverData'); expect(domainInfo).toHaveProperty('dnsData'); expect(domainInfo).toHaveProperty('httpStatus'); // Check general structure without specific values since they can change expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData).toHaveProperty('subject'); expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData).toHaveProperty('issuer'); expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData).toHaveProperty('valid'); expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData).toHaveProperty('validFrom'); expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData).toHaveProperty('validTo'); // Check new certificate fields (v2.2.0) expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData).toHaveProperty('certificate'); expect(typeof (domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.certificate)).toBe('string'); expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.certificate).toContain('-----BEGIN CERTIFICATE-----'); expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.certificate).toContain('-----END CERTIFICATE-----'); // Check intermediate and root certificates (might be undefined on some domains) expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData).toHaveProperty('intermediateCertificate'); if (domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.intermediateCertificate) { expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.intermediateCertificate).toContain('-----BEGIN CERTIFICATE-----'); expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.intermediateCertificate).toContain('-----END CERTIFICATE-----'); } expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData).toHaveProperty('rootCertificate'); if (domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.rootCertificate) { expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.rootCertificate).toContain('-----BEGIN CERTIFICATE-----'); expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.rootCertificate).toContain('-----END CERTIFICATE-----'); } // Check details object (v2.2.0) expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData).toHaveProperty('details'); expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.details).toHaveProperty('subject'); expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.details).toHaveProperty('issuer'); expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.details).toHaveProperty('validFrom'); expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.details).toHaveProperty('validTo'); // Ensure details.validFrom and validTo are Date objects expect(((_a = domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.details) === null || _a === void 0 ? void 0 : _a.validFrom) instanceof Date).toBe(true); expect(((_b = domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.details) === null || _b === void 0 ? void 0 : _b.validTo) instanceof Date).toBe(true); // Human-readable subject and issuer should be strings expect(typeof ((_c = domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.details) === null || _c === void 0 ? void 0 : _c.subject)).toBe('string'); expect(typeof ((_d = domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData.details) === null || _d === void 0 ? void 0 : _d.issuer)).toBe('string'); // Check DNS data expect(Array.isArray((_e = domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.dnsData) === null || _e === void 0 ? void 0 : _e.A)).toBe(true); expect((_f = domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.dnsData) === null || _f === void 0 ? void 0 : _f.A.length).toBeGreaterThan(0); // HTTP status should be valid expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.httpStatus).toBeGreaterThanOrEqual(200); expect(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.httpStatus).toBeLessThan(400); }); test('should pass custom options to requests', async () => { // Custom request options const options = { timeout: 5000, headers: { 'User-Agent': 'DomainInfoTest/1.0', }, }; const domainInfo = await (0, index_1.fetchDomainInfo)(TEST_DOMAIN, options); // Just verify call succeeds with custom options expect(domainInfo).toHaveProperty('sslData'); }); test('should throw error for empty domain', async () => { await expect((0, index_1.fetchDomainInfo)('')).rejects.toThrow('Domain name cannot be empty'); }); test('should throw error for invalid domain', async () => { await expect((0, index_1.fetchDomainInfo)('invalid')).rejects.toThrow('Invalid domain name'); }); }); describe('formatDomain', () => { test('should return domain without http and www', () => { const domain = (0, index_1.formatDomain)('https://www.google.com'); expect(domain).toBe('google.com'); }); test('should return domain without www', () => { const domain = (0, index_1.formatDomain)('www.google.com'); expect(domain).toBe('google.com'); }); test('should remove trailing slash', () => { const domain = (0, index_1.formatDomain)('example.com/'); expect(domain).toBe('example.com'); }); test('should convert to lowercase', () => { const domain = (0, index_1.formatDomain)('EXAMPLE.COM'); expect(domain).toBe('example.com'); }); test('should handle domains with subdomains', () => { const domain = (0, index_1.formatDomain)('sub.example.com'); expect(domain).toBe('sub.example.com'); }); }); describe('checkDomain', () => { test('should return true for valid domain', () => { expect((0, index_1.checkDomain)('google.com')).toBe(true); }); test('should return false for invalid domain', () => { expect((0, index_1.checkDomain)('invalid')).toBe(false); }); test('should return true for subdomain', () => { expect((0, index_1.checkDomain)('sub.example.com')).toBe(true); }); test('should return false for empty domain', () => { expect((0, index_1.checkDomain)('')).toBe(false); }); test('should return false for domain with empty parts', () => { expect((0, index_1.checkDomain)('.com')).toBe(false); }); }); describe('dateToTimestamp', () => { test('should convert date string to timestamp', () => { const date = new Date('2023-06-21'); const timestamp = (0, index_1.dateToTimestamp)(date.toString()); expect(timestamp).toBe(date.getTime()); }); test('should return NaN for invalid date string', () => { const timestamp = (0, index_1.dateToTimestamp)('invalid'); expect(timestamp).toBeNaN(); }); test('should handle different date formats', () => { // ISO format const isoDate = '2023-01-15T12:30:45.000Z'; const isoTimestamp = (0, index_1.dateToTimestamp)(isoDate); expect(isoTimestamp).toBe(new Date(isoDate).getTime()); // US format const usDate = 'Jan 15, 2023'; const usTimestamp = (0, index_1.dateToTimestamp)(usDate); expect(usTimestamp).toBe(new Date(usDate).getTime()); }); });