How to use limiter method in wpt

Best JavaScript code snippet using wpt

ratelimiter.js

Source:ratelimiter.js Github

copy

Full Screen

1'use strict';2var should = require('should');3var sinon = require('sinon');4var RateLimiter = require('../lib/ratelimiter');5describe('RateLimiter', function() {6 describe('@constructor', function() {7 it('will instantiate without options', function() {8 var limiter = new RateLimiter();9 should.exist(limiter);10 });11 it('will instantiate without new', function() {12 /* jshint newcap:false */13 var limiter = RateLimiter();14 should.exist(limiter);15 });16 it('will instantiate with options', function() {17 var whitelist = [];18 var blacklist = [];19 var node = {};20 var limiter = new RateLimiter({21 node: node,22 whitelist: whitelist,23 blacklist: blacklist,24 limit: 1,25 interval: 1,26 whitelistLimit: 1,27 whitelistInterval: 1,28 blacklistLimit: 1,29 blacklistInterval: 130 });31 should.exist(limiter);32 should.exist(limiter.config);33 should.exist(limiter.clients);34 should.exist(limiter.node);35 limiter.whitelist.should.equal(whitelist);36 limiter.blacklist.should.equal(blacklist);37 limiter.config.whitelist.totalRequests.should.equal(1);38 limiter.config.whitelist.interval.should.equal(1);39 limiter.config.blacklist.totalRequests.should.equal(1);40 limiter.config.blacklist.interval.should.equal(1);41 limiter.config.normal.interval.should.equal(1);42 limiter.config.normal.totalRequests.should.equal(1);43 });44 });45 describe('#middleware', function() {46 it('will set ratelimit headers', function(done) {47 var limiter = new RateLimiter();48 var req = {49 headers: {50 'cf-connecting-ip': '127.0.0.1'51 }52 };53 var setHeader = sinon.stub();54 var res = {55 setHeader: setHeader56 };57 limiter.middleware()(req, res, function() {58 setHeader.callCount.should.equal(2);59 setHeader.args[0][0].should.equal('X-RateLimit-Limit');60 setHeader.args[0][1].should.equal(10800);61 setHeader.args[1][0].should.equal('X-RateLimit-Remaining');62 setHeader.args[1][1].should.equal(10799);63 done();64 });65 });66 it('will give rate limit error', function() {67 var node = {68 log: {69 warn: sinon.stub()70 }71 };72 var limiter = new RateLimiter({node: node});73 limiter.exceeded = sinon.stub().returns(true);74 var req = {75 headers: {76 'cf-connecting-ip': '127.0.0.1'77 }78 };79 var jsonp = sinon.stub();80 var status = sinon.stub().returns({81 jsonp: jsonp82 });83 var res = {84 status: status,85 setHeader: sinon.stub()86 };87 limiter.middleware()(req, res);88 status.callCount.should.equal(1);89 status.args[0][0].should.equal(429);90 jsonp.callCount.should.equal(1);91 jsonp.args[0][0].should.eql({92 status: 429,93 error: 'Rate limit exceeded'94 });95 });96 });97 describe('#exceeded', function() {98 it('should not be exceeded', function() {99 var node = {};100 var limiter = new RateLimiter({node: node});101 var client = limiter.addClient('127.0.0.1');102 var exceeded = limiter.exceeded(client);103 exceeded.should.equal(false);104 });105 it('should be exceeded', function() {106 var node = {};107 var limiter = new RateLimiter({node: node});108 var client = limiter.addClient('127.0.0.1');109 client.visits = 3 * 60 * 60 + 1;110 var exceeded = limiter.exceeded(client);111 exceeded.should.equal(true);112 });113 it('should exclude whitelisted with no limit', function() {114 var node = {};115 var limiter = new RateLimiter({116 whitelist: [117 '127.0.0.1'118 ],119 node: node,120 whitelistLimit: -1121 });122 var client = limiter.addClient('127.0.0.1');123 client.visits = Infinity;124 var exceeded = limiter.exceeded(client);125 exceeded.should.equal(false);126 });127 });128 describe('#getClientName', function() {129 it('should get client name from cloudflare header', function() {130 var node = {};131 var limiter = new RateLimiter({node: node});132 var req = {133 headers: {134 'cf-connecting-ip': '127.0.0.1'135 }136 };137 var name = limiter.getClientName(req);138 name.should.equal('127.0.0.1');139 });140 it('should get client name from x forwarded header', function() {141 var node = {};142 var limiter = new RateLimiter({node: node});143 var req = {144 headers: {145 'x-forwarded-for': '127.0.0.1'146 }147 };148 var name = limiter.getClientName(req);149 name.should.equal('127.0.0.1');150 });151 it('should get client name from connection remote address', function() {152 var node = {};153 var limiter = new RateLimiter({node: node});154 var req = {155 headers: {},156 connection: {157 remoteAddress: '127.0.0.1'158 }159 };160 var name = limiter.getClientName(req);161 name.should.equal('127.0.0.1');162 });163 });164 describe('#addClient', function() {165 var sandbox = sinon.createSandbox();166 afterEach(function() {167 sandbox.restore();168 });169 it('will remove client after interval', function() {170 var THREE_HOURS_PLUS = 3 * 60 * 60 * 1000 + 1;171 var clock = sandbox.useFakeTimers();172 var node = {};173 var limiter = new RateLimiter({node: node});174 limiter.addClient('127.0.0.1');175 should.exist(limiter.clients['127.0.0.1']);176 clock.tick(THREE_HOURS_PLUS);177 should.not.exist(limiter.clients['127.0.0.1']);178 });179 });...

Full Screen

Full Screen

rate_limiter.spec.ts

Source:rate_limiter.spec.ts Github

copy

Full Screen

1/* This Source Code Form is subject to the terms of the Mozilla Public2 * License, v. 2.0. If a copy of the MPL was not distributed with this3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */4import assert from "assert";5import type { SinonFakeTimers } from "sinon";6import sinon from "sinon";7import RateLimiter, { RateLimiterState } from "../../../../src/core/upload/rate_limiter";8const sandbox = sinon.createSandbox();9const now = new Date();10describe("RateLimiter", function() {11 let clock: SinonFakeTimers;12 beforeEach(function() {13 clock = sandbox.useFakeTimers(now.getTime());14 });15 afterEach(function () {16 clock.restore();17 });18 it("rate limiter correctly resets in case elapsed time return an error", function () {19 const rateLimiter = new RateLimiter(20 1000, /* interval */21 3, /* maxCount */22 );23 // Reach the count for the current interval.24 assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing });25 assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing });26 assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing });27 sinon.replaceGetter(rateLimiter, "elapsed", () => NaN);28 assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing });29 });30 it("rate limiter correctly resets in case interval is over", function () {31 const rateLimiter = new RateLimiter(32 1000, /* interval */33 3, /* maxCount */34 );35 // Reach the count for the current interval.36 assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing });37 assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing });38 assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing });39 // Fake the time passing over the current interval40 sinon.replaceGetter(rateLimiter, "elapsed", () => 1000 * 2);41 assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing });42 });43 it("rate limiter returns throttled state when it is throttled", function () {44 const rateLimiter = new RateLimiter(45 1000, /* interval */46 3, /* maxCount */47 );48 // Reach the count for the current interval.49 assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing });50 assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing });51 assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing });52 // Try one more time and we should be throttled.53 const nextState = rateLimiter.getState();54 assert.strictEqual(nextState.state, RateLimiterState.Throttled);55 assert.ok(nextState.remainingTime as number <= 1000 && nextState.remainingTime as number > 0);56 });...

Full Screen

Full Screen

Limiter.js

Source:Limiter.js Github

copy

Full Screen

1define(["Tone/component/Limiter", "helper/Basic", "helper/PassAudio", "helper/PassAudioStereo", "Test"], 2 function (Limiter, Basic, PassAudio, PassAudioStereo, Test) {3 describe("Limiter", function(){4 Basic(Limiter);5 context("Limiting", function(){6 it("handles input and output connections", function(){7 var limiter = new Limiter();8 Test.connect(limiter);9 limiter.connect(Test);10 limiter.dispose();11 });12 it("passes the incoming signal through", function(){13 return PassAudio(function(input){14 var limiter = new Limiter().toMaster();15 input.connect(limiter);16 });17 });18 it("passes the incoming stereo signal through", function(){19 return PassAudioStereo(function(input){20 var limiter = new Limiter().toMaster();21 input.connect(limiter);22 });23 });24 it("can be get and set through object", function(){25 var limiter = new Limiter();26 var values = {27 "threshold" : -30,28 };29 limiter.set(values);30 expect(limiter.get().threshold).to.be.closeTo(-30, 0.1);31 limiter.dispose();32 });33 it("can set the threshold", function(){34 var limiter = new Limiter();35 limiter.threshold.value = -10;36 expect(limiter.threshold.value).to.be.closeTo(-10, 0.1);37 limiter.dispose();38 });39 });40 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var api = new wpt('API_KEY');3var options = {4 videoParams: {5 },6 lighthouseConfig: {7 settings: {8 }9 },10 basicAuth: {11 },

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var limiter = wptools.limiter;3var get = wptools.get;4var page = wptools.page;5var limiter = wptools.limiter;6var limiter = wptools.limiter;7var limiter = wptools.limiter;8var limiter = wptools.limiter;9var limiter = wptools.limiter;10var limiter = wptools.limiter;11var limiter = wptools.limiter;12var limiter = wptools.limiter;13var limiter = wptools.limiter;14var limiter = wptools.limiter;15var limiter = wptools.limiter;16var limiter = wptools.limiter;17var limiter = wptools.limiter;

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var limiter = wptools.limiter;3limiter(1, 1000, function() {4});5var wptools = require('wptools');6var limiter = wptools.limiter;7limiter(1, 1000, function() {8});9var wptools = require('wptools');10var limiter = wptools.limiter;11limiter(1, 1000, function() {12});13var wptools = require('wptools');14var limiter = wptools.limiter;15limiter(1, 1000, function() {16});17var wptools = require('wptools');18var limiter = wptools.limiter;19limiter(1, 1000, function() {20});21var wptools = require('wptools');22var limiter = wptools.limiter;23limiter(1, 1000, function() {24});25var wptools = require('wptools');26var limiter = wptools.limiter;27limiter(1, 1000, function() {28});29var wptools = require('wptools');30var limiter = wptools.limiter;31limiter(1, 1000, function() {32});33var wptools = require('wptools');34var limiter = wptools.limiter;35limiter(1, 1000, function() {36});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var limiter = new wpt('www.webpagetest.org','A.2e2f9d3c3e4e4c1b4a8b4d1e1b1fcb6d');3var location = 'Dulles:Chrome';4limiter.runTest(url, { location: location, connectivity: 'Cable', firstViewOnly: true }, function(err, data) {5 if(err) {6 console.log(err);7 } else {8 console.log(data);9 }10});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful