Skip to content

Commit e2ea6b1

Browse files
authored
support direct http and https urls in link annotations (fixes #480, via #1338)
1 parent ad8d68d commit e2ea6b1

3 files changed

Lines changed: 67 additions & 2 deletions

File tree

allure-java-commons/src/main/java/io/qameta/allure/util/ResultsUtils.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,11 @@ public static Link createLink(final String value, final String name,
636636
final String url, final String type) {
637637
final String resolvedName = firstNonEmpty(value).orElse(name);
638638
final String resolvedUrl = firstNonEmpty(url)
639-
.orElseGet(() -> getLinkUrl(resolvedName, type));
639+
.orElseGet(
640+
() -> isHttpOrHttpsUrl(resolvedName)
641+
? resolvedName
642+
: getLinkUrl(resolvedName, type)
643+
);
640644
return new Link()
641645
.setName(resolvedName)
642646
.setUrl(resolvedUrl)
@@ -847,6 +851,21 @@ private static String getLinkUrl(final String name, final String type) {
847851
return pattern.replaceAll("\\{}", Objects.isNull(name) ? "" : name);
848852
}
849853

854+
private static boolean isHttpOrHttpsUrl(final String value) {
855+
if (Objects.isNull(value)) {
856+
return false;
857+
}
858+
859+
try {
860+
final URI uri = URI.create(value);
861+
final String scheme = uri.getScheme();
862+
return nonNull(uri.getHost())
863+
&& ("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme));
864+
} catch (IllegalArgumentException ignored) {
865+
return false;
866+
}
867+
}
868+
850869
private static String getRealHostName() {
851870
if (Objects.isNull(cachedHost)) {
852871
try {

allure-java-commons/src/test/java/io/qameta/allure/ResultsUtilsTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,44 @@ public static Stream<Arguments> data() {
338338
return Stream.of(
339339
Arguments.of("a", "b", "c", "d", "e", link("a", "c", "d")),
340340
Arguments.of("a", "b", "c", "d", null, link("a", "c", "d")),
341+
Arguments.of(
342+
"https://example.org/path?query=value#fragment",
343+
null,
344+
null,
345+
"d",
346+
"pattern/{}/some",
347+
link(
348+
"https://example.org/path?query=value#fragment",
349+
"https://example.org/path?query=value#fragment",
350+
"d"
351+
)
352+
),
353+
Arguments.of(
354+
"http://example.org/path",
355+
null,
356+
null,
357+
"d",
358+
null,
359+
link("http://example.org/path", "http://example.org/path", "d")
360+
),
341361
Arguments.of("a", "b", null, "d", "invalid-pattern", link("a", "invalid-pattern", "d")),
342362
Arguments.of("a", "b", null, "d", "pattern/{}/some", link("a", "pattern/a/some", "d")),
363+
Arguments.of(
364+
"ftp://example.org/path",
365+
null,
366+
null,
367+
"d",
368+
"pattern/{}/some",
369+
link("ftp://example.org/path", "pattern/ftp://example.org/path/some", "d")
370+
),
371+
Arguments.of(
372+
"https:path-without-host",
373+
null,
374+
null,
375+
"d",
376+
"pattern/{}/some",
377+
link("https:path-without-host", "pattern/https:path-without-host/some", "d")
378+
),
343379
Arguments.of(null, null, null, "d", "pattern/{}/some", link(null, "pattern//some", "d")),
344380
Arguments.of(null, null, null, null, "pattern/{}/some", link(null, null, null)),
345381
Arguments.of(null, "b", null, "d", "pattern/{}/some/{}/and-more", link("b", "pattern/b/some/b/and-more", "d")),

allure-java-commons/src/test/java/io/qameta/allure/util/AnnotationUtilsTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ void shouldSupportCustomMultiLabelAnnotations() {
227227
name = "LINK-2",
228228
url = "https://example.org/link/2"
229229
),
230-
@Link(url = "https://example.org/some-custom-link")
230+
@Link(url = "https://example.org/some-custom-link"),
231+
@Link("https://example.org/direct-link"),
232+
@Link("http://example.org/direct-link"),
233+
@Link("ftp://example.org/pattern-link")
231234
}
232235
)
233236
@TmsLink("TMS-1")
@@ -275,6 +278,13 @@ void shouldExtractLinks() {
275278
tuple("LINK-1", "custom", "https://example.org/custom/LINK-1"),
276279
tuple("LINK-2", "custom", "https://example.org/link/2"),
277280
tuple("", "custom", "https://example.org/some-custom-link"),
281+
tuple("https://example.org/direct-link", "custom", "https://example.org/direct-link"),
282+
tuple("http://example.org/direct-link", "custom", "http://example.org/direct-link"),
283+
tuple(
284+
"ftp://example.org/pattern-link",
285+
"custom",
286+
"https://example.org/custom/ftp://example.org/pattern-link"
287+
),
278288
tuple("ISSUE-1", "issue", "https://example.org/issue/ISSUE-1"),
279289
tuple("ISSUE-2", "issue", "https://example.org/issue/ISSUE-2"),
280290
tuple("ISSUE-3", "issue", "https://example.org/issue/ISSUE-3"),

0 commit comments

Comments
 (0)