Skip to content

Commit 7c84936

Browse files
authoredDec 22, 2020
fix: always send custom User-Agent (#210)
1 parent 217074e commit 7c84936

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed
 

‎src/index.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ interface CrawlOptions {
5959
rootPath: string;
6060
}
6161

62+
// Spoof a normal looking User-Agent to keep the servers happy
63+
export const headers = {
64+
'User-Agent':
65+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36',
66+
};
67+
6268
/**
6369
* Instance class used to perform a crawl job.
6470
*/
@@ -318,10 +324,7 @@ export class LinkChecker extends EventEmitter {
318324
res = await gaxios.request<string>({
319325
method: opts.crawl ? 'GET' : 'HEAD',
320326
url: opts.url.href,
321-
headers: {
322-
'User-Agent':
323-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36',
324-
},
327+
headers,
325328
responseType: opts.crawl ? 'text' : 'stream',
326329
validateStatus: () => true,
327330
timeout: opts.checkOptions.timeout,
@@ -332,10 +335,7 @@ export class LinkChecker extends EventEmitter {
332335
res = await gaxios.request<string>({
333336
method: 'GET',
334337
url: opts.url.href,
335-
headers: {
336-
'User-Agent':
337-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36',
338-
},
338+
headers,
339339
responseType: 'stream',
340340
validateStatus: () => true,
341341
timeout: opts.checkOptions.timeout,
@@ -358,6 +358,7 @@ export class LinkChecker extends EventEmitter {
358358
url: opts.url.href,
359359
responseType: 'text',
360360
validateStatus: () => true,
361+
headers,
361362
timeout: opts.checkOptions.timeout,
362363
});
363364
}

‎test/test.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as sinon from 'sinon';
55
import * as path from 'path';
66
import {describe, it, afterEach} from 'mocha';
77

8-
import {check, LinkState, LinkChecker, CheckOptions} from '../src';
8+
import {check, LinkState, LinkChecker, CheckOptions, headers} from '../src';
99

1010
nock.disableNetConnect();
1111
nock.enableNetConnect('localhost');
@@ -416,4 +416,24 @@ describe('linkinator', () => {
416416
/returned 0 results/
417417
);
418418
});
419+
420+
it('should always send a human looking User-Agent', async () => {
421+
const scopes = [
422+
nock('http://fake.local')
423+
.get('/', undefined, {reqheaders: headers})
424+
.replyWithFile(200, 'test/fixtures/local/index.html', {
425+
'Content-Type': 'text/html; charset=UTF-8',
426+
}),
427+
nock('http://fake.local')
428+
.get('/page2.html', undefined, {reqheaders: headers})
429+
.replyWithFile(200, 'test/fixtures/local/page2.html', {
430+
'Content-Type': 'text/html; charset=UTF-8',
431+
}),
432+
];
433+
const results = await check({
434+
path: 'http://fake.local',
435+
});
436+
assert.ok(results.passed);
437+
scopes.forEach(x => x.done());
438+
});
419439
});

0 commit comments

Comments
 (0)
Please sign in to comment.