forked from Azure/autorest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.js
378 lines (371 loc) · 14.8 KB
/
header.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
var express = require('express');
var router = express.Router();
var util = require('util');
var constants = require('../util/constants');
var utils = require('../util/utils');
var header = function (coverage, optionalCoverage) {
optionalCoverage['HeaderParameterProtectedKey'] = 0;
optionalCoverage['CustomHeaderInRequest'] = 0;
router.post('/param/:scenario', function (req, res, next) {
if (req.params.scenario === "existingkey") {
if (req.get("User-Agent") === "overwrite") {
coverage['HeaderParameterExistingKey']++;
res.status(200).end();
} else {
utils.send400(res, next, "Did not like scenario \"" + req.params.scenario + "\" with value " + req.get("User-Agent"));
}
} else if (req.params.scenario === "protectedkey") {
if (req.get("Content-Type") !== "text/html") {
optionalCoverage['HeaderParameterProtectedKey']++;
res.status(200).end();
} else {
utils.send400(res, next, "Did not like scenario \"" + req.params.scenario + "\" with value " + req.get("Content-Type"));
}
} else {
utils.send400(res, next, "Did not like scenario \"" + req.params.scenario);
}
});
router.post('/custom/x-ms-client-request-id/9C4D50EE-2D56-4CD3-8152-34347DC9F2B0', function (req, res, next) {
if (req.get("x-ms-client-request-id").toLowerCase() === "9C4D50EE-2D56-4CD3-8152-34347DC9F2B0".toLowerCase()) {
optionalCoverage['CustomHeaderInRequest']++;
res.status(200).end();
} else {
utils.send400(res, next, "Did not like client request id \"" + req.get("x-ms-client-request-id"));
}
});
router.post('/response/:scenario', function (req, res, next) {
if (req.params.scenario === "existingkey") {
coverage['HeaderResponseExistingKey']++;
res.status(200).set("User-Agent", "overwrite").end();
} else if (req.params.scenario === "protectedkey") {
coverage['HeaderResponseProtectedKey']++;
res.status(200).set("Content-Type", "text/html").end();
} else {
utils.send400(res, next, "Did not like scenario \"" + req.params.scenario);
}
});
router.post('/param/prim/:type', function (req, res, next) {
var scenario = req.get("scenario");
var value = req.get("value");
switch (req.params.type) {
case "integer":
if (scenario === "positive") {
if (parseInt(value) === 1) {
coverage['HeaderParameterIntegerPositive']++;
res.status(200).end();
break;
}
} else if (scenario === "negative") {
if (parseInt(value) === -2) {
coverage['HeaderParameterIntegerNegative']++;
res.status(200).end();
break;
}
}
utils.send400(res, next, "Did not like integer scenario \"" + scenario + "\" with value " + value);
case "long":
if (scenario === "positive") {
if (parseInt(value) === 105) {
coverage['HeaderParameterLongPositive']++;
res.status(200).end();
break;
}
} else if (scenario === "negative") {
if (parseInt(value) === -2) {
coverage['HeaderParameterLongNegative']++;
res.status(200).end();
break;
}
}
utils.send400(res, next, "Did not like long scenario \"" + scenario + "\" with value " + value);
case "float":
if (scenario === "positive") {
if (parseFloat(value) === 0.07) {
coverage['HeaderParameterFloatPositive']++;
res.status(200).end();
break;
}
} else if (scenario === "negative") {
if (parseFloat(value) === -3.0) {
coverage['HeaderParameterFloatNegative']++;
res.status(200).end();
break;
}
}
utils.send400(res, next, "Did not like float scenario \"" + scenario + "\" with value " + value);
case "double":
if (scenario === "positive") {
if (parseFloat(value) === 7e120) {
coverage['HeaderParameterDoublePositive']++;
res.status(200).end();
break;
}
} else if (scenario === "negative") {
if (parseFloat(value) === -3.0) {
coverage['HeaderParameterDoubleNegative']++;
res.status(200).end();
break;
}
}
utils.send400(res, next, "Did not like double scenario \"" + scenario + "\" with value " + value);
case "bool":
if (scenario === "true") {
if (value === "true") {
coverage['HeaderParameterBoolTrue']++;
res.status(200).end();
break;
}
} else if (scenario === "false") {
if (value === "false") {
coverage['HeaderParameterBoolFalse']++;
res.status(200).end();
break;
}
}
utils.send400(res, next, "Did not like bool scenario \"" + scenario + "\" with value " + value);
case "string":
if (scenario === "valid") {
if (value === "The quick brown fox jumps over the lazy dog") {
coverage['HeaderParameterStringValid']++;
res.status(200).end();
break;
}
} else if (scenario === "null") {
if (value === null || value === undefined) {
coverage['HeaderParameterStringNull']++;
res.status(200).end();
break;
}
} else if (scenario === "empty") {
if (value === "" || value === null || value === undefined) {
coverage['HeaderParameterStringEmpty']++;
res.status(200).end();
break;
}
}
utils.send400(res, next, "Did not like string scenario \"" + scenario + "\" with value " + value);
case "enum":
if (scenario === "valid") {
if (value === "GREY") {
coverage['HeaderParameterEnumValid']++;
res.status(200).end();
break;
}
} else if (scenario === "null") {
if (value === null || value === undefined) {
coverage['HeaderParameterEnumNull']++;
res.status(200).end();
break;
}
}
utils.send400(res, next, "Did not like string scenario \"" + scenario + "\" with value " + value);
case "date":
if (scenario === "valid") {
if (value === "2010-01-01") {
coverage['HeaderParameterDateValid']++;
res.status(200).end();
break;
}
} else if (scenario === "min") {
if (value === "0001-01-01") {
coverage['HeaderParameterDateMin']++;
res.status(200).end();
break;
}
}
utils.send400(res, next, "Did not like date scenario \"" + scenario + "\" with value " + value);
case "datetime":
if (scenario === "valid") {
if (utils.coerceDate(value) === "2010-01-01T12:34:56Z") {
coverage['HeaderParameterDateTimeValid']++;
res.status(200).end();
break;
}
} else if (scenario === "min") {
if (utils.coerceDate(value) === "0001-01-01T00:00:00Z") {
coverage['HeaderParameterDateTimeMin']++;
res.status(200).end();
break;
}
}
utils.send400(res, next, "Did not like datetime scenario \"" + scenario + "\" with value " + value);
case "datetimerfc1123":
if (scenario === "valid") {
if (value === "Fri, 01 Jan 2010 12:34:56 GMT") {
coverage['HeaderParameterDateTimeRfc1123Valid']++;
res.status(200).end();
break;
}
} else if (scenario === "min") {
if (value === "Mon, 01 Jan 0001 00:00:00 GMT" || value == "Mon, 01 Jan 1 00:00:00 GMT" || value == "Mon, 01 Jan 1 00:00:00 GMT") {
coverage['HeaderParameterDateTimeRfc1123Min']++;
res.status(200).end();
break;
}
}
utils.send400(res, next, "Did not like datetimerfc1123 scenario \"" + scenario + "\" with value " + value);
case "duration":
if (scenario === "valid") {
//For some reason moment.js doesn't quite get the right time value out (due to what looks like floating point issues)
//so we have to check for two possible times
if (value === "P123DT22H14M12.011S" || value == "P123DT22H14M12.010999999998603S") {
coverage['HeaderParameterDurationValid']++;
res.status(200).end();
break;
}
}
utils.send400(res, next, "Did not like duration scenario \"" + scenario + "\" with value " + value);
case "byte":
var bytes = new Buffer(constants.MULTIBYTE_BUFFER);
if (scenario === "valid") {
if (value === bytes.toString('base64')) {
coverage['HeaderParameterBytesValid']++;
res.status(200).end();
break;
}
}
utils.send400(res, next, "Did not like byte scenario \"" + scenario + "\" with value " + value);
default:
utils.send400(res, next, 'Must provide a valid primitive type.');
}
});
router.post('/response/prim/:type', function (req, res, next) {
var scenario = req.get("scenario");
var value = req.get("value");
switch (req.params.type) {
case "integer":
if (scenario === "positive") {
coverage['HeaderResponseIntegerPositive']++;
res.status(200).set('value', 1).end();
break;
} else if (scenario === "negative") {
coverage['HeaderResponseIntegerNegative']++;
res.status(200).set('value', -2).end();
break;
}
utils.send400(res, next, "Did not like integer scenario \"" + scenario + "\" with value " + value);
case "long":
if (scenario === "positive") {
coverage['HeaderResponseLongPositive']++;
res.status(200).set('value', 105).end();
break;
} else if (scenario === "negative") {
coverage['HeaderResponseLongNegative']++;
res.status(200).set('value', -2).end();
break;
}
utils.send400(res, next, "Did not like long scenario \"" + scenario + "\" with value " + value);
case "float":
if (scenario === "positive") {
coverage['HeaderResponseFloatPositive']++;
res.status(200).set('value', 0.07).end();
break;
} else if (scenario === "negative") {
coverage['HeaderResponseFloatNegative']++;
res.status(200).set('value', -3.0).end();
break;
}
utils.send400(res, next, "Did not like float scenario \"" + scenario + "\" with value " + value);
case "double":
if (scenario === "positive") {
coverage['HeaderResponseDoublePositive']++;
res.status(200).set('value', 7e120).end();
break;
} else if (scenario === "negative") {
coverage['HeaderResponseDoubleNegative']++;
res.status(200).set('value', -3.0).end();
break;
}
utils.send400(res, next, "Did not like double scenario \"" + scenario + "\" with value " + value);
case "bool":
if (scenario === "true") {
coverage['HeaderResponseBoolTrue']++;
res.status(200).set('value', true).end();
break;
} else if (scenario === "false") {
coverage['HeaderResponseBoolFalse']++;
res.status(200).set('value', false).end();
break;
}
utils.send400(res, next, "Did not like bool scenario \"" + scenario + "\" with value " + value);
case "string":
if (scenario === "valid") {
coverage['HeaderResponseStringValid']++;
res.status(200).set('value', "The quick brown fox jumps over the lazy dog").end();
break;
} else if (scenario === "null") {
coverage['HeaderResponseStringNull']++;
res.status(200).set('value', null).end();
break;
} else if (scenario === "empty") {
coverage['HeaderResponseStringEmpty']++;
res.status(200).set('value', "").end();
break;
}
utils.send400(res, next, "Did not like string scenario \"" + scenario + "\" with value " + value);
case "enum":
if (scenario === "valid") {
coverage['HeaderResponseEnumValid']++;
res.status(200).set('value', "GREY").end();
break;
} else if (scenario === "null") {
coverage['HeaderResponseEnumNull']++;
res.status(200).set('value', '').end();
break;
}
utils.send400(res, next, "Did not like enum scenario \"" + scenario + "\" with value " + value);
case "date":
if (scenario === "valid") {
coverage['HeaderResponseDateValid']++;
res.status(200).set('value', "2010-01-01").end();
break;
} else if (scenario === "min") {
coverage['HeaderResponseDateMin']++;
res.status(200).set('value', "0001-01-01").end();
break;
}
utils.send400(res, next, "Did not like date scenario \"" + scenario + "\" with value " + value);
case "datetime":
if (scenario === "valid") {
coverage['HeaderResponseDateTimeValid']++;
res.status(200).set('value', "2010-01-01T12:34:56Z").end();
break;
} else if (scenario === "min") {
coverage['HeaderResponseDateTimeMin']++;
res.status(200).set('value', "0001-01-01T00:00:00Z").end();
break;
}
utils.send400(res, next, "Did not like datetime scenario \"" + scenario + "\" with value " + value);
case "datetimerfc1123":
if (scenario === "valid") {
coverage['HeaderResponseDateTimeRfc1123Valid']++;
res.status(200).set('value', "Fri, 01 Jan 2010 12:34:56 GMT").end();
break;
} else if (scenario === "min") {
coverage['HeaderResponseDateTimeRfc1123Min']++;
res.status(200).set('value', "Mon, 01 Jan 0001 00:00:00 GMT").end();
break;
}
utils.send400(res, next, "Did not like datetimerfc1123 scenario \"" + scenario + "\" with value " + value);
case "duration":
if (scenario === "valid") {
coverage['HeaderResponseDurationValid']++;
res.status(200).set('value', "P123DT22H14M12.011S").end();
break;
}
utils.send400(res, next, "Did not like duration scenario \"" + scenario + "\" with value " + value);
case "byte":
var bytes = new Buffer(constants.MULTIBYTE_BUFFER);
if (scenario === "valid") {
coverage['HeaderResponseBytesValid']++;
res.status(200).set('value', bytes.toString('base64')).end();
break;
}
utils.send400(res, next, "Did not like byte scenario \"" + scenario + "\" with value " + value);
default:
utils.send400(res, next, 'Must provide a valid primitive type.');
}
});
};
header.prototype.router = router;
module.exports = header;