-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Dh3356/test/restinterceptor-test-#11
Creating tests for RestInterceptor
- Loading branch information
Showing
1 changed file
with
117 additions
and
0 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
src/test/java/com/restful_spring/rest_interceptor/RestInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |