Last active
March 19, 2019 10:58
-
-
Save bendtherules/27438fb610aef01df80554962722e97f to your computer and use it in GitHub Desktop.
Slow down getStatus request
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
// change to slow down get order status | |
module.exports = { | |
summary: 'slow down placing order (https)', | |
*beforeDealHttpsRequest(requestDetail) { | |
return true; | |
}, | |
*beforeSendRequest(requestDetail) { | |
const newRequestOptions = requestDetail.requestOptions; | |
// set rejectUnauthorized as false | |
newRequestOptions.rejectUnauthorized = false; | |
return { | |
requestOptions: newRequestOptions | |
}; | |
}, | |
*beforeSendResponse(requestDetail, responseDetail) { | |
if (requestDetail.url.startsWith('https://api-dev-2.smallcase.com/user/sc/orders')) { | |
console.log('Handling url: ' +JSON.stringify(requestDetail.url)); | |
const newResponse = responseDetail.response; | |
let timeoutMs = 0; | |
try { | |
const parsedJSON = JSON.parse(newResponse.body); | |
if (parsedJSON.data instanceof Array) { | |
const lastBatch = parsedJSON.data[0].lastBatch || parsedJSON.data[0].batches[0]; | |
if (lastBatch.status !== "PLACED"){ | |
timeoutMs = (5 + Math.round(Math.random()*30)) * 1000; | |
} | |
} | |
}catch (e) { | |
} | |
newResponse.header['DELAYED-BY-MS'] = timeoutMs.toString(); | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { // delay | |
resolve({ response: newResponse }); | |
}, timeoutMs); | |
}); | |
} | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment