deno.land / x / google_play_scraper@v10.0.0 / test / utils.throttle.js
12345678910111213141516171819202122232425262728293031323334353637383940414243import requestLib from 'got';import throttled from '../lib/utils/throttle.js';import sinon from 'sinon';import { assert } from 'chai';
describe('Throttle tests', function () { this.timeout(6000); let server;
// Create a fake http server to emulate http call and responses. before(function () { server = sinon.fakeServer.create(); });
// Remove any server responses added in current test suite. after(function () { server.restore(); });
const url = 'https://yesno.wtf/api'; // Fake url used in this test, it could be anything.
it('Should make three requests with 2000ms interval. (Throttle function)', function () { // If we don't want to rely on the availability of a particular api we can use mocks. // The fake server intercept http calls and return specified objects if it mach the same method/url. server.respondWith('GET', url, JSON.stringify({ test: 'this works' })); const req = throttled(requestLib, { limit: 1, interval: 2000 }); return Promise.all([req({ url }), req({ url }), req({ url })]) .then((response) => response.map(req => new Date(req.headers.date).getTime())) .then((dates) => { const firstAndSecondReq = dates[1] - dates[0]; const secondAndThirdReq = dates[2] - dates[1];
assert.isAtLeast(firstAndSecondReq, 1000); assert.isAtMost(firstAndSecondReq, 3000); assert.isAtLeast(secondAndThirdReq, 1000); assert.isAtMost(secondAndThirdReq, 3000); }); });});
Version Info