use of org.springframework.mock.web.MockFilterChain in project uplace.es by Uplace.
the class JWTFilterTest method testJWTFilterMissingToken.
@Test
public void testJWTFilterMissingToken() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader(JWTConfigurer.AUTHORIZATION_HEADER, "Bearer ");
request.setRequestURI("/api/test");
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain filterChain = new MockFilterChain();
jwtFilter.doFilter(request, response, filterChain);
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
use of org.springframework.mock.web.MockFilterChain in project uplace.es by Uplace.
the class JWTFilterTest method testJWTFilter.
@Test
public void testJWTFilter() throws Exception {
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("test-user", "test-password", Collections.singletonList(new SimpleGrantedAuthority(AuthoritiesConstants.USER)));
String jwt = tokenProvider.createToken(authentication, false);
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader(JWTConfigurer.AUTHORIZATION_HEADER, "Bearer " + jwt);
request.setRequestURI("/api/test");
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain filterChain = new MockFilterChain();
jwtFilter.doFilter(request, response, filterChain);
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
assertThat(SecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo("test-user");
assertThat(SecurityContextHolder.getContext().getAuthentication().getCredentials().toString()).isEqualTo(jwt);
}
use of org.springframework.mock.web.MockFilterChain in project uplace.es by Uplace.
the class JWTFilterTest method testJWTFilterInvalidToken.
@Test
public void testJWTFilterInvalidToken() throws Exception {
String jwt = "wrong_jwt";
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader(JWTConfigurer.AUTHORIZATION_HEADER, "Bearer " + jwt);
request.setRequestURI("/api/test");
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain filterChain = new MockFilterChain();
jwtFilter.doFilter(request, response, filterChain);
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
use of org.springframework.mock.web.MockFilterChain in project gocd by gocd.
the class FlashLoadingFilterIntegrationTest method shouldLoadExistingFlashFromSession.
@Test
public void shouldLoadExistingFlashFromSession() throws IOException, ServletException {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpSession session = new MockHttpSession();
FlashMessageService.Flash oldFlash = new FlashMessageService.Flash();
oldFlash.put("my_key", new FlashMessageModel("my other message", "warning"));
session.putValue(FlashLoadingFilter.FLASH_SESSION_KEY, oldFlash);
req.setSession(session);
MockHttpServletResponse res = new MockHttpServletResponse();
FilterChain filterChain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response) {
flash = service.get("my_key");
}
};
filter.doFilter(req, res, filterChain);
assertThat(flash.toString(), is("my other message"));
assertThat(flash.getFlashClass(), is("warning"));
}
use of org.springframework.mock.web.MockFilterChain in project gocd by gocd.
the class FlashLoadingFilterIntegrationTest method shouldClearThreadContext.
@Test
public void shouldClearThreadContext() throws IOException, ServletException {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
FilterChain filterChain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response) {
messageKey = service.add(new FlashMessageModel("my message", "error"));
flash = service.get(messageKey);
}
};
filter.doFilter(req, res, filterChain);
assertThat(flash.toString(), is("my message"));
try {
service.get(messageKey);
fail("attempt to load flash message should fail, as no thread local is cleared out");
} catch (Exception e) {
assertThat(e.getMessage(), is("No flash context found, this call should only be made within a request."));
}
}
Aggregations