Skip to content

Commit 9642bda

Browse files
committed
test: Cover CORS preflight constructor validation
1 parent b9954e6 commit 9642bda

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

src/test/java/com/retailsvc/http/CorsPreflightHandlerTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,63 @@ void corsPreflightHandlerListOverloadDelegatesToPredicateBehaviour() {
212212
assertThat(allowed.status()).isEqualTo(HTTP_NO_CONTENT);
213213
assertThat(denied.status()).isEqualTo(HTTP_FORBIDDEN);
214214
}
215+
216+
@Test
217+
void corsPreflightHandlerRejectsNullOriginList() {
218+
assertThatThrownBy(
219+
() -> Handlers.corsPreflightHandler((List<String>) null, METHODS, HEADERS, false, null))
220+
.isInstanceOf(NullPointerException.class)
221+
.hasMessageContaining("allowedOrigins");
222+
}
223+
224+
@Test
225+
void corsPreflightHandlerRejectsNullOriginPredicate() {
226+
assertThatThrownBy(
227+
() ->
228+
Handlers.corsPreflightHandler(
229+
(Predicate<String>) null, METHODS, HEADERS, false, null))
230+
.isInstanceOf(NullPointerException.class)
231+
.hasMessageContaining("originAllowed");
232+
}
233+
234+
@Test
235+
void corsPreflightHandlerRejectsNullMethods() {
236+
assertThatThrownBy(() -> Handlers.corsPreflightHandler(ORIGINS, null, HEADERS, false, null))
237+
.isInstanceOf(NullPointerException.class)
238+
.hasMessageContaining("allowedMethods");
239+
}
240+
241+
@Test
242+
void corsPreflightHandlerRejectsEmptyMethods() {
243+
assertThatThrownBy(
244+
() -> Handlers.corsPreflightHandler(ORIGINS, List.of(), HEADERS, false, null))
245+
.isInstanceOf(IllegalArgumentException.class)
246+
.hasMessageContaining("allowedMethods");
247+
}
248+
249+
@Test
250+
void corsPreflightHandlerRejectsNullHeaders() {
251+
assertThatThrownBy(() -> Handlers.corsPreflightHandler(ORIGINS, METHODS, null, false, null))
252+
.isInstanceOf(NullPointerException.class)
253+
.hasMessageContaining("allowedHeaders");
254+
}
255+
256+
@Test
257+
void corsPreflightHandlerRejectsNegativeMaxAge() {
258+
assertThatThrownBy(
259+
() ->
260+
Handlers.corsPreflightHandler(
261+
ORIGINS, METHODS, HEADERS, false, Duration.ofSeconds(-1)))
262+
.isInstanceOf(IllegalArgumentException.class)
263+
.hasMessageContaining("maxAge");
264+
}
265+
266+
@Test
267+
void corsPreflightHandlerRejectsOverflowingMaxAge() {
268+
Duration tooBig = Duration.ofSeconds((long) Integer.MAX_VALUE + 1);
269+
assertThatThrownBy(
270+
() -> Handlers.corsPreflightHandler(ORIGINS, METHODS, HEADERS, false, tooBig))
271+
.isInstanceOf(IllegalArgumentException.class)
272+
.hasMessageContaining("maxAge");
273+
}
215274
}

0 commit comments

Comments
 (0)