Search in sources :

Example 36 with ServletResponse

use of javax.servlet.ServletResponse 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 37 with ServletResponse

use of javax.servlet.ServletResponse 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 38 with ServletResponse

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

the class BasicAuthenticationFilterTest method shouldConvey_itsBasicProcessingFilter.

@Test
public void shouldConvey_itsBasicProcessingFilter() throws IOException, ServletException {
    BasicAuthenticationFilter filter = new BasicAuthenticationFilter(localizer);
    final Boolean[] hadBasicMarkOnInsideAuthenticationManager = new Boolean[] { false };
    filter.setAuthenticationManager(new AuthenticationManager() {

        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            hadBasicMarkOnInsideAuthenticationManager[0] = BasicAuthenticationFilter.isProcessingBasicAuth();
            return new UsernamePasswordAuthenticationToken("school-principal", "u can be principal if you know this!");
        }
    });
    assertThat(BasicAuthenticationFilter.isProcessingBasicAuth(), is(false));
    MockHttpServletRequest httpRequest = new MockHttpServletRequest();
    httpRequest.addHeader("Authorization", "Basic " + java.util.Base64.getEncoder().encodeToString("loser:boozer".getBytes()));
    filter.doFilterHttp(httpRequest, new MockHttpServletResponse(), new FilterChain() {

        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
        }
    });
    assertThat(BasicAuthenticationFilter.isProcessingBasicAuth(), is(false));
    assertThat(hadBasicMarkOnInsideAuthenticationManager[0], is(true));
}
Also used : ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) AuthenticationException(org.springframework.security.AuthenticationException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken) IOException(java.io.IOException) AuthenticationManager(org.springframework.security.AuthenticationManager) ServletException(javax.servlet.ServletException) Authentication(org.springframework.security.Authentication) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 39 with ServletResponse

use of javax.servlet.ServletResponse 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 40 with ServletResponse

use of javax.servlet.ServletResponse 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)

Aggregations

ServletResponse (javax.servlet.ServletResponse)155 ServletRequest (javax.servlet.ServletRequest)129 HttpServletResponse (javax.servlet.http.HttpServletResponse)104 HttpServletRequest (javax.servlet.http.HttpServletRequest)90 FilterChain (javax.servlet.FilterChain)81 Test (org.junit.Test)76 ServletException (javax.servlet.ServletException)59 IOException (java.io.IOException)57 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)35 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)35 MockFilterChain (org.springframework.mock.web.MockFilterChain)32 Filter (javax.servlet.Filter)28 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)26 Injector (com.google.inject.Injector)25 NestedServletException (org.springframework.web.util.NestedServletException)19 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)18 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)18 FilterConfig (javax.servlet.FilterConfig)15 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14