mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-20 04:14:21 +08:00
chore(test): mock img server task
This commit is contained in:
@ -16,6 +16,60 @@ task('test.coverage', ['test.assembleVendorJs', 'compile.karma'], (done: Functio
|
||||
});
|
||||
});
|
||||
|
||||
task('test.imageserver', () => {
|
||||
const http = require('http');
|
||||
const url = require('url');
|
||||
const port = 8900;
|
||||
const requestedUrls = [];
|
||||
let start = Date.now();
|
||||
|
||||
function handleRequest(req, res) {
|
||||
const urlParse = url.parse(req.url, true);
|
||||
|
||||
if (urlParse.pathname === '/reset') {
|
||||
console.log('Image Server Reset');
|
||||
console.log('---------------------------');
|
||||
requestedUrls.length = 0;
|
||||
start = Date.now();
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
res.end('reset');
|
||||
return;
|
||||
}
|
||||
|
||||
const delay = urlParse.query.d || 1000;
|
||||
const id = urlParse.query.id || Math.round(Math.random() * 1000);
|
||||
const width = urlParse.query.w || 80;
|
||||
const height = urlParse.query.h || 80;
|
||||
const color = urlParse.query.c || 'yellow';
|
||||
|
||||
requestedUrls.push(req.url);
|
||||
|
||||
console.log(`id: ${id}, requested: ${requestedUrls.filter(f => f === req.url).length}, timestamp: ${Date.now() - start}`);
|
||||
|
||||
setTimeout(() => {
|
||||
res.setHeader('Content-Type', 'image/svg+xml');
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
res.end(`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
|
||||
style="background-color: ${color}; width: ${width}px; height: ${height}px;">
|
||||
<text x="5" y="22" style="font-family: Courier; font-size: 24px">${id}</text>
|
||||
</svg>`);
|
||||
}, delay);
|
||||
}
|
||||
|
||||
http.globalAgent.maxSockets = 1;
|
||||
|
||||
http.createServer(handleRequest).listen(port, () => {
|
||||
console.log(` Mock image server listening on: http://localhost:${port}/?d=2000&id=99`);
|
||||
console.log(` Possible querystrings:`);
|
||||
console.log(` id: the text to go in the svg image, defaults to a random number`);
|
||||
console.log(` d: how many milliseconds it should take to respond, defaults to 1000`);
|
||||
console.log(` w: image width, defaults to 80`);
|
||||
console.log(` h: image height, defaults to 80`);
|
||||
console.log(` c: image color, defaults to yellow`);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
function karmaTest(watch: boolean, done: Function) {
|
||||
const karma = require('karma');
|
||||
const argv = require('yargs').argv;
|
||||
|
Reference in New Issue
Block a user