-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
155 lines (126 loc) · 5.03 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Let's cluster
var cluster = require( 'cluster' );
if ( cluster.isMaster ) {
var cpuCount = require( 'os' ).cpus().length;
console.log( 'number of clusters: ' + cpuCount + '\n');
for ( var i = 0; i < cpuCount; i += 1 ) {
cluster.fork();
}
cluster.on( 'exit', function( worker ) {
console.log( 'Worker %d died.', worker.id );
cluster.fork();
} );
} else {
// Basic HTTP Server to Validate Locust Test Construction
var express = require( 'express' );
var bodyParser = require( 'body-parser' );
var app = express();
var storedPayload = require( './payload.json' );
app.get( '/locust-test', function( req, res ) {
// event log for received GET request
console.log( 'get request received' );
console.log( 'clusterid: ' + cluster.worker.id );
console.log( req.path );
console.log( req.headers );
// response to client
res.status(200).send( 'Request received\n' + cluster.worker.id );
} );
app.get( '/locust-test-mobile', function( req, res ) {
// event log for received GET request
console.log( 'get request received' );
console.log( 'clusterid: ' + cluster.worker.id );
console.log( req.path );
console.log( req.headers );
// response to client
res.status(200).send( 'Request received\n' + cluster.worker.id );
} );
app.use( bodyParser.json() );
app.post( '/locust-test', function( req, res, next ) {
// event log of received POST request
console.log( 'post request received' );
console.log( 'clusterid: ' + cluster.worker.id );
console.log( req.path );
console.log( req.headers );
console.log( req.body );
if ( req.body.AnswerSetDefinitions.Id === storedPayload.AnswerSetDefinitions.Id &&
req.body.AnswerSetDefinitions.Name === storedPayload.AnswerSetDefinitions.Name &&
req.body.AnswerSetDefinitions.OptionDefinitions === storedPayload.AnswerSetDefinitions.OptionDefinitions ) {
// event log that POST data passes validation
console.log( 'true' );
// response to client
res.status( 200 ).send( 'POST data accepted\n' + cluster.worker.id );
} else {
// event log POST data passes validation
console.log( 'false' );
// response to client
res.status( 400 ).send( 'invalid request\n' + cluster.worker.id );
}
} );
app.post( '/locust-test-mobile', function( req, res, next ) {
// event log of received POST request
console.log( 'post request received' );
console.log( 'clusterid: ' + cluster.worker.id );
console.log( req.path );
console.log( req.headers );
console.log( req.body );
if ( req.body.AnswerSetDefinitions.Id === storedPayload.AnswerSetDefinitions.Id &&
req.body.AnswerSetDefinitions.Name === storedPayload.AnswerSetDefinitions.Name &&
req.body.AnswerSetDefinitions.OptionDefinitions === storedPayload.AnswerSetDefinitions.OptionDefinitions ) {
// event log that POST data passes validation
console.log( 'true' );
// response to client
res.status( 200 ).send( 'POST data accepted\n' + cluster.worker.id );
} else {
// event log POST data passes validation
console.log( 'false' );
// response to client
res.status( 400 ).send( 'invalid request\n' + cluster.worker.id );
}
} );
app.put( '/locust-test', function( req, res ) {
// event log of PUT request
console.log( 'put request received' );
console.log( 'clusterid: ' + cluster.worker.id );
console.log( req.path );
console.log( req.headers );
// response to client
res.status(200).send( 'Request received\n' + cluster.worker.id );
} );
app.put( '/locust-test-mobile', function( req, res ) {
// event log of PUT request
console.log( 'put request received' );
console.log( 'clusterid: ' + cluster.worker.id );
console.log( req.path );
console.log( req.headers );
// response to client
res.status(200).send( 'Request received\n' + cluster.worker.id );
} );
app.delete( '/locust-test', function( req, res ) {
// event log of DELETE request
console.log( 'delete request received' );
console.log( 'clusterid: ' + cluster.worker.id );
console.log( req.path );
console.log( req.headers );
// response to client
res.status(200).send( 'Request received\n' + cluster.worker.id );
} );
app.delete( '/locust-test-mobile', function( req, res ) {
// event log of DELETE request
console.log( 'delete request received' );
console.log( 'clusterid: ' + cluster.worker.id );
console.log( req.path );
console.log( req.headers );
// response to client
res.status(200).send( 'Request received\n' + cluster.worker.id );
} );
app.listen( 1337, function() {
console.log(
'***************************************\n' +
'app server is running on port 1337\n' +
'clusterid: ' + cluster.worker.id + '\n' +
'End Point:\n/locust-test\n\nMETHODS:\nGET\nPOST\nPUT\nDELETE' +
'\n\nEnd Point:\n/locust-test-mobile\n\nMETHODS:\nGET\nPOST\nPUT\nDELETE\n' +
'***************************************\n'
);
} );
}