use of com.thoughtworks.go.http.mocks.MockHttpServletRequest in project gocd by gocd.
the class ThreadLocalUserFilterTest method shouldSetUserToThreadLocalWhenFilterIsCalledAndRemoveUserFromThreadLocalOnceRequestIsCompleted.
@Test
void shouldSetUserToThreadLocalWhenFilterIsCalledAndRemoveUserFromThreadLocalOnceRequestIsCompleted() throws ServletException, IOException {
final MockHttpServletRequest request = new MockHttpServletRequest();
final MockHttpServletResponse response = new MockHttpServletResponse();
final AuthenticationToken<UsernamePassword> authenticationToken = SessionUtilsHelper.createUsernamePasswordAuthentication("bob", "p@ssw0rd", 0L);
SessionUtils.setAuthenticationTokenAfterRecreatingSession(authenticationToken, request);
final GoUserPrinciple[] currentUserInFilter = { null };
final FilterChain filterChain = new MockFilterChain(mock(Servlet.class), spy(new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
currentUserInFilter[0] = SessionUtils.getCurrentUser();
}
}));
new ThreadLocalUserFilter().doFilter(request, response, filterChain);
assertThat(currentUserInFilter[0]).isNotNull();
assertThat(SessionUtils.getCurrentUser().getUsername()).isEqualTo("anonymous");
assertThat(SessionUtils.getCurrentUser().getAuthorities()).containsExactly(GoAuthority.ROLE_ANONYMOUS.asAuthority());
}
use of com.thoughtworks.go.http.mocks.MockHttpServletRequest in project gocd by gocd.
the class UserEnabledCheckFilterWithRedirectToLoginPageTest method setUp.
@BeforeEach
void setUp() {
filter = new UserEnabledCheckFilterWithRedirectToLoginPage(null, null);
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}
use of com.thoughtworks.go.http.mocks.MockHttpServletRequest in project gocd by gocd.
the class RequestRejectedExceptionHandlerTest method shouldReturn404WithHtmlResponse.
@Test
void shouldReturn404WithHtmlResponse() throws IOException {
final MockHttpServletRequest request = HttpRequestBuilder.GET("foo/bar").withHeader("Accept", "application/json").build();
final MockHttpServletResponse response = new MockHttpServletResponse();
new RequestRejectedExceptionHandler().handle(request, response, "Something went wrong", HttpStatus.BAD_REQUEST);
assertThat(response).isBadRequest().hasNoBody();
}
use of com.thoughtworks.go.http.mocks.MockHttpServletRequest in project gocd by gocd.
the class RequestRejectedExceptionHandlerTest method shouldHandleUrlsAsApiRequest.
@ParameterizedTest
@ValueSource(strings = { "/remoting/foo", "/add-on/bar/api/foo", "/api/foo", "/cctray.xml" })
void shouldHandleUrlsAsApiRequest(String url) throws IOException {
final MockHttpServletRequest request = HttpRequestBuilder.GET(url).withHeader("Accept", "application/json").build();
final MockHttpServletResponse response = new MockHttpServletResponse();
new RequestRejectedExceptionHandler().handle(request, response, "Something went wrong", HttpStatus.BAD_REQUEST);
assertThat(response).isBadRequest().hasBody("{\n" + " \"message\": \"Something went wrong\"\n" + "}");
}
use of com.thoughtworks.go.http.mocks.MockHttpServletRequest in project gocd by gocd.
the class AbstractUserEnabledCheckFilterTest method setUp.
@BeforeEach
void setUp() throws Exception {
userService = mock(UserService.class);
securityService = mock(SecurityService.class);
filter = spy(new AbstractUserEnabledCheckFilter(userService, securityService) {
@Override
void handleFailure(HttpServletRequest request, HttpServletResponse response, String errorMessage) throws IOException {
}
});
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
filterChain = mock(FilterChain.class);
}
Aggregations