@springmusk/who-is
Version:
Professional WHOIS data scraping module with structured parsing and batch processing capabilities
94 lines (79 loc) • 3.17 kB
JavaScript
const { Whois, WhoisParser, WhoisScrapingError, WhoisParsingError } = require('./index');
describe('WhoisParser', () => {
test('parseRawWhoisToJson should parse valid WHOIS data', () => {
const rawData = `
Domain Name: EXAMPLE.COM
Registry Domain ID: 2336799_DOMAIN_COM-VRSN
Registrar WHOIS Server: whois.example.com
Registrar URL: http://www.example.com
Updated Date: 2022-08-14T07:01:31Z
Creation Date: 1995-08-14T04:00:00Z
>>> Last update of whois database: 2023-07-24T05:45:32Z <<<
This is a disclaimer text.
`;
const result = WhoisParser.parseRawWhoisToJson(rawData);
expect(result).toEqual({
'Domain Name': 'EXAMPLE.COM',
'Registry Domain ID': '2336799_DOMAIN_COM-VRSN',
'Registrar WHOIS Server': 'whois.example.com',
'Registrar URL': 'http://www.example.com',
'Updated Date': '2022-08-14T07:01:31Z',
'Creation Date': '1995-08-14T04:00:00Z',
'lastWhoisUpdate': '2023-07-24T05:45:32Z',
'postWhoisDisclaimer': 'This is a disclaimer text.'
});
});
test('parseRawWhoisToJson should handle empty input', () => {
expect(() => {
WhoisParser.parseRawWhoisToJson('');
}).toThrow(WhoisParsingError);
});
test('parseRawWhoisToJson should handle invalid input type', () => {
expect(() => {
WhoisParser.parseRawWhoisToJson(null);
}).toThrow(WhoisParsingError);
});
});
describe('Whois', () => {
let scraper;
beforeEach(() => {
scraper = new Whois({
outputFormat: 'json',
http: {
timeout: 5000,
maxRetries: 1
}
});
});
test('should validate domain format', async () => {
await expect(scraper.scrape('')).rejects.toThrow(WhoisScrapingError);
await expect(scraper.scrape('invalid@domain')).rejects.toThrow(WhoisScrapingError);
await expect(scraper.scrape('domain-too-long'.repeat(30))).rejects.toThrow(WhoisScrapingError);
});
test('should handle batch scraping with empty input', async () => {
await expect(scraper.scrapeMultiple([])).rejects.toThrow(WhoisScrapingError);
});
test('should handle batch scraping with invalid input type', async () => {
await expect(scraper.scrapeMultiple('not-an-array')).rejects.toThrow(WhoisScrapingError);
});
test('should handle batch scraping configuration', async () => {
const domains = ['example.com', 'test.com'];
const options = { concurrent: 2, continueOnError: true };
// Mock the scrape method to avoid actual HTTP requests
scraper.scrape = jest.fn().mockImplementation((domain) =>
Promise.resolve({
domain,
scrapedAt: expect.any(String),
pageTitle: 'Mock Title',
registry: null,
registrar: null
})
);
const result = await scraper.scrapeMultiple(domains, options);
expect(result.summary.total).toBe(2);
expect(result.summary.successful).toBe(2);
expect(result.summary.failed).toBe(0);
expect(result.results).toHaveLength(2);
expect(scraper.scrape).toHaveBeenCalledTimes(2);
});
});