Search in sources :

Example 66 with FilterChain

use of javax.servlet.FilterChain 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"));
}
Also used : ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockFilterChain(org.springframework.mock.web.MockFilterChain) FlashMessageModel(com.thoughtworks.go.presentation.FlashMessageModel) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 67 with FilterChain

use of javax.servlet.FilterChain 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."));
    }
}
Also used : ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) FlashMessageModel(com.thoughtworks.go.presentation.FlashMessageModel) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Test(org.junit.Test)

Example 68 with FilterChain

use of javax.servlet.FilterChain in project gocd by gocd.

the class LocaleResolverTest method shouldSetLocaleStringToCurrentThread.

@Test
public void shouldSetLocaleStringToCurrentThread() throws IOException, ServletException {
    when(req.getLocale()).thenReturn(new Locale("ja"));
    CurrentLocale.setLocaleString("en");
    localeResolver.doFilter(req, res, new FilterChain() {

        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            localeInside = CurrentLocale.getLocaleString();
        }
    });
    assertThat(CurrentLocale.getLocaleString(), is("en"));
    assertThat(localeInside, is("ja"));
}
Also used : CurrentLocale(com.thoughtworks.go.i18n.CurrentLocale) Locale(java.util.Locale) ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException) Test(org.junit.Test)

Example 69 with FilterChain

use of javax.servlet.FilterChain in project gocd by gocd.

the class LocaleResolverTest method shouldFixThreadLocaleEvenIfFilterFails.

@Test
public void shouldFixThreadLocaleEvenIfFilterFails() throws IOException, ServletException {
    when(req.getLocale()).thenReturn(new Locale("ja"));
    CurrentLocale.setLocaleString("en");
    final RuntimeException exception = new RuntimeException("Oh no!");
    try {
        localeResolver.doFilter(req, res, new FilterChain() {

            public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
                throw exception;
            }
        });
        fail("exception should have been bubbled up");
    } catch (RuntimeException e) {
        assertThat(e, sameInstance(exception));
    }
    assertThat(CurrentLocale.getLocaleString(), is("en"));
}
Also used : CurrentLocale(com.thoughtworks.go.i18n.CurrentLocale) Locale(java.util.Locale) ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException) Test(org.junit.Test)

Example 70 with FilterChain

use of javax.servlet.FilterChain in project simba-os by cegeka.

the class DoFilterAndSetPrincipalActionTest method testExecute_withPrincipal.

@Test
public void testExecute_withPrincipal() throws Exception {
    FilterChain filterChain = mock(FilterChain.class);
    ActionDescriptor actionDescriptor = new ActionDescriptor(new HashSet<ActionType>(), new HashMap<String, String>(), null, null, null, null);
    actionDescriptor.getActionTypes().add(ActionType.DO_FILTER_AND_SET_PRINCIPAL);
    actionDescriptor.setPrincipal("principal");
    DoFilterAndSetPrincipalAction action = new DoFilterAndSetPrincipalAction(actionDescriptor);
    action.setRequest(request);
    action.setResponse(response);
    action.setFilterChain(filterChain);
    action.execute();
    verify(filterChain).doFilter(any(HttpServletRequestWithPrincipal.class), any(HttpServletResponse.class));
}
Also used : ActionType(org.simbasecurity.api.service.thrift.ActionType) FilterChain(javax.servlet.FilterChain) ActionDescriptor(org.simbasecurity.api.service.thrift.ActionDescriptor) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletRequestWithPrincipal(org.simbasecurity.client.filter.request.HttpServletRequestWithPrincipal) Test(org.junit.Test)

Aggregations

FilterChain (javax.servlet.FilterChain)418 HttpServletRequest (javax.servlet.http.HttpServletRequest)317 HttpServletResponse (javax.servlet.http.HttpServletResponse)269 Test (org.junit.Test)246 ServletResponse (javax.servlet.ServletResponse)135 ServletRequest (javax.servlet.ServletRequest)118 FilterConfig (javax.servlet.FilterConfig)80 Filter (javax.servlet.Filter)68 ServletException (javax.servlet.ServletException)54 IOException (java.io.IOException)48 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)46 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)46 Injector (com.google.inject.Injector)32 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)25 ServletContext (javax.servlet.ServletContext)25 Test (org.testng.annotations.Test)25 HttpSession (javax.servlet.http.HttpSession)24 MockFilterChain (org.springframework.mock.web.MockFilterChain)24 InvocationOnMock (org.mockito.invocation.InvocationOnMock)22 Properties (java.util.Properties)19