-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.js
1421 lines (1224 loc) · 46.6 KB
/
tests.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const ip = require('ip')
const moment = require('moment')
const request = require('request')
const { URL } = require('url')
const is = require('./is.js') // Test library
const report = require('./report.js').report // Logging
// TODO: The control flow of this code is difficult to follow because of
// the callbacks. The code runs tests in serial, so it should be modified
// to use async/await so control flow is clear.
exports.run = run
function run (opts, clientRequest, clientResponse) {
opts = setAndCheckOptions(opts, clientResponse)
// For simulating timeouts
// opts["metatimeout"] = 1;
// opts["datatimeout"] = 1;
let CLOSED = false
if (clientRequest) {
// If client closes connection, stop testing.
clientRequest.connection.on('close', function () { CLOSED = true })
}
// First object passed to report().
const r = { res: clientResponse, opts, infoAll: {}, dataAll1Body: {} }
let ignoreVersionError = false
if (opts.version) {
ignoreVersionError = true
}
// Catch uncaught exceptions. TODO: This will over-write any previous handler
// (will it?). Should move this to verifier.js.
process.on('uncaughtException', (err) => uncaughtException(err, clientResponse))
function finished () {
// All datasets have been checked.
if (r.catalogAll) {
// Restructure r.catalogAll to match r.catalog.
const catalogAll = {}
for (const idx in r.catalogAll.catalog) {
const id = r.catalogAll.catalog[idx].id
catalogAll[id] = r.catalogAll.catalog[idx].info
}
for (const id in r.infoAll) {
const rObj = is.InfoSame(catalogAll[id], r.infoAll[id], 'infoVsDepthAll')
report(r, opts.url + '/catalog?depth=all', rObj)
}
}
report(r)
}
if (opts.parameter) {
// Client wants to test one parameter.
capabilities()
} else {
// Test everything if no parameter given. (Technically, should perhaps not
// test everything if dataset given in URL. However, some servers have so
// many datasets that typically one only tests one dataset, so we may as
// well test everything.)
landing()
}
function landing () {
// Check optional landing page.
const timeoutString = timeoutCondition(landing, 'metadata')
const url = opts.url + '/'
report(r, url)
request(requestOptions(url, opts, timeoutString), function (err, res, body) {
landing.tries = landing.tries === undefined ? 1 : landing.tries + 1
if (err) {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true })
if (landing.tries === 1) {
landing() // Try again
} else {
landingRedirect()
}
return
}
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)))
report(r, url, is.HTTP200(res), { warn: true })
report(r, url, is.ContentType(/^text\/html/, res.headers['content-type']), { warn: true })
landingRedirect()
})
}
function landingRedirect () {
// Check that /hapi redirects to /hapi/
const timeoutString = timeoutCondition(landingRedirect, 'metadata')
const url = opts.url
report(r, url)
request(requestOptions(url, opts, timeoutString), function (err, res, body) {
landing.tries = landing.tries === undefined ? 1 : landing.tries + 1
if (err) {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true })
if (landing.tries === 1) {
landingRedirect() // Try again
} else {
capabilities()
}
return
}
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)))
report(r, url, is.HTTP302or200(res), { warn: true })
report(r, url, is.ContentType(/^text\/html/, res.headers['content-type']), { warn: true })
capabilities()
})
}
function checkCapabilitiesAndAbout (err, res, body, url, timeoutString, func, funcName) {
if (err) {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true })
if (func.tries === 0) {
func() // Try again
} else {
catalog()
}
return
}
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)))
report(r, url, is.ContentType(/^application\/json/, res.headers['content-type']))
report(r, url, is.CORSAvailable(res.headers), { warn: true })
report(r, url, is.LastModifiedGiven(res.headers), { warn: true })
if (!report(r, url, is.HTTP200(res), { stop: true })) {
catalog()
return
}
if (!report(r, url, is.JSONParsable(body), { stop: true })) {
catalog()
return
}
const json = JSON.parse(body)
versionCheckAndReport(r, url, opts, json.HAPI)
if (!report(r, url, is.HAPIJSON(body, version(opts, json.HAPI), funcName, ignoreVersionError), { stop: true })) {
catalog()
return
}
return json
}
function capabilities () {
if (CLOSED) { return }
const timeoutString = timeoutCondition(capabilities, 'metadata')
const url = opts.url + '/capabilities'
report(r, url)
request(requestOptions(url, opts, timeoutString), function (err, res, body) {
const json = checkCapabilitiesAndAbout(err, res, body, url, timeoutString, capabilities, 'capabilities')
if (json) {
r.capabilities = json
report(r, url, is.AllowedOutputFormat(json))
about()
}
})
}
function about () {
if (CLOSED) { return }
if (versionParts(r.capabilities.HAPI).major < 3) {
catalog()
return
}
const timeoutString = timeoutCondition(about, 'metadata')
const url = opts.url + '/about'
report(r, url)
request(requestOptions(url, opts, timeoutString), function (err, res, body) {
const json = checkCapabilitiesAndAbout(err, res, body, url, timeoutString, about, 'about')
if (json) {
r.about = json
catalog()
}
})
}
function catalog (depthCheck) {
if (CLOSED) { return }
const timeoutString = timeoutCondition(catalog, 'metadata')
let url = encodeURI(opts.url + '/catalog')
if (depthCheck !== undefined) {
url = url + '?depth=' + depthCheck
}
report(r, url)
request(requestOptions(url, opts, timeoutString), function (err, res, body) {
if (err) {
if (catalog.tries === 0) {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true })
catalog() // Try again
} else {
// Abort rest of server checks.
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { abort: true })
}
return
}
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)))
report(r, url, is.ContentType(/^application\/json/, res.headers['content-type']))
report(r, url, is.CORSAvailable(res.headers), { warn: true })
report(r, url, is.LastModifiedGiven(res.headers), { warn: true })
if (!report(r, url, is.HTTP200(res), { abort: true })) return
if (!report(r, url, is.JSONParsable(body), { abort: true })) return
const catalogObj = JSON.parse(body)
versionCheckAndReport(r, url, opts, catalogObj.HAPI)
if (depthCheck === undefined) {
r.catalog = catalogObj
r.datasetsToCheck = JSON.parse(JSON.stringify(catalogObj.catalog))
report(r, url, is.HAPIJSON(body, version(opts, catalogObj.HAPI), 'catalog', ignoreVersionError))
report(r, url, is.Unique(r.datasetsToCheck, 'datasets', 'id'))
r.datasetsToCheck = removeDuplicates(r.datasetsToCheck, 'id')
report(r, url, is.TooLong(r.datasetsToCheck, 'catalog', 'id', 'title', 40), { warn: true })
report(r, url, is.CIdentifier(r.datasetsToCheck, 'dataset id'), { warn: true })
r.datasetsToCheck = selectDatasets(r.datasetsToCheck, opts)
if (r.datasetsToCheck.length === 0) {
let desc = 'Dataset ' + opts.id + ' is not in catalog'
if (opts.id.startsWith('^')) {
desc = `<code>${opts.id} did not match any dataset id in catalog.`
}
const robj = { description: desc, error: true, got: 'to abort' }
if (!report(r, url, robj, { abort: true })) {
return
}
}
} else {
if (depthCheck === 'all') {
// r.catalogAll is compared with r.catalog after infoAll request.
r.catalogAll = catalogObj
}
}
if (r.capabilities.catalogDepthOptions) {
if (catalog.catalogDepthOptionsToCheck === undefined) {
// Copy
const toCheck = JSON.parse(JSON.stringify(r.capabilities.catalogDepthOptions))
// Remove 'dataset', which was already checked.
catalog.catalogDepthOptionsToCheck = toCheck.filter(el => el !== 'dataset')
}
}
if (catalog.catalogDepthOptionsToCheck && catalog.catalogDepthOptionsToCheck.length > 0) {
catalog(catalog.catalogDepthOptionsToCheck.shift())
return
}
if (opts.parameter) {
// 'parameter' given in client request
infoAll()
} else {
infoError()
}
})
}
function infoError () {
if (CLOSED) { return }
if (opts.parameter) {
infoAll()
}
const timeoutString = timeoutCondition(infoError, 'metadata')
const url = encodeURI(opts.url + '/info?id=' + 'a_test_of_an_invalid_id_by_verifier-nodejs')
report(r, url)
request(requestOptions(url, opts, timeoutString), function (err, res, body) {
if (err) {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true })
if (infoError.tries === 0) {
infoError() // Try again
} else {
infoAll()
}
return
}
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)))
report(r, url, is.ContentType(/^application\/json/, res.headers['content-type']))
report(r, url, is.ErrorCorrect(res.statusCode, 404, 'httpcode'))
report(r, url, is.StatusInformative(res.statusMessage, errors(1406), 'httpstatus'), { warn: true })
if (report(r, url, is.JSONParsable(body), { stop: true })) {
const json = JSON.parse(body)
versionCheckAndReport(r, url, opts, json.HAPI)
if (report(r, url, is.HAPIJSON(json, version(opts, json.HAPI), 'error', ignoreVersionError), { stop: true })) {
report(r, url, is.ErrorCorrect(json.status.code, 1406, 'hapicode'))
report(r, url, is.StatusInformative(json.status.message, 'HAPI error 1406', 'hapistatus'), { warn: true })
}
infoAll()
}
})
}
function nextDataset (r, currentId) {
if (r.datasetsToCheck.length > 1) {
r.datasetsToCheck.shift() // Remove first element
delete r.dataAll1Body[currentId]
infoAll() // Start next dataset
} else {
finished()
}
}
function infoAll () {
if (CLOSED) { return }
const datasets = r.datasetsToCheck
const id = datasets[0].id
const url = encodeURI(opts.url + '/info?id=' + datasets[0].id)
// Initialize object that stores additional info for this dataset.
r.catalog[id] = {}
const timeoutString = timeoutCondition(infoAll, 'metadata')
report(r, url)
if (infoAll.tries === undefined) {
plotLink(clientResponse, opts, id)
}
request(requestOptions(url, opts, timeoutString), function (err, res, body) {
if (err) {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true })
if (infoAll.tries === 0) {
infoAll() // Try again
} else {
nextDataset(r, id)
}
return
}
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)))
let rargs = { abort: true }
if (datasets.length > 1) {
// If more datasets to check, don't abort rest of server checks.
rargs = { stop: true }
}
if (!report(r, url, is.HTTP200(res), rargs)) {
nextDataset(r, id)
return
}
report(r, url, is.ContentType(/^application\/json/, res.headers['content-type']))
report(r, url, is.CORSAvailable(res.headers), { warn: true })
report(r, url, is.LastModifiedGiven(res.headers), { warn: true })
if (!report(r, url, is.JSONParsable(body), rargs)) {
nextDataset(r, id)
return
}
const json = JSON.parse(body)
r.infoAll[id] = json
versionCheckAndReport(r, url, opts, json.HAPI)
const hapiVersion = version(opts, json.HAPI)
report(r, url, is.HAPIJSON(json, hapiVersion, 'info', ignoreVersionError))
if (json.parameters) {
if (json.parameters[0].name) {
report(r, url, is.Unique(json.parameters, 'parameters', 'name'))
// json.parameters = removeDuplicates(header.parameters,'name');
} else {
const rObj = {
description: "Expect first parameter object to have a key 'name'",
error: true,
got: JSON.stringify(json.parameters[0])
}
report(r, url, rObj, { abort: true })
return
}
} else {
const rObj = {
description: 'Expect parameters element in catalog',
error: true,
got: json.parameters
}
report(r, url, rObj, { abort: true })
return
}
computeAndCheckStartStop(url, id)
report(r, url, is.FormatInHeader(json, 'nodata'))
report(r, url, is.FirstParameterOK(json, 'name'), { warn: true })
report(r, url, is.FirstParameterOK(json, 'fill'))
report(r, url, is.TimeIncreasing(json, '{start,stop}Date'))
report(r, url, is.TimeIncreasing(json, 'sample{Start,Stop}Date'))
report(r, url, is.DefinitionsOK(json))
if (opts.parameter) {
const tmp = selectOne(json.parameters, 'name', opts.parameter)
if (tmp.length !== 1) {
const rOpts = {
description: 'Parameter ' + opts.parameter + ' given in URL or on command line is not in parameter array returned by ' + url,
error: true,
got: 'To abort'
}
if (!report(r, url, rOpts, { abort: true })) {
return
}
}
}
for (let i = 0; i < json.parameters.length; i++) {
if (opts.parameter && (json.parameters[i].name !== opts.parameter)) {
continue
}
const len = json.parameters[i].length
const type = json.parameters[i].type
const name = json.parameters[i].name
let size = json.parameters[i].size
const fill = json.parameters[i].fill
const units = json.parameters[i].units
const label = json.parameters[i].label
const bins = json.parameters[i].bins
if (size === undefined) {
size = [1]
}
report(r, url, is.TimeParameterUnitsOK(name, units, type, size))
report(r, url, is.LengthAppropriate(len, type, name))
report(r, url, is.LabelOrUnitsOK(name, label, size, 'label', hapiVersion))
report(r, url, is.LabelOrUnitsOK(name, units, size, 'units', hapiVersion))
report(r, url, is.BinsLengthOK(name, bins, size, hapiVersion))
for (const d in size) {
report(r, url, is.BinsLabelOrUnitsOK(name, bins, size, d, 'labels', hapiVersion))
report(r, url, is.BinsLabelOrUnitsOK(name, bins, size, d, 'units', hapiVersion))
report(r, url, is.BinsCentersOrRangesOK(json.parameters, i, d, 'centers', hapiVersion))
report(r, url, is.BinsCentersOrRangesOK(json.parameters, i, d, 'ranges', hapiVersion))
}
report(r, url, is.FillOK(fill, type, len, name, type))
if (type === 'string') {
// Additional checks.
report(r, url, is.FillOK(fill, type, len, name, 'nullstring'))
report(r, url, is.FillOK(fill, type, len, name, 'stringparse'))
}
}
if (opts.parameter) {
// No need to check single parameter request, because already checked above.
dataAll1()
} else {
infoSingleParameter()
}
})
}
function infoSingleParameter () {
if (CLOSED) { return }
// Check if JSON response has two parameter objects when only
// one parameter is requested. Checks only the second parameter
// (first parameter after Time).
const datasets = r.datasetsToCheck
const id = datasets[0].id
const timeoutString = timeoutCondition(infoSingleParameter, 'metadata')
const infoAll = r.infoAll[id]
if (infoAll.parameters.length === 1) {
// Time is only parameter; can't do request for second parameter.
dataAll1()
return
}
let parameter = infoAll.parameters[1].name
if (opts.parameter !== '') {
for (let i = 0; i < infoAll.parameters.length; i++) {
if (infoAll.parameters[i].name === opts.parameter) {
parameter = infoAll.parameters[i].name
break
}
}
}
const url = encodeURI(opts.url + '/info' + '?id=' + id + '¶meters=' + parameter)
report(r, url)
request(requestOptions(url, opts, timeoutString), function (err, res, body) {
if (err) {
if (infoSingleParameter.tries === 0) {
// Try again
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true })
infoSingleParameter()
} else {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { stop: true })
dataAll1()
}
return
}
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)))
report(r, url, is.CORSAvailable(res.headers), { warn: true })
report(r, url, is.LastModifiedGiven(res.headers), { warn: true })
if (!report(r, url, is.HTTP200(res), { stop: true })) {
dataAll1()
return
}
report(r, url, is.ContentType(/^application\/json/, res.headers['content-type']))
if (!report(r, url, is.JSONParsable(body), { stop: true })) {
dataAll1()
return
}
const infoFirst = JSON.parse(body) // Reduced header
versionCheckAndReport(r, url, opts, infoFirst.HAPI)
const hapiVersion = version(opts, infoFirst.HAPI)
if (report(r, url, is.HAPIJSON(body, hapiVersion, 'info', ignoreVersionError))) {
if (infoFirst.parameters) {
if (infoFirst.parameters[0]) {
report(r, url,
{
description: 'Expect # parameters in JSON to be 2 when one non-time parameter is requested',
error: infoFirst.parameters.length !== 2,
got: infoFirst.parameters.length + ' parameters.'
})
} else {
report(r, url,
{
description: 'Cannot count # of parameters because parameters element is not an array.',
error: true,
got: 'Non-array parameter element.'
})
}
} else {
report(r, url,
{
description: 'Cannot count # of parameters because parameters element not found.',
error: true,
got: 'Missing parameter element.'
})
}
} else {
report(r, url,
{
description: 'Expect # parameters in JSON to be 2 when one non-time parameter is requested',
error: infoFirst.parameters.length !== 2,
got: infoFirst.parameters.length + ' parameters.'
})
}
report(r, url, is.InfoEquivalent(infoAll, infoFirst))
infoUsingDataset()
})
}
function infoUsingDataset () {
// Check that dataset=DATASET is supported if HAPI version >= 3.0
if (versionParts(r.catalog.HAPI).major < 3) {
dataAll1()
return
}
if (CLOSED) { return }
const datasets = r.datasetsToCheck
const id = datasets[0].id
const timeoutString = timeoutCondition(infoUsingDataset, 'metadata')
const url = encodeURI(opts.url + '/info?dataset=' + datasets[0].id)
report(r, url)
request(requestOptions(url, opts, timeoutString), function (err, res, body) {
if (err) {
if (infoUsingDataset.tries === 0) {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true })
infoUsingDataset() // Try again
} else {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { stop: true })
dataAll1()
}
return
}
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)))
report(r, url, is.CORSAvailable(res.headers), { warn: true })
report(r, url, is.LastModifiedGiven(res.headers), { warn: true })
if (!report(r, url, is.HTTP200(res), { stop: true })) {
dataAll1()
return
}
report(r, url, is.ContentType(/^application\/json/, res.headers['content-type']))
if (!report(r, url, is.JSONParsable(body), { stop: true })) {
dataAll1()
return
}
const infoAllUsingDataset = JSON.parse(body)
report(r, url, is.InfoSame(r.infoAll[id], infoAllUsingDataset, 'APIvsAPI'), { warn: true })
dataAll1()
})
}
function computeAndCheckStartStop (url, id) {
const json = r.infoAll[id]
let validCadence = false
const obj = is.CadenceValid(json.cadence)
if (!report(r, url, is.CadenceGiven(json.cadence), { warn: true })) {
report(r, url, obj)
}
validCadence = !obj.error
let start, stop, dataTimeout
if (opts.start && opts.stop) {
// start/stop given in verifier request URL
dataTimeout = 'datasamplechosen'
start = opts.start
stop = opts.stop
} else if (json.sampleStartDate && json.sampleStopDate) {
dataTimeout = 'datasamplesuggested'
start = json.sampleStartDate
stop = json.sampleStopDate
report(r, url, is.CadenceOK(json.cadence, start, stop, 'sampleStart/sampleStop'), { warn: true })
} else {
start = json.startDate
stop = json.stopDate
if (!start || !stop) {
const desc = 'Need at least startDate and stopDate or sampleStartDate and sampleStopDate to continue.'
report(r, url, { description: desc, error: true, got: 'To abort' }, { abort: true })
}
if (json.cadence && validCadence) {
dataTimeout = 'datasample10xcadence'
report(r, url, is.CadenceOK(json.cadence, start, stop, 'start/stop'))
const md = moment.duration(json.cadence)
stop = new Date(start).valueOf() + 10 * md._milliseconds
stop = new Date(stop).toISOString()
} else {
dataTimeout = 'datadefault'
// Use one day
const desc = 'Expect enough infoSingleParametermation to compute time.max to use for data sample tests.'
const got = 'No cadence and no sampleStartDate and sampleStopDate.' +
' Will use time.min = startDate and time.max = startDate + P1D.'
report(r, url, { description: desc, error: true, got }, { warn: true })
stop = new Date(start).valueOf() + 86400 * 1000
stop = new Date(stop).toISOString()
}
}
r.catalog[id].startToUse = start
r.catalog[id].stopToUse = stop
r.catalog[id].dataTimeout = dataTimeout
}
function dataAll1 () {
// Request all parameters using time format in request.
if (CLOSED) { return }
const datasets = r.datasetsToCheck
const id = datasets[0].id
const timeoutString = timeoutCondition(dataAll1, 'data')
const url = encodeURI(opts.url +
'/data?id=' + id +
'&time.min=' + r.catalog[id].startToUse +
'&time.max=' + r.catalog[id].stopToUse)
report(r, url)
const reqOpts = requestOptions(url, opts, timeoutString, true)
request(reqOpts, function (err, res, dataAll1Body) {
if (err) {
if (dataAll1.tries === 0) {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true })
dataAll1() // Try again
} else {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true, stop: true })
// Start checking individual parameters. Skip test using different
// time format (dataAll2()) and request with header (dataAllHeader()).
datar()
}
return
}
r.dataAll1Body[id] = dataAll1Body
function next () {
if (opts.parameter) {
datar()
} else {
dataAll2('alternateTimeFormat')
}
}
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)))
if (!report(r, url, is.HTTP200(res), { stop: true })) {
next()
return
}
if (!report(r, url, is.FileStructureOK(dataAll1Body, 'empty', res.statusMessage), { stop: true })) {
next()
return
}
report(r, url, is.CompressionAvailable(res.headers), { warn: true })
report(r, url, is.ContentType(/^text\/csv/, res.headers['content-type']))
report(r, url, is.CORSAvailable(res.headers), { warn: true })
if (!dataAll1Body || dataAll1Body.length === 0) {
next()
return
}
report(r, url, is.FileStructureOK(dataAll1Body, 'firstchar'))
report(r, url, is.FileStructureOK(dataAll1Body, 'lastchar'))
report(r, url, is.FileStructureOK(dataAll1Body, 'extranewline'))
report(r, url, is.FileStructureOK(dataAll1Body, 'numlines'))
report(r, url, is.NumberOfColumnsCorrect(r.infoAll[id], dataAll1Body))
next()
})
}
function dataAll2 (testName) {
if (!dataAll2[testName]) {
dataAll2[testName] = { tries: 0 }
}
// Same request as dataAll1() but with different time format.
// If dataAll1() used YMD, then dataAll2() uses YDOY and vice-versa.
if (CLOSED) { return }
const datasets = r.datasetsToCheck
const id = datasets[0].id
const timeoutString = timeoutCondition(dataAll2[testName], 'data')
let url = ''
if (testName === 'alternateTimeFormat') {
// switchTimeFormat converts YMD -> DOY or YDOY -> YMD
url = encodeURI(opts.url +
'/data?id=' + id +
'&time.min=' + switchTimeFormat(r.catalog[id].startToUse) +
'&time.max=' + switchTimeFormat(r.catalog[id].stopToUse))
}
if (testName === 'emptyParameters') {
url = encodeURI(opts.url +
'/data?id=' + id +
'¶meters=' +
'&time.min=' + switchTimeFormat(r.catalog[id].startToUse) +
'&time.max=' + switchTimeFormat(r.catalog[id].stopToUse))
}
if (testName === 'hapi3API') {
url = encodeURI(opts.url +
'/data?dataset=' + id +
'&start=' + switchTimeFormat(r.catalog[id].startToUse) +
'&stop=' + switchTimeFormat(r.catalog[id].stopToUse))
}
const reqOpts = requestOptions(url, opts, timeoutString, true)
request(reqOpts, function (err, res, dataAll2Body) {
if (err) {
if (dataAll2[testName].tries === 0) {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true })
dataAll2(testName) // Try again
} else {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true, stop: true })
// Start checking individual parameters. Skip test
// using different time format (dataAll2()) and request
// with header (dataAllHeader()).
datar()
}
return
}
function next () {
if (testName === 'alternateTimeFormat') {
dataAll2('emptyParameters')
} else if (testName === 'emptyParameters' && versionParts(r.catalog.HAPI).major > 3) {
dataAll2('hapi3API')
} else {
dataAllHeader()
}
}
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)))
if (!report(r, url, is.HTTP200(res), { stop: true })) {
next()
return
}
if (!report(r, url, is.FileStructureOK(dataAll2Body, 'empty', res.statusMessage), { stop: true })) {
next()
return
}
if (!dataAll2Body || dataAll2Body.length === 0) {
next()
return
}
report(r, url, is.FileContentSameOrConsistent(r.infoAll[id], dataAll2Body, r.dataAll1Body[id], 'same'))
next()
})
}
function dataAllHeader () {
// Same as dataAll1() but request with header.
if (CLOSED) { return }
const datasets = r.datasetsToCheck
const id = datasets[0].id
const timeoutString = timeoutCondition(dataAllHeader, 'data')
const url = encodeURI(opts.url +
'/data?id=' + id +
'&time.min=' + r.catalog[id].startToUse +
'&time.max=' + r.catalog[id].stopToUse +
'&include=header')
report(r, url)
const reqOpts = requestOptions(url, opts, timeoutString, true)
request(reqOpts, function (err, res, body) {
// TODO: Code below is very similar to that in dataAll1()
if (err) {
if (dataAllHeader.tries === 0) {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true })
dataAllHeader() // Try again
} else {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true, stop: true })
// Start next test
dataAll1201()
}
return
}
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)))
if (!report(r, url, is.HTTP200(res), { stop: true })) {
dataAll1201()
return
}
if (!report(r, url, is.FileStructureOK(body, 'empty', res.statusMessage), { stop: true })) {
dataAll1201()
return
}
if (!body || body.length === 0) {
dataAll1201()
return
}
// End similar code.
const ret = is.HeaderParsable(body)
if (report(r, url, ret, { stop: true })) {
report(r, url, is.FormatInHeader(ret.csvparts.header, 'data'))
report(r, url, is.InfoSame(r.infoAll[id], ret.csvparts.header, 'infoVsHeader'), { warn: true })
report(r, url, is.FileContentSameOrConsistent(r.infoAll[id], r.dataAll1Body[id], ret.csvparts.data, 'same'))
}
dataAll1201()
})
}
function dataAll1201 () {
// Attempt to create a HAPI 1201 response (no data in interval) by setting
// start time to be 1 ms after reported dataset start and stop time to be
// 2 ms after reported start.
if (CLOSED) { return }
const datasets = r.datasetsToCheck
const id = datasets[0].id
const timeoutString = timeoutCondition(dataAll1201, 'data')
const start = r.catalog[id].stopToUse
const stop = r.catalog[id].startToUse
let stop2 = r.catalog[id].stopToUse
let start2 = r.catalog[id].startToUse
// moment.js assumes local time if no trailing Z. Add trailing
// Z if it is not given. The trailingZfix function addresses
// case where stop2 is a date only, in which case moment.js
// does not accept a date only with a trailing Z and it is removed.
if (!start2.match(/Z$/)) {
start2 = start2 + 'Z'
}
if (!stop2.match(/Z$/)) {
stop2 = stop2 + 'Z'
}
start2 = moment(is.trailingZfix(start2)).add(1, 'ms').toISOString()
stop2 = moment(is.trailingZfix(start2)).add(2, 'ms').toISOString()
if (!start.match(/Z$/)) {
// If start did not have trailing Z, remove it from new start.
start2 = start2.slice(0, -1)
}
if (!stop.match(/Z$/)) {
// If stop did not have trailing Z, remove it from new stop.
stop2 = stop2.slice(0, -1)
}
const url = encodeURI(opts.url + '/data?id=' + id + '&time.min=' + start2 + '&time.max=' + stop2)
report(r, url)
const reqOpts = requestOptions(url, opts, timeoutString, true)
request(reqOpts, function (err, res, body) {
if (err) {
if (dataAll1201.tries === 0) {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true })
dataAll1201() // Try again
} else {
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)), { warn: true, stop: true })
// Start next check
datar()
}
return
}
report(r, url, is.RequestError(err, res, timeout(opts, timeoutString)))
if (!report(r, url, is.HTTP200(res), { stop: true })) {
datar()
return
}
// End similar code.
report(r, url, is.FileStructureOK(body, 'empty', res.statusMessage, true), { warn: true })
datar()
})
}
function datar (pn) {
// Reduced data request. Request one parameter at a time.
if (CLOSED) { return }
const datasets = r.datasetsToCheck
const id = datasets[0].id
const start = r.catalog[id].startToUse
const stop = r.catalog[id].stopToUse
if (pn === undefined) pn = 0
if (pn === -1 || pn === r.infoAll[id].parameters.length) {
// All parameters for dataset have been checked.
// -1 is case when one parameter given (opts["parameter"] === false)
nextDataset(r, id)
return
}
// TODO: This is contorted logic to check only one parameter. Need
// to rewrite. Also allow opts["parameter"] to be list of parameters.
let i = NaN
if (opts.parameter) {
for (i = 0; i < r.infoAll[id].parameters.length; i++) {
if (r.infoAll[id].parameters[i].name === opts.parameter) {
pn = i
break
}
}
}
const parameter = r.infoAll[id].parameters[pn].name
const timeoutString = timeoutCondition(datar, 'data')
const url = encodeURI(opts.url +
'/data?id=' + datasets[0].id +
'¶meters=' + parameter +
'&time.min=' + start +
'&time.max=' + stop)
if (!parameter) {
report(r, url,
{
description: 'Parameter #' + pn + ' does not have a name.',