Skip to content

Commit

Permalink
Add HttpHeaders.containsHeaderString() test
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-krueger committed Mar 22, 2024
1 parent 6214d02 commit 2df867c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion jaxrs-api/src/main/java/jakarta/ws/rs/core/HttpHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public interface HttpHeaders {
* (missing comma), or the value {@code no - store} (whitespace within value).
*
* @param name the message header.
* @param valueSeparatorRegex Separates the header value into single values. {@code null} does not split.
* @param valueSeparatorRegex Regular expression that separates the header value into single values.
* {@code null} does not split.
* @param valuePredicate value must fulfil this predicate.
* @return {@code true} if and only if a header with the given name exists, having either a whitespace-trimmed value
* matching the predicate, or having at least one whitespace-trimmed single value in a token-separated list of single values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package ee.jakarta.tck.ws.rs.ee.rs.core.headers;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
Expand Down Expand Up @@ -67,6 +70,39 @@ public String headersGet() {
return sb.toString();
}

@GET
@Path("/contains-headers")
public String containsHeadersGet() {
sb = new StringBuffer();
List<String> myHeaders = Arrays.asList("Accept", "Content-Type", "Header3");

try {
assertTrue(hs.containsHeaderString("accept", "text/html"::equalsIgnoreCase));
sb.append("Header: accept contains text/html; ");

//Verify Predicate and separator character usage
assertTrue(hs.containsHeaderString("Accept", ",", "Text/html;Level=1"::equalsIgnoreCase));
sb.append("Header: accept contains text/html;level=1; ");

//Verify incorrect separator character fails
assertFalse(hs.containsHeaderString("Accept", ";", "text/html;level=1"::equals));
sb.append("Incorrect separator character fails as expected; ");

//Verify white space in parameter not trimmed
assertFalse(hs.containsHeaderString("header3", "**", "value3"::equalsIgnoreCase));
sb.append("White space not trimmed from parameter as expected; ");

//Verify white space in front and back of value trimmed
assertTrue(hs.containsHeaderString("HEADER3", "**", "value2"::equalsIgnoreCase));
sb.append("White space trimmed around value as expected; ");
} catch (Throwable ex) {
sb.append("Unexpected exception thrown in containsHeadersGet: "
+ ex.getMessage());
ex.printStackTrace();
}
return sb.toString();
}

@GET
@Path("/acl")
public String aclGet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,26 @@ public void requestHeadersTest() throws Fault {
invoke();
}

/*
* @testName: requestHeadersTest
*
* @assertion_ids: JAXRS:JAVADOC:1361; JAXRS:JAVADOC:1362;
*
* @test_Strategy: HttpHeaders.containsHeaderString used to confirm that a given header contains
* a specified value with the appropriate value separator.
*/
@Test
public void containsHeaderStringTest() throws Fault {
setProperty(Property.REQUEST_HEADERS,
"Accept:text/*, text/html, text/html;level=1, */*");
setProperty(Property.REQUEST_HEADERS,
"Content-Type:application/xml;charset=utf8");
setProperty(Property.REQUEST_HEADERS,
"Header3:value1 ** value2 **value 3");
setProperty(Property.REQUEST, buildRequest(Request.GET, "contains-headers"));
invoke();
}

/*
* @testName: getDateTest
*
Expand Down

0 comments on commit 2df867c

Please sign in to comment.