From 2e7c767f15baea077a185069ce61dc55d9e878d0 Mon Sep 17 00:00:00 2001 From: Kim Daehyeon Date: Mon, 2 Dec 2024 23:44:12 +0900 Subject: [PATCH] test: Add unit tests for RestInterceptor - #11 - Test handling of pre-flight requests (OPTIONS method). - Verify path matching with RestfulPattern in RestInterceptor. - Ensure requests with non-matching paths are skipped. - Check behavior when no patterns are configured. - Test that doInternal() is called for matching requests. - Verify that doInternal() is not called for non-matching requests. --- .../rest_interceptor/RestInterceptorTest.java | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/test/java/com/restful_spring/rest_interceptor/RestInterceptorTest.java diff --git a/src/test/java/com/restful_spring/rest_interceptor/RestInterceptorTest.java b/src/test/java/com/restful_spring/rest_interceptor/RestInterceptorTest.java new file mode 100644 index 0000000..d1a4c4c --- /dev/null +++ b/src/test/java/com/restful_spring/rest_interceptor/RestInterceptorTest.java @@ -0,0 +1,117 @@ +package com.restful_spring.rest_interceptor; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.http.HttpMethod.GET; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpMethod; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +class RestInterceptorTest { + + private static final String FOO = "/foo"; + private static final String BAR = "/bar"; + private static final RestfulPattern REGISTRATION_PATTERN = RestfulPattern.of(FOO, GET); + private RestInterceptor interceptor; + private MockHttpServletRequest request; + private MockHttpServletResponse response; + + @BeforeEach + void setUp() { + interceptor = new RestInterceptor() { + @Override + protected boolean doInternal(HttpServletRequest request, HttpServletResponse response, Object handler) { + return true; + } + }; + + request = new MockHttpServletRequest(); + response = new MockHttpServletResponse(); + } + + @Test + void preFlightRequest() { + // Given + request.setMethod(HttpMethod.OPTIONS.name()); + + // When + boolean result = interceptor.preHandle(request, response, new Object()); + + // Then + assertThat(result).isTrue(); + } + + @Test + void pathMatchingWithRestfulPattern() { + // Given + interceptor.restfulPatterns = List.of(REGISTRATION_PATTERN); + + // When + request.setRequestURI(FOO); + request.setMethod(GET.name()); + boolean result = interceptor.preHandle(request, response, new Object()); + + // Then + assertThat(result).isTrue(); + } + + @Test + void skipWithNonMatchingPath() { + // Given + interceptor.restfulPatterns = List.of(REGISTRATION_PATTERN); + + // When + request.setRequestURI(BAR); + request.setMethod(GET.name()); + boolean result = interceptor.preHandle(request, response, new Object()); + + // Then + assertThat(result).isTrue(); + } + + @Test + void shouldSkipWhenNoPatterns() { + // Given + interceptor.restfulPatterns = List.of(); + + // When + request.setRequestURI(FOO); + boolean result = interceptor.preHandle(request, response, new Object()); + + // Then + assertThat(result).isTrue(); + } + + @Test + void doInternalCalledForMatchingRequest() { + // Given + interceptor.restfulPatterns = List.of(REGISTRATION_PATTERN); + + // When + request.setRequestURI(FOO); + request.setMethod(GET.name()); + + // Then + boolean result = interceptor.preHandle(request, response, new Object()); + assertThat(result).isTrue(); + } + + @Test + void doInternalNotCalledForNonMatchingRequest() { + // Given + interceptor.restfulPatterns = List.of(REGISTRATION_PATTERN); + + // When + request.setRequestURI(BAR); + request.setMethod(GET.name()); + + // Then + boolean result = interceptor.preHandle(request, response, new Object()); + assertThat(result).isTrue(); + } +}