Skip to content

Commit

Permalink
put header content in constants
Browse files Browse the repository at this point in the history
issue #58
  • Loading branch information
alekszivko committed Mar 3, 2025
1 parent 74af8b1 commit 66031ab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static com.gepardec.rest.config.filters.response.CorsResponseFilter.*;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.blankOrNullString;
import static org.hamcrest.Matchers.nullValue;
Expand All @@ -13,9 +14,6 @@ public class CorsResponseFilterIT {

private final String VALID_ORIGIN = "http://gamertrack-frontend.apps.cloudscale-lpg-2.appuio.cloud";
private final String INVALID_ORIGIN = "http://lkadsjlksjdfgamertrack-frontend.apps.cloudscale-lpg-2.appuio.com";
private final String ALLOWED_METHODS = "GET, POST, PUT, DELETE, HEAD";
private final String ALLOWED_HEADERS = "Content-Type, Authorization";
private final String ACCESS_CONTROL_ALLOW_CREDENTIALS = "true";

@BeforeAll
public static void setup() {
Expand All @@ -40,7 +38,7 @@ void ensureCorsHeadersArePresentIfOriginMatchesAndUsesHttp() {
.header("Access-Control-Allow-Origin", VALID_ORIGIN)
.header("Access-Control-Allow-Methods", ALLOWED_METHODS)
.header("Access-Control-Allow-Headers", ALLOWED_HEADERS)
.header("Access-Control-Allow-Credentials", ACCESS_CONTROL_ALLOW_CREDENTIALS);
.header("Access-Control-Allow-Credentials", ACCESS_CONTROL_ALLOW_CREDENTIALS_IS_ALLOWED);
}

@Test
Expand All @@ -53,7 +51,7 @@ void ensureCorsHeadersArePresentIfOriginMatchesAndUsesHttps() {
.header("Access-Control-Allow-Origin", VALID_ORIGIN.replace("http", "https"))
.header("Access-Control-Allow-Methods", ALLOWED_METHODS)
.header("Access-Control-Allow-Headers", ALLOWED_HEADERS)
.header("Access-Control-Allow-Credentials", ACCESS_CONTROL_ALLOW_CREDENTIALS);
.header("Access-Control-Allow-Credentials", ACCESS_CONTROL_ALLOW_CREDENTIALS_IS_ALLOWED);
;
}

Expand Down Expand Up @@ -81,7 +79,7 @@ void ensureCorsWorksWhenMakingAHeadRequest() {
.header("Access-Control-Allow-Origin", VALID_ORIGIN)
.header("Access-Control-Allow-Methods", ALLOWED_METHODS)
.header("Access-Control-Allow-Headers", ALLOWED_HEADERS)
.header("Access-Control-Allow-Credentials", ACCESS_CONTROL_ALLOW_CREDENTIALS)
.header("Access-Control-Allow-Credentials", ACCESS_CONTROL_ALLOW_CREDENTIALS_IS_ALLOWED)
.body(blankOrNullString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
@Provider
public class CorsResponseFilter implements ContainerResponseFilter {

protected static final String ALLOWED_METHODS = "GET, POST, PUT, DELETE, HEAD";
protected static final String ALLOWED_HEADERS = "Content-Type, Authorization";
protected static final String ACCESS_CONTROL_ALLOW_CREDENTIALS_IS_ALLOWED = "true";


Dotenv dotenv = Dotenv
.configure()
.directory("../..")
Expand All @@ -25,9 +30,9 @@ public void filter(ContainerRequestContext requestContext, ContainerResponseCont

if (origin != null && origin.matches(dotenv.get("ALLOWED_ORIGINS_AS_REGEX"))) {
responseContext.getHeaders().add("Access-Control-Allow-Origin", origin);
responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, HEAD");
responseContext.getHeaders().add("Access-Control-Allow-Headers", "Content-Type, Authorization, x-total-count");
responseContext.getHeaders().add("Access-Control-Allow-Credentials", true);
responseContext.getHeaders().add("Access-Control-Allow-Methods", ALLOWED_METHODS);
responseContext.getHeaders().add("Access-Control-Allow-Headers", ALLOWED_HEADERS);
responseContext.getHeaders().add("Access-Control-Allow-Credentials", ACCESS_CONTROL_ALLOW_CREDENTIALS_IS_ALLOWED);
}
}
}

0 comments on commit 66031ab

Please sign in to comment.