Skip to content

Commit ece2d2e

Browse files
committed
Respond to Options request with reasonable values. (#1289)
Fixes #1175. (cherry picked from commit 6d008de)
1 parent 451fb3f commit ece2d2e

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

cwms-data-api/src/main/java/cwms/cda/ApiServlet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,12 @@ public void init() {
503503
ctx.json(errResponse);
504504
})
505505
.routes(this::configureRoutes)
506+
.options("/*", ctx -> {
507+
ctx.header("Access-Control-Allow-Origin", "*"); // Allow requests from any origin
508+
ctx.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); // Specify allowed methods
509+
ctx.header("Access-Control-Allow-Headers", "Content-Type, Authorization"); // Specify allowed headers
510+
ctx.status(200); // Respond with a 200 OK status
511+
})
506512
.javalinServlet();
507513
logger.atInfo().log("Javalin initialized.");
508514
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cwms.cda.api;
2+
3+
import static io.restassured.RestAssured.given;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.is;
6+
import static org.hamcrest.Matchers.nullValue;
7+
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
import org.junit.jupiter.api.Tag;
11+
import org.junit.jupiter.api.extension.ExtendWith;
12+
import org.junit.jupiter.params.ParameterizedTest;
13+
import org.junit.jupiter.params.provider.ValueSource;
14+
15+
import fixtures.CwmsDataApiSetupCallback;
16+
import io.restassured.filter.log.LogDetail;
17+
18+
/**
19+
* Location for tests that aren't specifically related to a given endpoint.
20+
*/
21+
@Tag("integration")
22+
@ExtendWith(CwmsDataApiSetupCallback.class)
23+
class BaseLineTestIT extends DataApiTestIT {
24+
25+
@ParameterizedTest
26+
@ValueSource(strings = {"/blobs/", "/timeseries", "/levels"})
27+
void test_options_handling_known_url(String url) throws Exception {
28+
given()
29+
.log().ifValidationFails(LogDetail.ALL,true)
30+
.when()
31+
.redirects().follow(true)
32+
.redirects().max(3)
33+
.options(url)
34+
.then()
35+
.log().ifValidationFails(LogDetail.ALL,true)
36+
.assertThat()
37+
.statusCode(is(HttpServletResponse.SC_OK))
38+
.header("Access-Control-Allow-Methods", equalTo("GET, POST, PUT, DELETE, OPTIONS"))
39+
.header("Access-Control-Allow-Headers", equalTo("Content-Type, Authorization"));
40+
}
41+
42+
@ParameterizedTest
43+
@ValueSource(strings = {"/flurgle/", "/blah/", "/levels-i-do-not-exist"})
44+
void test_options_handling_unknown_url(String url) throws Exception {
45+
given()
46+
.log().ifValidationFails(LogDetail.ALL,true)
47+
.when()
48+
.redirects().follow(true)
49+
.redirects().max(3)
50+
.options(url)
51+
.then()
52+
.log().ifValidationFails(LogDetail.ALL,true)
53+
.assertThat()
54+
.statusCode(is(HttpServletResponse.SC_OK))
55+
.header("Access-Control-Allow-Methods", nullValue())
56+
.header("Access-Control-Allow-Headers", nullValue());
57+
}
58+
}

0 commit comments

Comments
 (0)