From a4ab7cae8fcaa1c7d14d652d8032bf0f715038b0 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Tue, 29 Nov 2016 10:16:21 -0600 Subject: [PATCH] chore(test): mock img server task --- scripts/gulp/tasks/test.ts | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/scripts/gulp/tasks/test.ts b/scripts/gulp/tasks/test.ts index 2f5f0bdeed..8014b1201f 100644 --- a/scripts/gulp/tasks/test.ts +++ b/scripts/gulp/tasks/test.ts @@ -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(` + ${id} + `); + }, 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;