Search in sources :

Example 1 with FlashMessageModel

use of com.thoughtworks.go.presentation.FlashMessageModel 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 2 with FlashMessageModel

use of com.thoughtworks.go.presentation.FlashMessageModel 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 3 with FlashMessageModel

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

the class FlashLoadingFilterTest 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.jupiter.api.Test)

Example 4 with FlashMessageModel

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

the class FlashLoadingFilterTest 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.jupiter.api.Test)

Example 5 with FlashMessageModel

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

the class FlashLoadingFilterTest 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.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