1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import child_process from 'child_process';
- import tape from 'tape';
- import _test from 'tape-promise';
- import http from 'http';
- import socketclient from 'socket.io-client';
- const tapetest = _test(tape);
- const serverhost = 'localhost',
- serverport = 2424,
- server = child_process.fork('build/server.js');
- tapetest('test server', async function testServer(t) {
- let client;
- try {
- await waitPro();
- const data = await requestPro(`http://${serverhost}:${serverport}`);
- t.equal(data, 'working', 'received message from server');
- }
- catch(e) {
- console.log('error somewhere', e);
- t.fail('error somewhere');
- }
- 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);
- });
- });
- }
|