-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathValidRelativeUrlsTest.java
More file actions
92 lines (76 loc) · 3.28 KB
/
ValidRelativeUrlsTest.java
File metadata and controls
92 lines (76 loc) · 3.28 KB
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
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import io.jenkins.plugins.designlibrary.UISample;
import java.util.List;
import java.util.stream.Stream;
import org.htmlunit.html.DomNode;
import org.htmlunit.html.HtmlAnchor;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
@WithJenkins
@TestInstance(PER_CLASS)
class ValidRelativeUrlsTest {
private JenkinsRule jenkins;
private List<UISample> samples;
private final List<String> otherUrls = List.of("llms.txt");
@BeforeAll
void beforeAll(JenkinsRule jenkins) {
this.jenkins = jenkins;
this.samples = jenkins.getInstance().getExtensionList(UISample.class);
}
/**
* Validate that relative URLs on a page actually link to a {@link UISample}
*/
@ParameterizedTest
@MethodSource("getPages")
void validRelativeUrls(String url) throws Exception {
try (var webClient = jenkins.createWebClient().withJavaScriptEnabled(false)) {
// We get a bunch of spam in our logs about missing CSS, let's ignore that
webClient.getOptions().setPrintContentOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
List<String> validUrls = Stream.concat(samples.stream().map(UISample::getUrlName), otherUrls.stream())
.toList();
var page = webClient.goTo(url);
var links = page.querySelectorAll(".jdl-section a");
if (links.isEmpty()) {
System.out.println("🤷 No URLs on: " + url);
return;
}
links.stream().filter(this::filterUrl).map(this::cleanseUrl).forEach(link -> {
System.out.println("🕵️ Checking URL: " + link);
assertThat(validUrls)
.withFailMessage(() -> "'" + link + "' isn't a valid URL, it needs to be one of:\n"
+ String.join("\n", validUrls))
.contains(link);
});
}
}
private Stream<String> getPages() {
Stream<String> homePage = Stream.of("design-library");
Stream<String> otherItems = samples.stream().map(e -> "design-library/" + e.getUrlName());
return Stream.concat(homePage, otherItems);
}
private boolean filterUrl(DomNode anchor) {
var url = ((HtmlAnchor) anchor).getHrefAttribute();
var response = !url.startsWith("http")
&& !url.startsWith("#")
&& !url.equals(".")
&& !url.equals("..")
&& !url.equals("twoColumn")
&& !url.equals("oneColumn")
&& !url.equals("fullscreen")
&& !url.startsWith("/jenkins/iconSize");
if (!response) {
System.out.println("🤫 Ignoring URL: " + url);
}
return response;
}
private String cleanseUrl(DomNode anchor) {
var url = ((HtmlAnchor) anchor).getHrefAttribute();
return url.replace("../", "");
}
}