77import java .util .List ;
88import java .util .Locale ;
99import java .util .Set ;
10- import java .util .stream .Stream ;
1110import java .util .stream .Collectors ;
1211import org .junit .jupiter .api .BeforeAll ;
1312import org .junit .jupiter .params .ParameterizedTest ;
14- import org .junit .jupiter .params .provider .MethodSource ;
13+ import org .junit .jupiter .params .provider .ValueSource ;
1514
1615import static io .restassured .RestAssured .given ;
16+ import static org .hamcrest .Matchers .anyOf ;
17+ import static org .hamcrest .Matchers .blankOrNullString ;
18+ import static org .hamcrest .Matchers .is ;
19+ import static org .hamcrest .Matchers .not ;
1720import static org .junit .jupiter .api .Assertions .assertEquals ;
1821import static org .junit .jupiter .api .Assertions .assertTrue ;
1922
2831 */
2932public class CorsIT {
3033 private static final String ORIGIN_NULL = "null" ;
31- private static final List <String > PRE_FLIGHT_ENDPOINTS = List .of (
32- "/api/dataverses/root/datasets" ,
33- "/api/v1/dataverses/root/datasets" ,
34- "/page_doesnt_exist" ,
35- "/dvn/api/data-deposit/v1.1/swordv2/collection/dataverse/root"
36- );
3734
3835 @ BeforeAll
3936 public static void setUp () {
4037 RestAssured .baseURI = UtilIT .getRestAssuredBaseUri ();
4138 }
4239
4340 @ ParameterizedTest (name = "CORS preflight headers on {0}" )
44- @ MethodSource ("preflightEndpoints" )
41+ @ ValueSource (strings = {
42+ "/api/dataverses/root/datasets" ,
43+ "/api/v1/dataverses/root/datasets" ,
44+ "/page_doesnt_exist" ,
45+ "/dvn/api/data-deposit/v1.1/swordv2/collection/dataverse/root"
46+ })
4547 public void testPreflightOptionsCorsHeaders (String path ) {
4648 assertPreflightCorsHeaders (path );
4749 }
4850
49- private static Stream <String > preflightEndpoints () {
50- return PRE_FLIGHT_ENDPOINTS .stream ();
51- }
52-
5351 private void assertPreflightCorsHeaders (String path ) {
5452 Response response = given ()
5553 .header ("Accept" , "*/*" )
@@ -58,10 +56,16 @@ private void assertPreflightCorsHeaders(String path) {
5856 .header ("Access-Control-Request-Method" , "POST" )
5957 .header ("Origin" , ORIGIN_NULL )
6058 .header ("User-Agent" , "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36" )
61- .options (path );
62-
63- int statusCode = response .getStatusCode ();
64- assertTrue (statusCode == 200 || statusCode == 204 , "Expected 200 or 204 but got " + statusCode );
59+ .when ()
60+ .options (path )
61+ .then ()
62+ .log ().ifValidationFails ()
63+ .statusCode (anyOf (is (200 ), is (204 )))
64+ .header ("Access-Control-Allow-Methods" , not (blankOrNullString ()))
65+ .header ("Access-Control-Allow-Headers" , not (blankOrNullString ()))
66+ .header ("Access-Control-Expose-Headers" , not (blankOrNullString ()))
67+ .extract ()
68+ .response ();
6569
6670 assertHeaderSetEquals ("Access-Control-Allow-Methods" , getExpectedCorsMethods (), response );
6771 assertHeaderSetEquals ("Access-Control-Allow-Headers" , getExpectedCorsAllowHeaders (), response );
0 commit comments