|
| 1 | +package org.springframework.security.web.access; |
| 2 | + |
| 3 | +import jakarta.servlet.ServletException; |
| 4 | +import jakarta.servlet.http.HttpServletRequest; |
| 5 | +import jakarta.servlet.http.HttpServletResponse; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 8 | +import org.mockito.Mock; |
| 9 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 10 | +import org.springframework.http.HttpStatus; |
| 11 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 12 | +import org.springframework.security.access.AccessDeniedException; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 18 | + |
| 19 | +@ExtendWith(MockitoExtension.class) |
| 20 | +public class HttpStatusAccessDeniedHandlerTests { |
| 21 | + |
| 22 | + @Mock |
| 23 | + private HttpServletRequest request; |
| 24 | + |
| 25 | + @Mock |
| 26 | + private HttpServletResponse response; |
| 27 | + |
| 28 | + private HttpStatus httpStatus = HttpStatus.FORBIDDEN; |
| 29 | + |
| 30 | + private HttpStatusAccessDeniedHandler handler = new HttpStatusAccessDeniedHandler(this.httpStatus); |
| 31 | + |
| 32 | + private AccessDeniedException exception = new AccessDeniedException("Forbidden"); |
| 33 | + |
| 34 | + @Test |
| 35 | + public void constructorHttpStatusWhenNullThenException() { |
| 36 | + assertThatIllegalArgumentException().isThrownBy(() -> new HttpStatusAccessDeniedHandler(null)); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void commenceThenStatusSet() throws IOException, ServletException { |
| 41 | + this.response = new MockHttpServletResponse(); |
| 42 | + this.handler.handle(this.request, this.response, this.exception); |
| 43 | + assertThat(this.response.getStatus()).isEqualTo(this.httpStatus.value()); |
| 44 | + } |
| 45 | + |
| 46 | +} |
0 commit comments