-
Notifications
You must be signed in to change notification settings - Fork 1
/
GS1DigitalLinkResolverTestSuite.js
1630 lines (1491 loc) · 73.3 KB
/
GS1DigitalLinkResolverTestSuite.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 outputElement = 'gs1ResolverTests' // Set this to the id of the element in the document where you want the output to go
const resultProps = {
"id": "", //An id for the test
"test": "", // conformance statement from spec
"status": "fail", // (pass|fail|warn), default is fail
"msg": "", // Displayed to the end user
"url": "", // The URL we're going to fetch
"headers": {} // Ready for any headers we want to set
}
const linkProps = { // We'll need to test lots of links so it's good to have al their properties in an object
"href": "",
"rel": "",
"title": "",
"hreflang": "",
"type": ""
}
const perlTests = 'https://philarcher.org/cgi-bin/testHarness.pl'; // This is the helper application we call. It generally does a HEAD request and returns all the info we need in a JSON object. It's much easier to control and work with than just fetch. Ideally, yes, it should be on a gs1 domain.
// const RabinRegEx = /^(([^:\/?#]+):)?(\/\/((([^\/?#]*)@)?([^\/?#:]*)(:([^\/?#]*))?))?([^?#]*)(\?([^#]*))?(#(.*))?/;
const RabinRegEx = /^((https?):)(\/\/((([^\/?#]*)@)?([^\/?#:]*)(:([^\/?#]*))?))?([^?#]*)(\?([^#]*))?(#(.*))?/; // As above but specifically for HTTP(s) URIs
// (see https://www.w3.org/TR/powder-grouping/#rabinsRegEx for the origin of this regex by Jo Rabin)
// gives [2] scheme, [4] domain,[9] port, [10] path, [12] query, [14] fragment
// This is 'RE1' from the DL 1.2 spec
const plausibleDlURI = /^https?:(\/\/((([^\/?#]*)@)?([^\/?#:]*)(:([^\/?#]*))?))?([^?#]*)(((\/(01|gtin|8006|itip|8013|gmn|8010|cpid|414|gln|417|party|8017|gsrnp|8018|gsrn|255|gcn|00|sscc|253|gdti|401|ginc|402|gsin|8003|grai|8004|giai)\/)(\d{4}[^\/]+)(\/[^/]+\/[^/]+)?[/]?(\?([^?\n]*))?(#([^\n]*))?))/;
// And this is 'RE2' ('RE3' that attempts to look for a compressed DL URI is not used in the test suite).
const plausibleDlURINoAlphas = /^https?:(\/\/((([^\/?#]*)@)?([^\/?#:]*)(:([^\/?#]*))?))?([^?#]*)(((\/(01|8006|8013|8010|414|417|8017|8018|255|00|253|401|402|8003|8004)\/)(\d{4}[^\/]+)(\/[^/]+\/[^/]+)?[/]?(\?([^?\n]*))?(#([^\n]*))?))/;
const linkTypeListSource = 'https://gs1.github.io/WebVoc/current.jsonld';
// Global variables
let testList = [];
let resultsArray = [];
let gs1dlt = new GS1DigitalLinkToolkit(); // We'll make a lot of use of the GS1 Digital Link toolkit
// ***************************************************************************
// This is the main function that takes a Digital Link URI as input and creates the output.
// The second parameter can be used to switch individual tests on and off to
// test against the relevant version of the DL spec.
// ***************************************************************************
const testDL = async (dl, dlVersion) =>
{
clearGrid();
let domain; // We need to make tests on the domain name in the DL URI
// First we want to test whether the given input is a URL or not.
// Set up the a results object for testing whether what we have is a URL or not
let isURL = Object.create(resultProps);
isURL.id = 'isURL';
isURL.test = 'Not listed as a conformance criterion but the DL URI must be a valid URL';
isURL.msg = 'Given GS1 Digital Link URI is not a valid URL';
recordResult(isURL); // The default message is sent to the output. We'll update it if the test is passed.
// While we're at it, we'll set up the results object for whether it's a valid DL URI or not
let validDL = Object.create(resultProps);
validDL.id = 'validDL';
validDL.test = 'Given input must be a GS1 Digital Link URI';
validDL.msg = 'Given input is not a valid GS1 Digital link URI, no further testing is possible';
recordResult(validDL);
// And whether it's using HTTPS or not
let isHttps = Object.create(resultProps);
isHttps.id = 'isHttps';
isHttps.test = 'SHALL support HTTP Over TLS (HTTPS)';
isHttps.msg = 'Given GS1 Digital Link URI uses a scheme other than HTTPS';
isHttps.status = 'warn';
recordResult(isHttps);
// We will tolerate leading and training spaces but not spaces within the URL
dl = dl.replace(/(^\s+|\s+$)/g, ''); // Remove leading and trailing spaces
console.log('Given GS1 Digital Link URI is "' + dl + '"');
let UriElements = dl.match(RabinRegEx)
if (UriElements)
{
let scheme = UriElements[2];
domain = UriElements[4]; // Sets this global variable
if (((scheme === 'http') || (scheme === 'https')) && (domain.indexOf('.') !== -1))
{
isURL.msg = 'Given GS1 Digital Link URI is a valid URL';
isURL.status = 'pass';
recordResult(isURL);
// At this point we probably have a URL and we have its various elements,
} // End is it a URL
if (scheme === 'https')
{
isHttps.msg = 'Given GS1 Digital Link URI defines HTTPS as its scheme';
isHttps.status = 'pass';
recordResult(isHttps);
}
} // End is it a URI
let plausibleDL;
// if isHttps.status is pass or warn then we have a URL and we can probe further
if (isHttps.status !== 'fail')
{
plausibleDL = Object.create(resultProps);
plausibleDL.id = 'plausibleDL';
plausibleDL.test = 'Following GS1 Digital Link: URI syntax, we can check whether a URL plausibly is, or definitely is not, a DL URI';
plausibleDL.msg = 'Given URL does not conform to GS1 Digital Link URI syntax (uncompressed), no further tests are possible';
plausibleDL.status = 'fail';
if (plausibleDlURI.test(dl))
{
plausibleDL.status = 'pass';
plausibleDL.msg = 'URL under test plausibly is a GS1 Digital Link URI (uncompressed)';
if (!plausibleDlURINoAlphas.test(dl))
{
plausibleDL.status = 'warn';
plausibleDL.msg = 'URL under test plausibly is a GS1 Digital Link URI BUT uses convenience alphas which are being deprecated in favour of all-numeric AIs';
}
}
recordResult(plausibleDL);
}
// So now if plausibleDL.status is pass or warn, then we can pass it to the toolkit for a full check
if (plausibleDL.status !== 'fail')
{
try
{
let gs1Array = gs1dlt.extractFromGS1digitalLink(dl);
// You can get here with a variety of URLs including just a domain name and /gtin etc. So we need to check
// further Object returned has a GS1 object within it. Test for that using Mark's code
if (gs1dlt.buildStructuredArray(gs1Array.GS1).identifiers.length === 1)
{
validDL.status = 'pass';
validDL.msg = 'Given input is a valid GS1 Digital Link URI';
recordResult(validDL);
}
}
catch (err)
{
console.log('Error when extracting keys from given DL. Message is ' + err);
}
}
// If validDL.status is pass, we're good to go.
if (validDL.status === 'pass')
{
// We'll call a series of functions rather than putting everything here
// They return an object that normally goes into the asynch fetch array
TLSCheck(domain).then(); // This one doesn't push to the array
rdFileCheck(domain).then(); // Nor this one, so we don't need to wait either for them
//We'll wait for these run tests
await runTest(checkHttpVersion(domain));
await runTest(headerBasedChecks(dl, dlVersion));
await runTest(errorCodeChecks(dl));
await runTest(trailingSlashCheck(dl));
await runTest(compressionChecks(dl, domain, gs1dlt));
await runTest(testQuery(dl));
if (dlVersion === '1.1')
{
await runTest(jsonTests(dl));
await runTest(jsonLdTests(dl));
}
else
{
await runTest(testLinkset(dl));
await runTest(testJldContext(dl));
}
}
rotatingCircle(false);
// End validDL.status=='pass'
}
const TLSCheck = async (domain) =>
{
// This is designed to make sure that the server is available over TLS (i.e. using https works, even if the given
// DL is http) It does not handle a JSON response and therefore we don't use the promises array
//console.log('Domain is ' + domain);
let tlsOK = Object.create(resultProps);
tlsOK.id = 'tlsOK';
tlsOK.test = 'SHALL support HTTP Over TLS (HTTPS)';
tlsOK.msg = 'Resolver does not support HTTP over TLS';
recordResult(tlsOK);
// fetch('https://hintleshamandchattisham.onesuffolk.net/', { // Used for debugging, this is one of the few sites
// I know that doesn't support https!
try
{
let response = await fetch('https://' + domain, { method: 'HEAD', mode: 'no-cors' });
if (response.status >= 0)
{ // status is usually 0 for a site that supports https, I think 'cos we're using non-cors mode.
tlsOK.msg = 'Confirmed that server supports HTTP over TLS';
tlsOK.status = 'pass';
}
recordResult(tlsOK);
}
catch (error)
{
console.log('There has been a problem with your fetch operation when checking for TLS support: ', error.message);
}
return tlsOK;
}
const checkHttpVersion = (domain) =>
{
let httpVersion = Object.create(resultProps);
httpVersion.id = 'httpVersion';
httpVersion.test = 'SHALL support HTTP 1.1 (or higher)';
httpVersion.status = 'warn';
httpVersion.msg = 'HTTP version not detected. If other tests passed, it\'s probably OK';
httpVersion.url = perlTests + '?test=getHTTPversion&testVal=' + domain;
recordResult(httpVersion);
httpVersion.process = async (data) =>
{
let r = parseFloat(data.result);
if (r && r >= 1.1)
{
httpVersion.status = 'pass';
httpVersion.msg = 'Server at ' + domain + ' supports HTTP ' + r;
}
recordResult(httpVersion);
}
return httpVersion;
}
const headerBasedChecks = (dl, dlVersion) =>
{
// We'll perform a number of checks based on the headers returned from checking the DL directly
let corsCheck = Object.create(resultProps);
corsCheck.id = 'corsCheck';
corsCheck.test = 'SHALL support CORS';
corsCheck.msg = 'CORS headers not detected';
recordResult(corsCheck);
let methodsCheck = Object.create(resultProps);
methodsCheck.id = 'methodsCheck';
methodsCheck.test = 'SHALL support HTTP 1.1 (or higher) GET, HEAD and OPTIONS requests.';
methodsCheck.msg = 'At least one of GET, HEAD or OPTIONS not detected';
recordResult(methodsCheck);
// *************** Various tests around the links
let linkOnRedirect = Object.create(resultProps);
linkOnRedirect.id = 'linkOnRedirect';
linkOnRedirect.test = 'SHOULD expose the full list of links available to the client in an HTTP Link header when redirecting.';
linkOnRedirect.status = 'warn';
linkOnRedirect.msg = 'No link header detected when redirecting';
recordResult(linkOnRedirect);
let defaultLinkSet = Object.create(resultProps);
defaultLinkSet.id = 'defaultLinkSet';
defaultLinkSet.test = 'SHALL recognise one available linkType as the default for any given request URI and, within that, SHALL recognise one default link';
defaultLinkSet.msg = 'Default response does not redirect to any of the list of available links';
recordResult(defaultLinkSet);
let linkMetadata = Object.create(resultProps);
linkMetadata.id = 'linkMetadata';
linkMetadata.test = 'All links exposed SHALL include the target URL, the link relationship type (the linkType) and a human-readable title';
linkMetadata.msg = 'Incomplete link metadata';
recordResult(linkMetadata);
// We'll use the corsCheck object as the primary one here but will process the headers to look at the ones about
// the links too We need to get rid of any query string in the dl so we'll do that first
let u = stripQuery(dl);
corsCheck.url = perlTests + '?test=getAllHeaders&testVal=' + encodeURIComponent(u);
corsCheck.process = async (data) =>
{
// console.log('Looking for access control header ' + data.result['access-control-allow-origin']);
if (data.result['access-control-allow-origin'])
{
// That's probably enough tbh
corsCheck.status = 'pass';
corsCheck.msg = 'CORS headers detected';
recordResult(corsCheck);
}
if ((typeof data.result['access-control-allow-methods'] === 'string') &&
(((data.result['access-control-allow-methods'].indexOf('GET') > -1) &&
(data.result['access-control-allow-methods'].indexOf('HEAD') > -1)) &&
(data.result['access-control-allow-methods'].indexOf('OPTIONS') > -1)))
{ // We have our three allowed methods
methodsCheck.msg = 'GET, HEAD an OPTIONS methods declared to be supported';
methodsCheck.status = 'pass';
recordResult(methodsCheck);
}
if (data.result.link != null)
{ // we have a link header. We'll now test that each link has the required attributes
// The structure we're dealing with is:
// <url>; rel="val"; type="val"; hreflang="val"; title="val", {next}
// Tempting to split on the comma but titles can include commas, so we'll split on ", and then
// push a comma back on the end of each one
let allLinks = data.result.link.split(/",/);
for (let i in allLinks)
{
if (!allLinks.hasOwnProperty(i)) continue;
allLinks[i] += '"';
}
// We'll use a series of regular expressions to extract the relevant metadata for each link since order is
// unimportant RegExes are computationally expensive but performance is not a key issue here
let hrefRE = /^((https?):)(\/\/((([^\/?#]*)@)?([^\/?#:]*)(:([^\/?#]*))?))?([^?#]*)(\?([^#]*))?(#(.*))?/;
let relRE = /rel="(.*?)"/;
let titleRE = /title="(.*?)"/;
let hreflangRE = /hreflang="(.*?)"/;
let typeRE = /type="(.*?)"/;
let linkArray = [];
linkMetadata.status = 'pass'; // Assume pass and switch to fail if any of the SHALL tests fail
// We store each of the attributes in an object as we'll need this to run further tests on those links
for (let link in allLinks)
{
//Safety code when using for..in as some objects may have inherited parent properties
if (!allLinks.hasOwnProperty(link)) continue;
let linkObj = Object.create(linkProps);
linkObj.href = allLinks[link].substring(allLinks[link].indexOf('<') + 1, allLinks[link].indexOf('>'));
if (!hrefRE.test(linkObj.href))
{
linkMetadata.status = 'fail';
console.log('Link ' + link + ' failed on url which is ' + linkObj.href)
}
if (relRE.test(allLinks[link]))
{
linkObj.rel = relRE.exec(allLinks[link])[1]
}
else
{
linkMetadata.status = 'fail';
console.log('No link type (rel) declared for ' + linkObj.href + ' (link ' + link + ')')
}
if (titleRE.test(allLinks[link]))
{ // owl:sameAs doesn't need a title
linkObj.title = titleRE.exec(allLinks[link])[1]
}
else if (linkObj.rel !== 'owl:sameAs')
{
linkMetadata.status = 'fail';
console.log('No title given for ' + linkObj.href + ' (link ' + allLinks[link] + ')')
}
//Those are the SHALLs, now we'll record the others for future use
linkObj.hreflang = hreflangRE.exec(allLinks[link]) == null ? '' : hreflangRE.exec(allLinks[link])[1];
linkObj.type = typeRE.exec(allLinks[link]) == null ? '' : typeRE.exec(allLinks[link])[1];
// If we still have linkMetadata.status == 'pass' at this point, then we can go ahead and test that
// link in more detail
if ((linkMetadata.status === 'pass') && (linkObj.rel !== 'owl:sameAs'))
{
linkArray.push(linkObj)
}
}
// Now we want to set up tests for all the links in linkArray but only for testing version 1.1 (1.2 tests the links in the linkset)
if (dlVersion === '1.1')
{
for (let i in linkArray)
{
if (!linkArray.hasOwnProperty(i)) continue;
linkArray[i].id = 'link' + i;
linkArray[i].status = 'fail';
linkArray[i].msg = 'Requesting link type of ' + linkArray[i].rel + ' does not redirect to correct target URL (' + linkArray[i].href + ')';
linkArray[i].test = 'SHALL redirect to the requested linkType if available';
linkArray[i].url = perlTests + '?test=getAllHeaders&testVal=' + encodeURIComponent(u + '?linkType=' + linkArray[i].rel);
if (linkArray[i].hreflang !== '')
{ // We have a specific language to deal with
linkArray[i].headers = {'Accept-language': linkArray[i].hreflang};
}
recordResult(linkArray[i]);
linkArray[i].process = async (data) =>
{
// Strip the query strings before matching (should probably be more precise about this. Might be
// important info in target URL that we're missing)
let l = stripQuery(data.result.location);
let k = stripQuery(linkArray[i].href);
if (l === k)
{ // redirection target is correct
linkArray[i].msg = 'Requesting link type of ' + linkArray[i].rel;
if (linkArray[i].hreflang !== '')
{
linkArray[i].msg += ' (with language set to ' + linkArray[i].hreflang + ')';
}
linkArray[i].msg += ' redirects to correct target URL (' + linkArray[i].href + ')';
linkArray[i].status = 'pass';
}
}
}
// Now we can test those links one by one.
try
{
for (let test of linkArray) await runTest(test);
}
catch (error)
{
console.log('There has been a problem with your fetch operation for: ', error.message);
}
// Old code...
/*
linkArray.reduce(
(chain, d) => chain.then(() => runTest(d))
.catch((error) => console.log('There has been a problem with your fetch operation for: ', error.message)),
Promise.resolve()
);
*/
}
if (linkMetadata.status === 'pass')
{
linkMetadata.msg = 'Target URL and required metadata found for all links';
// Looking for redirect link
if (data.result.location != null)
{ // We have a redirect
for (let i = 0;
i < linkArray.length;
i++)
{
if (linkArray[i].href === data.result.location)
{
defaultLinkSet.msg = 'Default response is to redirect to one of the available links (' + linkArray[i].href + ') with a linkType of ' + linkArray[i].rel;
defaultLinkSet.status = 'pass';
}
}
if (data.result.link != null)
{ // We have a link header present when redirecting
linkOnRedirect.msg = 'Link headers present when redirecting';
linkOnRedirect.status = 'pass';
}
}
else
{
defaultLinkSet.msg = 'Default response is not to redirect so can\'t test that a given linkType is the default';
defaultLinkSet.status = 'warn';
linkOnRedirect.status = 'warn';
linkOnRedirect.msg = u + ' does not redirect so cannot test';
}
recordResult(defaultLinkSet)
}
else
{ // We have problems with the links, can't continue testing them
linkMetadata.msg = 'Target URL and/or required metadata not found for all links';
}
}
recordResult(linkMetadata);
recordResult(linkOnRedirect);
}
return corsCheck;
}
const errorCodeChecks = (dl) =>
{
// ******* Test for appropriate use of 400
let reportWith400 = Object.create(resultProps);
reportWith400.id = 'reportWith400';
reportWith400.test = 'SHALL extract and syntactically validate the URI and report errors with an HTTP response code of 400';
reportWith400.msg = 'Non-conformant GS1 Digital Link URI not reported with 400 error';
recordResult(reportWith400);
// ********** Also test not using 200 to report errors
let noErrorWith200 = Object.create(resultProps);
noErrorWith200.id = 'noErrorWith200';
noErrorWith200.test = 'A GS1 conformant resolver SHALL NOT use a 200 OK response code with a resource that indicates an error condition';
noErrorWith200.msg = 'Error reported with 200 OK';
recordResult(noErrorWith200);
// Let's create an error - we know we have a valid DL
reportWith400.url = perlTests + '?test=getAllHeaders&testVal=' + encodeURIComponent(stripQuery(dl) + '/foo');
reportWith400.process = async (data) =>
{
if (data.result.httpCode === '400')
{
reportWith400.msg = 'Non-conformant GS1 Digital Link URI reported with 400 error';
reportWith400.status = 'pass';
}
else
{
reportWith400.msg = 'Non-conformant GS1 Digital Link URI reported with ' + data.result.httpCode + ' error';
}
recordResult(reportWith400);
if (data.result.httpCode !== '200')
{
noErrorWith200.msg = 'Error not reported with 200 OK';
noErrorWith200.status = 'pass';
recordResult(noErrorWith200);
}
}
return reportWith400;
}
const trailingSlashCheck = (dl) =>
{
// Need to create a URI with and without a trailing slash
// We can dispense with any query string and create version with no trailing slash, then append slash to make the
// other
let noSlash = stripQuery(dl);
if (noSlash.lastIndexOf('/') + 1 === noSlash.length)
{
noSlash = noSlash.substring(0, noSlash.lastIndexOf('/'));
}
let slash = noSlash + '/';
let trailingSlash = Object.create(resultProps);
trailingSlash.id = 'trailingSlash';
trailingSlash.test = 'SHOULD tolerate trailing slashes at the end of GS1 Digital Link URIs, i.e. the resolver SHOULD NOT fail if one is present';
trailingSlash.msg = 'Resolver responds differently with or without a trailing slash';
trailingSlash.status = 'warn'; // This is a SHOULD not a SHALL so warn is as strong as we should be
recordResult(trailingSlash);
trailingSlash.url = perlTests + '?test=getAllHeaders&testVal=' + encodeURIComponent(noSlash);
trailingSlash.process = async (data) =>
{
try
{
let slashRequest = new Request(perlTests + '?test=getAllHeaders&testVal=' + encodeURIComponent(slash), {
method: 'get',
mode: 'cors'
})
let response = await fetch(slashRequest);
let slashJSON = await response.json();
if ((data.result.httpCode === slashJSON.result.httpCode) && (data.result.location === slashJSON.result.location))
{
trailingSlash.status = 'pass';
trailingSlash.msg = 'Response from server with and without trailing slash is identical'
recordResult(trailingSlash); // No need to update if we didn't get here
}
}
catch (error)
{
console.log('There has been a problem with your fetch operation for ' + trailingSlash.url + ': ', error.message);
}
}
return trailingSlash;
}
const compressionChecks = (dl, domain, gs1dlt) =>
{
// ******** test for decompression support************
// Basic method is to send the same request compressed and uncompressed, should get the same response except that
// compressed adds uncompressed to Link header
// First compression test checks whether HTTP response is the same and any redirect is the same
let validDecompressCheck = Object.create(resultProps);
validDecompressCheck.id = 'validDecompressCheck';
validDecompressCheck.test = 'SHALL be able to decompress a URI to generate a GS1 Digital Link URI';
validDecompressCheck.msg = 'Response from server for compressed and not-compressed URI not identical';
recordResult(validDecompressCheck);
// Second compression test checks whether resolver exposes uncompressed version in the link header
let exposeDecompressedLink = Object.create(resultProps);
exposeDecompressedLink.id = 'exposeDecompressedLink';
exposeDecompressedLink.test = 'If handling a compressed request URI, it SHALL expose the uncompressed URI in the Link response header with a rel value of owl:sameAs.';
exposeDecompressedLink.msg = 'Uncompressed URI not found in response link header when resolving a compressed URI';
recordResult(exposeDecompressedLink);
// Unlike the header-based tests, we preserve the query string for this test
// console.log('Uncompressed url is ' + perlTests + '?test=getAllHeaders&testVal=' + encodeURIComponent(dl));
validDecompressCheck.url = perlTests + '?test=getAllHeaders&testVal=' + encodeURIComponent(dl);
console.log(validDecompressCheck.url);
validDecompressCheck.process = async (data) =>
{
try
{
let compDL = gs1dlt.compressGS1DigitalLink(dl, false, 'https://' + domain, false, true, false);
// console.log('Compressed URI is ' + perlTests + '?test=getAllHeaders&testVal=' + encodeURIComponent(compDL));
const compressedRequest = new Request(perlTests + '?test=getAllHeaders&testVal=' + encodeURIComponent(compDL), {
method: 'get',
mode: 'cors'
});
let response = await fetch(compressedRequest);
let compressedJSON = await response.json();
// OK, we have our two JSON objects and we can look for key points of similarity
// We should get the same redirect or 200 for both compressed and not compressed
// console.log('comparing ' + data.result.httpCode + ' with ' + compressedJSON.result.httpCode);
if ((data.result.httpCode === compressedJSON.result.httpCode) && (data.result.location === compressedJSON.result.location))
{
validDecompressCheck.status = 'pass';
validDecompressCheck.msg = 'Response from server identical for compressed and uncompressed GS1 Digital link URI';
recordResult(validDecompressCheck);
}
// Now we're looking for the uncompressed version in the link header
let numericDL = numericOnly(dl);
console.log(`Looking for a numericDL of ${numericDL} in ${compressedJSON.result.link}`);
if (compressedJSON.result.link.indexOf(numericDL) > -1)
{ // It's in there, now we have to check for presence of owl:sameAs @rel
let allLinks = compressedJSON.result.link.split(',');
let i = 0;
while (allLinks[i].indexOf(numericDL) === -1)
{
i++
} // We know it's there somewhere because we just tested for it
let re = /(rel=.owl:sameAs)|(rel=.http:\/\/www.w3.org\/2002\/07\/owl#sameAs)/;
if (allLinks[i].search(re) !== -1)
{
exposeDecompressedLink.status = 'pass';
exposeDecompressedLink.msg = 'Uncompressed URI present in compressed URI response link header with @rel of owl:sameAs';
recordResult(exposeDecompressedLink);
}
else
{
exposeDecompressedLink.status = 'warn';
exposeDecompressedLink.msg = 'Uncompressed URI present in compressed URI response link header but without @rel of owl:sameAs';
recordResult(exposeDecompressedLink);
}
}
}
catch (error)
{
console.log('There has been a problem with your fetch operation for ' + compressedRequest.url + ' ( testing ' + compDL + '): ', error.message)
}
}
return validDecompressCheck;
}
// jsonTests only apply to DL version 1.1
const jsonTests = (dl) =>
{
let varyAccept = Object.create(resultProps);
varyAccept.id = 'varyAccept';
varyAccept.test = 'SHALL respond to a query parameter of linkType set to all by returning a list of links available to the client application. The list SHALL be available as JSON, SHOULD be available as JSON-LD and MAY be available in HTML and any other formats, served through content negotiation.';
varyAccept.msg = 'Vary response not found suggesting no content negotiation';
varyAccept.status = 'warn'; // This is a SHOULD not a SHALL so warn is as strong as we should be
recordResult(varyAccept);
// ************ When we test this, we can also see if we get JSON back if asked for it
let listAsJSON = Object.create(resultProps);
listAsJSON.id = 'listAsJSON';
listAsJSON.test = 'SHALL respond to a query parameter of linkType set to all by returning a list of links available to the client application. The list SHALL be available as JSON, SHOULD be available as JSON-LD and MAY be available in HTML and any other formats, served through content negotiation.';
listAsJSON.msg = 'List of links does not appear to be available as JSON';
recordResult(listAsJSON);
varyAccept.url = perlTests + '?test=getAllHeaders&testVal=' + encodeURIComponent(stripQuery(dl) + '?linkType=all');
varyAccept.headers = {'Accept': 'application/json'};
varyAccept.process = async (data) =>
{
if (data.result.vary != null)
{
// We have a vary header
// might be an array or a single value
if (data.result.vary[0] != null)
{ // We have an array
varyAccept.msg = 'Vary header detected, but does not include Accept suggesting no content negotiation for media type';
for (let k in data.result.vary)
{
if (!data.result.vary.hasOwnProperty(k)) continue;
if (data.result.vary[k] === 'Accept')
{
varyAccept.status = 'pass';
}
}
}
else if (data.result.vary === 'Accept')
{
varyAccept.status = 'pass';
}
if (varyAccept.status === 'pass')
{
varyAccept.msg = 'Vary response header includes Accept suggesting content negotiation for media type';
recordResult(varyAccept);
}
}
// Let's see if the content-type reported from that HEAD request is JSON
if (data.result['content-type'] === 'application/json')
{
listAsJSON.msg = 'Response media type is JSON';
listAsJSON.status = 'warn';
recordResult(listAsJSON);
// Media type says it's JSON, but is it? We need to test that directly
try
{
let response = await fetch(stripQuery(dl) + '?linkType=all', { headers: {'Accept': 'application/json'} });
let receivedJSON = await response.json();
listAsJSON.msg = 'JSON received with linkType=all';
listAsJSON.status = 'pass';
recordResult(listAsJSON);
}
catch (error)
{
listAsJSON.status = 'fail';
listAsJSON.msg = 'Asking for linkType=all did not return JSON';
recordResult(listAsJSON);
}
}
}
return varyAccept;
}
const jsonLdTests = (dl) =>
{
// ************ Test for JSON-LD
let listAsJSONLD = Object.create(resultProps);
listAsJSONLD.id = 'listAsJSONLD';
listAsJSONLD.test = 'SHALL respond to a query parameter of linkType set to all by returning a list of links available to the client application. The list SHALL be available as JSON, SHOULD be available as JSON-LD and MAY be available in HTML and any other formats, served through content negotiation.';
listAsJSONLD.msg = 'List of links does not appear to be available as JSON-LD';
listAsJSONLD.status = 'warn'; // This is a SHOULD so default is warn, not fail
recordResult(listAsJSONLD);
listAsJSONLD.url = perlTests + '?test=getAllHeaders&accept=jld&testVal=' + encodeURIComponent(stripQuery(dl) + '?linkType=all');
listAsJSONLD.process = (data) =>
{
// Let's see if the content-type reported from that HEAD request is JSON-LD
if (data.result['content-type'] === 'application/ld+json')
{
listAsJSONLD.msg = 'List of links appears to be available as JSON-LD';
listAsJSONLD.status = 'pass';
recordResult(listAsJSONLD);
}
}
return listAsJSONLD;
}
const rdFileCheck = async (domain) =>
{
// Checking that the Resolver Description File is available. Also want to check a few things about it.
let rdFile = Object.create(resultProps);
rdFile.id = 'rdFile';
rdFile.test = 'SHALL provide a resolver description file at /.well-known/gs1resolver';
rdFile.msg = 'Resolver Description File not found';
recordResult(rdFile);
try
{
let testRequest = new Request('https://' + domain + '/.well-known/gs1resolver', {
method: 'get',
mode: 'cors',
redirect: 'follow',
headers: new Headers({
'Accept': 'application/json'
})
});
let response = await fetch(testRequest);
let data = await response.json();
if (data['resolverRoot'].match(/^((https?):)(\/\/((([^\/?#]*)@)?([^\/?#:]*)(:([^\/?#]*))?))?([^?#]*)(\?([^#]*))?(#(.*))?/) && (data.supportedPrimaryKeys != null))
{
rdFile.msg = 'Resolver description file found with at least minimum required data';
rdFile.status = 'pass';
}
else
{
rdFile.msg = 'Resolver description file found but minimum required data not found';
}
recordResult(rdFile);
}
catch (error)
{
console.log('There has been a problem with your fetch operation: ', error.message);
}
}
const testQuery = (dl) =>
{
let qsPassedOn = Object.create(resultProps);
qsPassedOn.id = 'qsPassedOn';
qsPassedOn.test = 'By default, SHALL pass on all key=value pairs in the query string of the request URI (if present) when redirecting';
qsPassedOn.msg = 'Query string not passed on. If the query string is deliberately suppressed for this Digital Link URI, test another one where the default behaviour of passing on the query string applies';
qsPassedOn.status = 'warn';
recordResult(qsPassedOn);
let u = stripQuery(dl);
let query = 'foo=bar';
qsPassedOn.url = perlTests + '?test=getAllHeaders&testVal=' + encodeURIComponent(u + '?' + query);
qsPassedOn.process = async (data) =>
{
if (data.result.location !== undefined)
{ // There is a redirection
if (data.result.location.indexOf(query) > -1)
{ // Our query is being passed on
qsPassedOn.status = 'pass';
qsPassedOn.msg = 'Query passed on when redirecting';
}
}
else
{
qsPassedOn.msg = 'GS1 Digital Link URI not redirected so cannot test this feature';
}
recordResult(qsPassedOn);
}
return qsPassedOn;
}
// Linkset introduced in version 1.2
const testLinkset = (dl) =>
{
// We need to run several tests against the linkset. The first is the one we'll use to try and fetch the linkset.
// If all the tests pass, we'll record the linkset in the thisLinkset global variable
let validLinkset = true; // There are lots of places where this can be set to false, this is the only 'true' assignment
//Setup soleMember ready for test
let soleMember = Object.create(resultProps);
soleMember.id = 'soleMember';
soleMember.test = 'A set of links MUST be represented as a JSON object which MUST have "linkset" as its sole member.';
soleMember.msg = 'No linkset found or multiple members found';
soleMember.url = stripQuery(dl) + '?linkType=all';
soleMember.headers = {'Accept': 'application/linkset+json'};
recordResult(soleMember);
//Setup contextObjectArray ready for test
let contextObjectArray = Object.create(resultProps);
contextObjectArray.id = 'contextObjectArray';
contextObjectArray.test = 'The "linkset" member is an array in which a distinct JSON object - the "link context object" - MUST be used to represent links that have the same link context.';
contextObjectArray.msg = 'No array found';
// console.log("contextObjectArray = ", contextObjectArray);
recordResult(contextObjectArray);
//Setup contextObject ready for test
let contextObject = Object.create(resultProps);
contextObject.id = 'contextObject'
contextObject.test = 'Each link context object MUST have an "anchor" member with a value that represents the link context. The linkset standard allows a value of "" for anchor but GS1 Digital Link requires an absolute URI that follows the GS1 DL syntax (uncompressed)';
contextObject.msg = 'No anchor found';
recordResult(contextObject);
//Author test script for execution in runTest()
soleMember.process = async (data) =>
{
// data = dummy; // Used in debugging. See dummy linkset at the end of the file
try
{
soleMember.status = data.linkset && Array.isArray(data.linkset) ? 'pass' : 'fail';
if (soleMember.status === 'pass')
{
soleMember.msg = 'Linkset found as sole member';
recordResult(soleMember);
if (typeof(data.linkset) === "object" && typeof(data.linkset[0]) === "object")
{
contextObjectArray.status = 'pass';
contextObjectArray.msg = 'Array found';
recordResult(contextObjectArray);
}
if (typeof data.linkset[0].anchor === 'string')
{
contextObject.msg = 'Value for anchor found but is not a GS1 Digital Link URI (uncompressed)';
if (plausibleDlURI.test(data.linkset[0].anchor))
{
contextObject.status = 'pass';
contextObject.msg = 'GS1 Digital Link URI (uncompressed) found as anchor';
if (!plausibleDlURINoAlphas.test(data.linkset[0].anchor))
{
contextObject.status = 'warn';
contextObject.msg = 'GS1 Digital Link URI (uncompressed) found as anchor BUT using convenience alphas which are being deprecated in favour of all-numeric AIs';
}
}
recordResult(contextObject);
}
if (contextObject.status === 'fail')
{
validLinkset = false;
}
else
{
// This means we almost certainly have a linkset with a valid anchor
// Now we want to work through the linkset looking for link relation types
// Linkset says these can be registered IANA link relations or URIs.
// For this test, we'll ignore any link rel type that isn't a URI
// If it is a URI, pass if it's a GS1 link type, otherwise warn
// A URI MUST be followed by an array with one or more link target objects
// First we need to fetch the current set of GS1 link types from linkTypeListSource
let GS1LinkTypes = [];
let response = await fetch(linkTypeListSource, {headers: {'Accept': 'application/ld+json'}});
let lt = await response.json();
for (const entry of lt['@graph'])
{
if (entry['rdfs:subPropertyOf'] && entry['rdfs:subPropertyOf']['@id'] === 'gs1:linkType')
{
const linkTypeName = entry['@id'].replace('gs1:', '');
GS1LinkTypes.push({
title: entry['rdfs:label']['@value'],
description: entry['rdfs:comment']['@value'],
curie: `gs1:${linkTypeName}`,
url: `https://gs1.org/voc/${linkTypeName}`,
});
}
}
// console.log(GS1LinkTypes);
let count = 0;
let defaultLinkOK = false;
let defaultLinkObject = Object.create(resultProps);
defaultLinkObject.id = 'defaultLinkObject';
// defaultLinkObject.test = 'SHALL recognise one available link as the default for any given request URI.';
defaultLinkObject.test = 'SHALL recognise one available linkType as the default for any given request URI and, within that, SHALL recognise one default link which may be at a less granular level than the request URI';
defaultLinkObject.msg = 'No default link found';
recordResult(defaultLinkObject);
for (let i in data.linkset[0])
{
//Safety code in case object we are iterating over has inherited some unwanted parent properties:
if (!data.linkset[0].hasOwnProperty(i)) continue;
count++;
if (RabinRegEx.test(i))
{ // We're looking at a URI as a link rel type
let linkRelObject = Object.create(resultProps);
linkRelObject.id = 'linkRelObject' + count;
linkRelObject.test = 'If supporting multiple links per identified item, SHALL recognise the GS1 Web vocabulary namespace, noting its change management practice. A resolver SHOULD recognise the schema.org namespace. A resolver MAY recognise additional namespaces but link types defined elsewhere SHALL NOT duplicate link types available from GS1 and schema.org.';
// linkRelObject.test = 'If supporting multiple links per identified item, (resolvers) SHALL recognise the GS1 Web vocabulary namespace. A resolver MAY recognise additional namespaces (registered IANA link rel types are ignored in this test suite)';
linkRelObject.msg = 'Link relation type ' + i + ' is not recognised or proposed by GS1';
linkRelObject.status = 'warn';
recordResult(linkRelObject);
if (typeof GS1LinkTypes.find(x => x.url === i) === 'object')
{
linkRelObject.msg = 'Link relation type ' + i + ' recognised or proposed by GS1';
linkRelObject.status = 'pass';
recordResult(linkRelObject);
}
if ((i === 'https://gs1.org/voc/defaultLink') && (defaultLinkOK === false))
{ // So we haven't already got a default link.
defaultLinkOK = true; // We have found our default link
defaultLinkObject.status = 'pass';
defaultLinkObject.msg = 'Default link found';
recordResult(defaultLinkObject);
}
else if ((i === 'https://gs1.org/voc/defaultLink') && (defaultLinkOK))
{
// Multiple default links found, which is an error
defaultLinkObject.status = 'fail';
defaultLinkObject.msg = 'Duplicate default link found (perhaps you meant to use defaultLinkMulti?)';
recordResult(defaultLinkObject);
validLinkset = false;
}
let arrayOfTargetObjects = Object.create(resultProps);
arrayOfTargetObjects.id = 'arrayOfTargetObjects' + count;
arrayOfTargetObjects.test = 'Linkset requires there be an array of link target objects for every link relation, even if there is only one link target object.';
arrayOfTargetObjects.msg = 'No array, or invalid array of link target objects found for ' + i;
if (typeof data.linkset[0][i] === 'object')
{
if (checkTargetObjects(i, data.linkset[0][i]))
{
arrayOfTargetObjects.status = 'pass';
arrayOfTargetObjects.msg = 'Array of target objects found for ' + i;
}
else
{
validLinkset = false;
}
}
recordResult(arrayOfTargetObjects);
}
}
if (defaultLinkOK === false)
{
validLinkset = false
}
if (validLinkset)
{
await testLinksInLinkset(dl, data.linkset[0])
}
}
}
} // End if soleMember.status === 'pass', i.e. we probably have some sort of linkset
catch (error)
{
console.log('There has been a problem with your fetch operation: ', error.message);
}
}
return soleMember;
}
function checkTargetObjects(lt, a)
{
let counter = 0;
let returnValue = true;
let langRegEx = /^[a-z]{2}($|-[A-Z]{2})/; // We'll assume that if a language tag matches this, it's a valid value.
// We won't fetch the list of valid values and check against that.
let typeRegEx = /^[a-z]{4,}\//; // Again, not checking against IANA list but must begin with at least 4
// lower case letters followed by a slash
for (let target in a)
{
//Safety code when using for..in as some objects may have inherited parent properties
if (!a.hasOwnProperty(target))
{
continue;
}
counter++;
let targetObj = Object.create(resultProps);
targetObj.id = 'targetObj' + lt + counter;
targetObj.test = 'All links exposed SHALL include the target URL, the link relationship type (the link type) and a human-readable title';
targetObj.msg = 'Target link minimum requirements not met';
// We know we have a link rel type or we wouldn't be here. So we just need to check the other two
if ((typeof a[target].href === 'string') && (RabinRegEx.test(a[target].href)))
{ // we have a target URL. Now to check the title - which can come in two ways (that are not exclusive)
let myTitle = '';
if ((typeof a[target].title === 'string') && (a[target].title !== ""))
{
myTitle = a[target].title;
}
if (Array.isArray(a[target]["title*"]) && typeof a[target]["title*"][0].value === 'string' && a[target]["title*"][0].value !== "")
{
for (const t of a[target]["title*"])
{
myTitle += `, ${t.value} (${t.language})`;
}