From c6b957232fdc75ba7f2654ac3812ea23e6b433e4 Mon Sep 17 00:00:00 2001 From: tstibbs Date: Wed, 29 Jan 2020 20:54:40 +0000 Subject: [PATCH] Allowing the User-Agent header to be set --- src/_fetch/_request.js | 6 +++++- tests/fetch.test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/_fetch/_request.js b/src/_fetch/_request.js index ab1a180c..2e340791 100644 --- a/src/_fetch/_request.js +++ b/src/_fetch/_request.js @@ -1,15 +1,19 @@ const fetch = require('cross-fetch') const request = function(url, options) { + const fallbackUserAgent = 'Random user of the wtf_wikipedia library' let params = { method: 'GET', headers: { 'Content-Type': 'application/json', 'Api-User-Agent': + options['Api-User-Agent'] || + fallbackUserAgent, + 'User-Agent': options.userAgent || options['User-Agent'] || options['Api-User-Agent'] || - 'Random user of the wtf_wikipedia library' + fallbackUserAgent } } return fetch(url, params) diff --git a/tests/fetch.test.js b/tests/fetch.test.js index 1920f829..113f66f3 100644 --- a/tests/fetch.test.js +++ b/tests/fetch.test.js @@ -135,3 +135,31 @@ test('ambiguous-pageids', async function(t) { t.end(); }); */ + +test('intensive', t => { + /* fires a bunch of requests in parallel - this should be enough to get blocked by wikipedia if the user agent is not set correctly */ + var pages = [ + 'Mouse', + 'Rat', + 'Porcupine', + 'Chipmunk', + 'Vole', + 'Chinchilla', + 'Gopher', + 'Capybara', + 'Beaver', + 'Hamster' + ]; + t.plan(pages.length) + var promises = pages.map(page => wtf.fetch(page, 'en', { + 'Api-User-Agent': 'wtf_wikipedia test script - ' + })) + Promise.all(promises).then(results => { + results.forEach(result => { + t.ok(result.title(), 'got a page') + }) + t.end(); + }).catch((e) => { + t.error(e, e) + }) +})