Skip to content

Commit a612120

Browse files
committed
Merge pull request #156 from marco-c/remove_serverjs
Remove test/server.js file
2 parents 1341187 + 383d968 commit a612120

File tree

2 files changed

+90
-102
lines changed

2 files changed

+90
-102
lines changed

test/server.js

Lines changed: 0 additions & 99 deletions
This file was deleted.

test/testSelenium.js

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
var webPush = require('../index');
12
var assert = require('assert');
23
var fs = require('fs');
34
var path = require('path');
45
var fse = require('fs-extra');
56
var temp = require('temp').track();
67
var colors = require('colors');
7-
var semver = require('semver');
88
var childProcess = require('child_process');
9+
var http = require('http');
10+
var portfinder = require('portfinder');
911
var net = require('net');
1012
var seleniumInit = require('./selenium-init');
11-
var createServer = require('./server');
12-
var webPush = require('../index.js');
1313

1414
if (!process.env.GCM_API_KEY) {
1515
console.log('You need to set the GCM_API_KEY env variable to run the tests with Chromium.'.bold.red);
16+
} else {
17+
webPush.setGCMAPIKey(process.env.GCM_API_KEY);
1618
}
1719

1820
if (!process.env.VAPID_PRIVATE_KEY || !process.env.VAPID_PUBLIC_KEY) {
@@ -25,6 +27,91 @@ if (!process.env.VAPID_PRIVATE_KEY || !process.env.VAPID_PUBLIC_KEY) {
2527

2628
process.env.PATH = process.env.PATH + ':test_tools/';
2729

30+
function createServer(pushPayload, vapid) {
31+
var server = http.createServer(function(req, res) {
32+
if (req.method === 'GET') {
33+
if (req.url === '/') {
34+
req.url = '/index.html';
35+
}
36+
37+
if (!fs.existsSync('test' + req.url)) {
38+
res.writeHead(404);
39+
res.end(data);
40+
return;
41+
}
42+
43+
var data = fs.readFileSync('test' + req.url);
44+
45+
res.writeHead(200, {
46+
'Content-Length': data.length,
47+
'Content-Type': path.extname(req.url) === '.html' ? 'text/html' : 'application/javascript',
48+
});
49+
50+
res.end(data);
51+
} else {
52+
var body = '';
53+
54+
req.on('data', function(chunk) {
55+
body += chunk;
56+
})
57+
58+
req.on('end', function() {
59+
var obj = JSON.parse(body);
60+
61+
console.log('Push Application Server - Register: ' + obj.endpoint);
62+
63+
console.log('Push Application Server - Send notification to ' + obj.endpoint);
64+
65+
var promise;
66+
if (!pushPayload) {
67+
promise = webPush.sendNotification(obj.endpoint, {
68+
vapid: vapid,
69+
});
70+
} else {
71+
promise = webPush.sendNotification(obj.endpoint, {
72+
payload: pushPayload,
73+
userPublicKey: obj.key,
74+
userAuth: obj.auth,
75+
vapid: vapid,
76+
});
77+
}
78+
79+
promise
80+
.then(function() {
81+
console.log('Push Application Server - Notification sent to ' + obj.endpoint);
82+
})
83+
.catch(function(error) {
84+
console.log('Push Application Server - Error in sending notification to ' + obj.endpoint);
85+
console.log(error);
86+
})
87+
});
88+
89+
res.writeHead(200, {
90+
'Content-Type': 'application/json',
91+
'Access-Control-Allow-Origin': '*',
92+
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
93+
});
94+
95+
res.end('ok');
96+
}
97+
});
98+
99+
portfinder.getPort(function(err, port) {
100+
if (err) {
101+
server.port = 50005;
102+
} else {
103+
server.port = port;
104+
}
105+
server.listen(server.port);
106+
});
107+
108+
return new Promise(function(resolve, reject) {
109+
server.on('listening', function() {
110+
resolve(server);
111+
});
112+
});
113+
}
114+
28115
function isPortOpen(port) {
29116
return new Promise(function(resolve, reject) {
30117
var socket = new net.Socket();

0 commit comments

Comments
 (0)