Skip to content

Commit

Permalink
Merge pull request #1238 from jim-krueger/1227-TCK-containsHeaderString
Browse files Browse the repository at this point in the history
Add tests for containsHeaderString
  • Loading branch information
jim-krueger authored Mar 28, 2024
2 parents 8820386 + ac9837b commit 273a477
Show file tree
Hide file tree
Showing 16 changed files with 294 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -207,7 +207,8 @@ public default boolean hasProperty(String name) {
* (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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -99,7 +99,8 @@ public interface ClientResponseContext {
* (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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -239,7 +239,7 @@ public default boolean hasProperty(String name) {
* (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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -129,7 +129,7 @@ public interface ContainerResponseContext {
* (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
5 changes: 3 additions & 2 deletions jaxrs-api/src/main/java/jakarta/ws/rs/core/HttpHeaders.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down 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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,6 +17,7 @@
package ee.jakarta.tck.ws.rs.api.client.clientrequestcontext;

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

import java.io.ByteArrayInputStream;
import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -860,6 +861,46 @@ protected void checkFilterContext(ClientRequestContext context)
assertContains(entity, "Accept");
}

/*
* @testName: containsHeaderStringTest
*
* @assertion_ids: JAXRS:JAVADOC:1353; JAXRS:JAVADOC:1354;
*
* @test_Strategy: Check if the specified header contains a specified value.
*
* ClientRequestFilter.abortWith
*/
@Test
public void containsHeaderStringTest() throws Fault {
ContextProvider provider = new ContextProvider() {
@Override
protected void checkFilterContext(ClientRequestContext context) throws Fault {
assertTrue(context.containsHeaderString("header1", "value"::equals));
assertTrue(context.containsHeaderString("HEADER1", ",", "value2"::equals));
//Incorrect separator character
assertFalse(context.containsHeaderString("header1", ";", "value2"::equals));
//Shouldn't find first value when separator character is incorrect
assertFalse(context.containsHeaderString("header1", ";", "Value1"::equalsIgnoreCase));
//Test regular expression
assertTrue(context.containsHeaderString("header1", ";|,", "VALUE2"::equalsIgnoreCase));
//White space in value not trimmed
assertFalse(context.containsHeaderString("header1", "whitespace"::equals));
//Multiple character separator
assertTrue(context.containsHeaderString("header2", ";;", "Value5"::equalsIgnoreCase));
Response r = Response.ok().build();
context.abortWith(r);
}
};
Invocation invocation = buildBuilder(provider)
.header("header1", "value")
.header("header1", "value1 , value2")
.header("header1", "Value3,white space ")
.header("header2", "Value4;;Value5")
.buildGet();
Response response = invoke(invocation);
}


/*
* @testName: getHeaderStringTest
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,6 +16,7 @@

package ee.jakarta.tck.ws.rs.api.client.clientresponsecontext;

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

import java.io.BufferedReader;
Expand Down Expand Up @@ -426,6 +427,47 @@ protected void checkFilterContext(ClientRequestContext requestContext,
invokeWithResponseAndAssertStatus(response, Status.OK, in);
}

/*
* @testName: containsHeaderStringTest
*
* @assertion_ids: JAXRS:JAVADOC:1355; JAXRS:JAVADOC:1356;
*
* @test_Strategy: Check if the specified header contains a specified value.
*
* ClientRequestFilter.abortWith
*/
@Test
public void containsHeaderStringTest() throws Fault {

ContextProvider in = new ContextProvider() {
@Override
protected void checkFilterContext(ClientRequestContext requestContext,
ClientResponseContext responseContext) throws Fault {
assertTrue(responseContext.containsHeaderString("header1", "value"::equals));
assertTrue(responseContext.containsHeaderString("HEADER1", ",", "value2"::equals));
//Incorrect separator character
assertFalse(responseContext.containsHeaderString("header1", ";", "value2"::equals));
//Shouldn't find first value when separator character is incorrect
assertFalse(responseContext.containsHeaderString("header1", ";", "Value1"::equalsIgnoreCase));
//Test regular expression
assertTrue(responseContext.containsHeaderString("header1", ";|,", "VALUE2"::equalsIgnoreCase));
//White space in value not trimmed
assertFalse(responseContext.containsHeaderString("header1", "whitespace"::equalsIgnoreCase));
//Multiple character separator
assertTrue(responseContext.containsHeaderString("header2", ";;", "Value5"::equalsIgnoreCase));
}
};
Response response = Response.ok()
.header("header1", "value")
.header("header1", "value1 , value2")
.header("header1", "Value3,white space ")
.header("header2", "Value4;;Value5")
.build();
invokeWithResponseAndAssertStatus(response, Status.OK, in);
}



/*
* @testName: getLanguageTest
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,5 +17,5 @@
package ee.jakarta.tck.ws.rs.ee.rs.container.requestcontext;

public enum ContextOperation {
ABORTWITH, GETACCEPTABLELANGUAGES, GETACCEPTABLELANGUAGESISREADONLY, GETACCEPTABLEMEDIATYPES, GETACCEPTABLEMEDIATYPESISREADONLY, GETCOOKIES, GETCOOKIESISREADONLY, GETDATE, GETENTITYSTREAM, GETHEADERS, GETHEADERSISMUTABLE, GETHEADERSTRING2, GETLANGUAGE, GETLENGTH, GETMEDIATYPE, GETMETHOD, GETPROPERTY, GETPROPERTYNAMES, GETPROPERTYNAMESISREADONLY, GETREQUEST, GETSECURITYCONTEXT, GETURIINFO, HASENTITY, REMOVEPROPERTY, SETENTITYSTREAM, SETMETHOD, SETPROPERTY, SETPROPERTYNULL, SETPROPERTYCONTEXT, SETREQUESTURI1, SETREQUESTURI2, SETSECURITYCONTEXT;
ABORTWITH, GETACCEPTABLELANGUAGES, GETACCEPTABLELANGUAGESISREADONLY, GETACCEPTABLEMEDIATYPES, GETACCEPTABLEMEDIATYPESISREADONLY, GETCOOKIES, GETCOOKIESISREADONLY, GETDATE, GETENTITYSTREAM, CONTAINSHEADERSTRING, GETHEADERS, GETHEADERSISMUTABLE, GETHEADERSTRING2, GETLANGUAGE, GETLENGTH, GETMEDIATYPE, GETMETHOD, GETPROPERTY, GETPROPERTYNAMES, GETPROPERTYNAMESISREADONLY, GETREQUEST, GETSECURITYCONTEXT, GETURIINFO, HASENTITY, REMOVEPROPERTY, SETENTITYSTREAM, SETMETHOD, SETPROPERTY, SETPROPERTYNULL, SETPROPERTYCONTEXT, SETREQUESTURI1, SETREQUESTURI2, SETSECURITYCONTEXT;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -333,6 +333,34 @@ public void getEntityStreamTest() throws Fault {
ContextOperation.GETENTITYSTREAM);
}

/*
* @testName: containsHeaderStringTest
*
* @assertion_ids: JAXRS:JAVADOC:JAXRS:JAVADOC:1359; JAXRS:JAVADOC:1360
*
* @test_Strategy: Check if the specified header contains a specified value.
*
* Filter method called before a request has been dispatched to a resource.
* Throws IOException.
*/
@Test
@Tag("servlet")
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.SEARCH_STRING, "Test1");
setProperty(Property.SEARCH_STRING, "Test2");
setProperty(Property.SEARCH_STRING, "Test3");
setProperty(Property.SEARCH_STRING, "Test4");
setProperty(Property.SEARCH_STRING, "Test5");

invokeRequestAndCheckResponse(ContextOperation.CONTAINSHEADERSTRING);
}

/*
* @testName: getHeadersTest
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -140,6 +140,37 @@ public void getEntityStream() throws IOException {
abortWithEntity(entity);
}

public void containsHeaderString() {
StringBuffer sb = new StringBuffer();
sb.append("containsHeaderString= ");

try {
assertTrue(requestContext.containsHeaderString("accept", "text/html"::equals));
sb.append("Test1: accept contains text/html; ");

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

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

//Verify white space in value not trimmed and double character separator
assertTrue(!(requestContext.containsHeaderString("header3", ";;", "value3"::equals)));
sb.append("Test4: White space not trimmed from value as expected; ");

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

public void getHeaders() {
MultivaluedMap<String, String> headers = requestContext.getHeaders();
StringBuilder sb = new StringBuilder();
Expand All @@ -149,7 +180,7 @@ public void getHeaders() {
}
abortWithEntity(sb.toString());
}

public void getHeadersIsMutable() {
String key = "KEY";
MultivaluedMap<String, String> headers = requestContext.getHeaders();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,7 +17,7 @@
package ee.jakarta.tck.ws.rs.ee.rs.container.responsecontext;

public enum ContextOperation {
GETALLOWEDMETHODS, GETCOOKIES, GETCOOKIESISREADONLY, GETDATE, GETENTITY, GETENTITYANNOTATIONS, GETENTITYANNOTATIONSONENTITY, GETENTITYCLASS, GETENTITYSTREAM, GETENTITYTAG, GETENTITYTYPE, GETHEADERS, GETHEADERSISMUTABLE, GETHEADERSTRINGOPERATION, GETHEADERSTRINGHEADER, GETLANGUAGE, GETLASTMODIFIED, GETLENGTH, GETLINK, GETLINKBUILDER, GETLINKS, GETLOCATION, GETMEDIATYPE, GETSTATUS, GETSTATUSINFO, GETSTRINGHEADERS, HASENTITY, HASLINK, SETENTITY, SETENTITYSTREAM, SETSTATUS, SETSTATUSINFO,
GETALLOWEDMETHODS, GETCOOKIES, GETCOOKIESISREADONLY, GETDATE, GETENTITY, GETENTITYANNOTATIONS, GETENTITYANNOTATIONSONENTITY, GETENTITYCLASS, GETENTITYSTREAM, GETENTITYTAG, GETENTITYTYPE, CONTAINSHEADERSTRING, GETHEADERS, GETHEADERSISMUTABLE, GETHEADERSTRINGOPERATION, GETHEADERSTRINGHEADER, GETLANGUAGE, GETLASTMODIFIED, GETLENGTH, GETLINK, GETLINKBUILDER, GETLINKS, GETLOCATION, GETMEDIATYPE, GETSTATUS, GETSTATUSINFO, GETSTRINGHEADERS, HASENTITY, HASLINK, SETENTITY, SETENTITYSTREAM, SETSTATUS, SETSTATUSINFO,

// just helping methods, pass
SETSTRINGBEANRUNTIME, SETORIGINALRUNTIME
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -445,6 +445,25 @@ public void getEntityTypeInputStreamTest() throws Fault {
invokeRequestAndCheckResponse(ContextOperation.GETENTITYTYPE);
}

/*
* @testName: getHeadersTest
*
* @assertion_ids: JAXRS:JAVADOC:1357; JAXRS:JAVADOC:1358;
*
* @test_Strategy: Get the mutable response headers multivalued map.
*
* Filter method called after a response has been provided for a request.
* Throws IOException.
*/
@Test
public void containsHeaderStringTest() throws Fault {
String header = "Test";
for (int i = 1; i != 6; i++)
setProperty(Property.UNORDERED_SEARCH_STRING, header + i);
setProperty(Property.CONTENT, header);
invokeRequestAndCheckResponse(ContextOperation.CONTAINSHEADERSTRING);
}

/*
* @testName: getHeadersTest
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -179,6 +179,17 @@ public Response setEntityStream() {
return response;
}

@POST
@Path("containsheaderstring")
public Response containsHeaderString(String header) {
ResponseBuilder builder = createResponseWithHeader();
builder = builder.header("Accept", "text/html, text/html;level=1, */*");
builder = builder.header("Content-Type", "application/xml;charset=utf8");
builder = builder.header("Header3", "value1 ;; Value2 ;;value 3");
Response response = builder.build();
return response;
}

@POST
@Path("getheaders")
public Response getHeaders(String header) {
Expand Down
Loading

0 comments on commit 273a477

Please sign in to comment.