12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import child_process from 'child_process';
- import tape from 'blue-tape';
- import http from 'http';
- import socketclient from 'socket.io-client';
- const serverhost = 'localhost',
- serverport = 2424,
- server = child_process.fork('build/server.js');
- tape.test('test server', function testServer(t) {
- let client;
- waitPro()
- .then(function done(a) {
- return requestPro(`http://${serverhost}:${serverport}`)
- .then(function done(a) {
- t.equal(a, 'yes', 'received message from server');
- })
- })
- .catch(function catchall(e) {
- console.log('error somewhere', e);
- t.fail('error somewhere');
- })
- .then(function done(a) {
- t.end();
- server.kill();
- })
- });
- function waitPro(n = 1000) {
- return new Promise(function pro(resolve, reject) {
- setTimeout(resolve, n);
- });
- }
- function requestPro(reqopts, data) {
- return new Promise(function prot(resolve, reject) {
- try {
- let req = http.request(reqopts, function received(resp) {
- let str = '';
- resp.on('data', function p(part) {
- str += part;
- });
- resp.on('end', function done() {
- if( resp.statusCode >= 400 && resp.statusCode <= 599 ) {
- reject(str);
- }
- else {
- resolve(str);
- }
- });
- });
- req.on('error', function err(ee) {
- reject(ee);
- });
- if( data ) {
- req.write(data);
- }
- req.end();
- }
- catch(e) {
- reject(e);
- }
- });
- }
- function responsePro(socket, evt, wait = 3000) {
- return new Promise(function pro(resolve, reject) {
- let to = setTimeout(reject.bind(null, 'socket event TIMEOUT ' + evt), wait);
- socket.once(evt, function (a) {
- clearTimeout(to);
- resolve(a);
- });
- });
- }
|