1
+ var webPush = require ( '../index' ) ;
1
2
var assert = require ( 'assert' ) ;
2
3
var fs = require ( 'fs' ) ;
3
4
var path = require ( 'path' ) ;
4
5
var fse = require ( 'fs-extra' ) ;
5
6
var temp = require ( 'temp' ) . track ( ) ;
6
7
var colors = require ( 'colors' ) ;
7
- var semver = require ( 'semver' ) ;
8
8
var childProcess = require ( 'child_process' ) ;
9
+ var http = require ( 'http' ) ;
10
+ var portfinder = require ( 'portfinder' ) ;
9
11
var net = require ( 'net' ) ;
10
12
var seleniumInit = require ( './selenium-init' ) ;
11
- var createServer = require ( './server' ) ;
12
- var webPush = require ( '../index.js' ) ;
13
13
14
14
if ( ! process . env . GCM_API_KEY ) {
15
15
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 ) ;
16
18
}
17
19
18
20
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) {
25
27
26
28
process . env . PATH = process . env . PATH + ':test_tools/' ;
27
29
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
+
28
115
function isPortOpen ( port ) {
29
116
return new Promise ( function ( resolve , reject ) {
30
117
var socket = new net . Socket ( ) ;
0 commit comments