Skip to content

Commit 5827c21

Browse files
committed
Test URLs that are referenced
1 parent e3c0280 commit 5827c21

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

yaml-generation/bib.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414
ini_set('display_startup_errors', 1);
1515
error_reporting(E_ALL);
1616
define('NUMBER_LEVELS', 4);
17-
if (isset($_ENV["IS_IMPLEMENTED_WHEN_EVIDENCE"])) {
18-
$enforce=boolval($_ENV["IS_IMPLEMENTED_WHEN_EVIDENCE"]);
19-
define('IS_IMPLEMENTED_WHEN_EVIDENCE', $enforce);
20-
}else {
21-
define('IS_IMPLEMENTED_WHEN_EVIDENCE', false);
17+
defineConstFromEnv("IS_IMPLEMENTED_WHEN_EVIDENCE");
18+
defineConstFromEnv("TEST_REFERENCED_URLS");
19+
20+
21+
function defineConstFromEnv($envVar) {
22+
if (isset($_ENV[$envVar])) {
23+
$value = boolval($_ENV[$envVar]);
24+
define($envVar, $value);
25+
}else {
26+
define($envVar, false);
27+
}
2228
}
2329

2430
/**

yaml-generation/generateDimensions.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
$implementationReferences = readYaml($implementationReferenceFile)['implementations'];
166166
$references = array("implementations" => $implementationReferences);
167167
assertUniqueRefs($references, $errorMsg);
168+
assertLiveUrlsInRefs($references, $errorMsg);
168169

169170
resolveYamlReferences($dimensionsAggregated, $references, $errorMsg);
170171

@@ -215,6 +216,63 @@ function assertUniqueRefByKey($references, $keyToAssert, &$errorMsg) {
215216
}
216217

217218

219+
function assertLiveUrlsInRefs($all_references, &$errorMsg) {
220+
if (TEST_REFERENCED_URLS) {
221+
echo "\nTesting referenced URLs:\n";
222+
foreach ($all_references as $references) {
223+
foreach ($references as $key => $reference) {
224+
if (array_key_exists('url', $reference)) {
225+
$url = $reference['url'];
226+
echo " $key: $url\n";
227+
$err = assertLiveUrl($reference['url']);
228+
if ($err) {
229+
echo " # $err\n";
230+
array_push($errorMsg, "Dead ref URL ($key): $err");
231+
}
232+
}
233+
}
234+
}
235+
echo "\n";
236+
}
237+
}
238+
239+
240+
function assertLiveUrl($url):string {
241+
$useragent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0';
242+
243+
$curl = curl_init($url);
244+
curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
245+
curl_setopt($curl, CURLOPT_NOBODY, true);
246+
curl_setopt($curl, CURLOPT_HEADER, true);
247+
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
248+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
249+
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
250+
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
251+
$response = curl_exec($curl);
252+
253+
if (curl_errno($curl)) {
254+
echo curl_error($curl);
255+
curl_close($curl);
256+
return "No reply";
257+
}
258+
259+
// Extract header info
260+
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
261+
$redirectUrl = curl_getinfo($curl, CURLINFO_REDIRECT_URL );
262+
263+
curl_close($curl);
264+
265+
if ($statusCode == 200) {
266+
return "";
267+
}
268+
if ($statusCode == 301 || $statusCode == 302) {
269+
return "Status code $statusCode redirects to: $redirectUrl";
270+
}
271+
return "Status code: $statusCode: $url";
272+
}
273+
274+
275+
218276
/**
219277
*
220278
* @param unknown $dimensions

0 commit comments

Comments
 (0)