Search in sources :

Example 6 with FlashMessageModel

use of com.thoughtworks.go.presentation.FlashMessageModel in project gocd by gocd.

the class FlashLoadingFilterIntegrationTest method shouldInitializeFlashIfNotPresent.

@Test
public void shouldInitializeFlashIfNotPresent() 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);
        }
    };
    assertThat(messageKey, is(nullValue()));
    filter.doFilter(req, res, filterChain);
    assertThat(messageKey, not(nullValue()));
    assertThat(flash.toString(), is("my message"));
    assertThat(flash.getFlashClass(), is("error"));
}
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) Test(org.junit.Test)

Example 7 with FlashMessageModel

use of com.thoughtworks.go.presentation.FlashMessageModel in project gocd by gocd.

the class FlashLoadingFilterIntegrationTest method shouldClearThreadContextInCaseOfExceptionAsWell.

@Test
public void shouldClearThreadContextInCaseOfExceptionAsWell() 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);
            throw new RuntimeException("exception here");
        }
    };
    try {
        filter.doFilter(req, res, filterChain);
        fail("exception gobbled");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("exception here"));
    }
    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 (RuntimeException 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 8 with FlashMessageModel

use of com.thoughtworks.go.presentation.FlashMessageModel in project gocd by gocd.

the class FlashLoadingFilterTest method shouldClearThreadContextInCaseOfExceptionAsWell.

@Test
public void shouldClearThreadContextInCaseOfExceptionAsWell() 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);
            throw new RuntimeException("exception here");
        }
    };
    try {
        filter.doFilter(req, res, filterChain);
        fail("exception gobbled");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("exception here"));
    }
    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 (RuntimeException 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.jupiter.api.Test)

Aggregations

FlashMessageModel (com.thoughtworks.go.presentation.FlashMessageModel)8 FilterChain (javax.servlet.FilterChain)8 ServletRequest (javax.servlet.ServletRequest)8 ServletResponse (javax.servlet.ServletResponse)8 MockFilterChain (org.springframework.mock.web.MockFilterChain)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 IOException (java.io.IOException)4 ServletException (javax.servlet.ServletException)4 Test (org.junit.Test)4 Test (org.junit.jupiter.api.Test)4 MockHttpSession (org.springframework.mock.web.MockHttpSession)2