Search in sources :

Example 81 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project gocd by gocd.

the class EchoAttributeServletTest method shouldEchoAttributeInBody.

@Theory
public void shouldEchoAttributeInBody(Req dataPt) throws UnsupportedEncodingException {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setAttribute(EchoAttributeServlet.ECHO_BODY_ATTRIBUTE, "some-random-echo-body");
    MockHttpServletResponse res = new MockHttpServletResponse();
    res.setContentType("foo/bar");
    dataPt.call(servlet, req, res);
    assertThat(res.getContentAsString(), is("some-random-echo-body"));
    assertThat(res.getContentType(), is("foo/bar"));
    assertThat(res.getStatus(), is(200));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Theory(org.junit.experimental.theories.Theory)

Example 82 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse 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 83 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse 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 84 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project gocd by gocd.

the class JobControllerIntegrationTest method setUp.

@Before
public void setUp() throws Exception {
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    configHelper = new GoConfigFileHelper();
    configHelper.usingCruiseConfigDao(goConfigDao);
    fixture = new PipelineWithTwoStages(materialRepository, transactionTemplate, temporaryFolder);
    fixture.usingConfigHelper(configHelper).usingDbHelper(dbHelper).onSetUp();
    controller = new JobController(jobInstanceService, agentService, jobInstanceDao, goConfigService, pipelineService, restfulService, artifactService, propertiesService, stageService, localizer, jobAgentMetadataDao);
}
Also used : PipelineWithTwoStages(com.thoughtworks.go.fixture.PipelineWithTwoStages) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) GoConfigFileHelper(com.thoughtworks.go.util.GoConfigFileHelper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Example 85 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project gocd by gocd.

the class PropertiesControllerTest method setUp.

@Before
public void setUp() throws Exception {
    configHelper = new GoConfigFileHelper();
    fixture = new PipelineWithTwoStages(materialRepository, transactionTemplate, temporaryFolder);
    fixture.usingConfigHelper(configHelper).usingDbHelper(dbHelper).onSetUp();
    controller = new PropertiesController(propertiesService, restfulService, pipelineService, systemEnvironment);
    response = new MockHttpServletResponse();
}
Also used : PipelineWithTwoStages(com.thoughtworks.go.fixture.PipelineWithTwoStages) GoConfigFileHelper(com.thoughtworks.go.util.GoConfigFileHelper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Aggregations

MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2338 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1986 Test (org.junit.jupiter.api.Test)1412 lombok.val (lombok.val)946 Test (org.junit.Test)558 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)484 MockServletContext (org.springframework.mock.web.MockServletContext)462 MockRequestContext (org.springframework.webflow.test.MockRequestContext)460 MockFilterChain (org.springframework.mock.web.MockFilterChain)239 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)184 JEEContext (org.pac4j.core.context.JEEContext)159 FilterChain (jakarta.servlet.FilterChain)117 Authentication (org.springframework.security.core.Authentication)116 BeforeEach (org.junit.jupiter.api.BeforeEach)106 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)97 HashMap (java.util.HashMap)84 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)83 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)67 MockHttpSession (org.springframework.mock.web.MockHttpSession)65 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)64