Skip to content

Commit 898c7de

Browse files
committed
Fixed regex and corresponding test case
1 parent e2458ef commit 898c7de

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

http/http-api/src/main/java/com/okta/commons/http/HttpHeaders.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,14 @@ public Map<String, String> getLinkMap() {
515515
List<String> headerValues = headers.get(LINK);
516516
return headerValues.stream()
517517
.map(HttpHeaders::parseLinkHeader)
518+
.filter(link -> link != null)
518519
.collect(Collectors.toMap(Link::getRelationType, Link::getHref));
519520
}
520521
return Collections.emptyMap();
521522
}
522523

523524
private static Link parseLinkHeader(String rawHeader) {
524-
Pattern pattern = Pattern.compile("\\<(.*)\\>;.*rel=\"(.*)\"");
525+
Pattern pattern = Pattern.compile("<(.*)>;.*rel=\"?([^;|,|\"]*)\"?.*");
525526
Matcher matcher = pattern.matcher(rawHeader);
526527
if (matcher.matches()) {
527528
return new DefaultLink(matcher.group(2), matcher.group(1));

http/http-api/src/test/groovy/com/okta/commons/http/HttpHeadersTest.groovy

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,16 @@ class HttpHeadersTest {
413413
def headers = new HttpHeaders()
414414
headers.add("Link", "<https://example.com/api/v1/users?limit=200>; rel=\"self\"")
415415
headers.add("Link", "<https://dev-259824.oktapreview.com/api/v1/users?after=200u9wv2af0FYl791n0h7&limit=200>; rel=\"next\"")
416+
headers.add("Link", "<meta.rdf>; rel=meta; as=meta")
417+
headers.add("Link", "<random.link>;")
418+
headers.add("Link", "<https://dev-259824.oktapreview.com/_sec/cp_challenge/sec-3-8.css>; as=style; rel=\"preload\"")
416419

417420
def result = headers.getLinkMap()
418421
assertThat(result.get("self"), equalToObject("https://example.com/api/v1/users?limit=200"))
419422
assertThat(result.get("next"), equalToObject("https://dev-259824.oktapreview.com/api/v1/users?after=200u9wv2af0FYl791n0h7&limit=200"))
420-
assertThat(result, aMapWithSize(2))
423+
assertThat(result.get("meta"), equalToObject("meta.rdf"))
424+
assertThat(result.get("preload"), equalToObject("https://dev-259824.oktapreview.com/_sec/cp_challenge/sec-3-8.css"))
425+
assertThat(result, aMapWithSize(4))
421426
}
422427

423428
@Test

0 commit comments

Comments
 (0)