Skip to content

Commit

Permalink
Merge pull request #335 from tstibbs/bugfix/334-user-agent
Browse files Browse the repository at this point in the history
Allowing the User-Agent header to be set
  • Loading branch information
spencermountain authored Jan 30, 2020
2 parents ad965f4 + c6b9572 commit 43335fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/_fetch/_request.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
28 changes: 28 additions & 0 deletions tests/fetch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 - <spencermountain@gmail.com>'
}))
Promise.all(promises).then(results => {
results.forEach(result => {
t.ok(result.title(), 'got a page')
})
t.end();
}).catch((e) => {
t.error(e, e)
})
})

0 comments on commit 43335fb

Please sign in to comment.