Search in sources :

Example 96 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project entando-core by entando.

the class ApsAdminBaseTestCase method setUp.

@Override
protected void setUp() throws Exception {
    boolean refresh = false;
    if (null == _applicationContext) {
        // Link the servlet context and the Spring context
        _servletContext = new MockServletContext("", new FileSystemResourceLoader());
        _applicationContext = this.getConfigUtils().createApplicationContext(_servletContext);
        _servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, _applicationContext);
    } else {
        refresh = true;
    }
    this._request = new MockHttpServletRequest();
    this._response = new MockHttpServletResponse();
    this._request.setSession(new MockHttpSession(this._servletContext));
    if (refresh) {
        try {
            ApsWebApplicationUtils.executeSystemRefresh(this._request);
            this.waitNotifyingThread();
        } catch (Throwable e) {
        }
    }
    // Use spring as the object factory for Struts
    StrutsSpringObjectFactory ssf = new StrutsSpringObjectFactory(null, null, null, null, _servletContext, null, this.createContainer());
    ssf.setApplicationContext(_applicationContext);
    // Dispatcher is the guy that actually handles all requests.  Pass in
    // an empty Map as the parameters but if you want to change stuff like
    // what config files to read, you need to specify them here
    // (see Dispatcher's source code)
    java.net.URL url = ClassLoader.getSystemResource("struts.properties");
    Properties props = new Properties();
    props.load(url.openStream());
    this.setInitParameters(props);
    Map params = new HashMap(props);
    this._dispatcher = new Dispatcher(_servletContext, params);
    this._dispatcher.init();
    Dispatcher.setInstance(this._dispatcher);
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FileSystemResourceLoader(org.springframework.core.io.FileSystemResourceLoader) Properties(java.util.Properties) Dispatcher(org.apache.struts2.dispatcher.Dispatcher) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpSession(org.springframework.mock.web.MockHttpSession) StrutsSpringObjectFactory(org.apache.struts2.spring.StrutsSpringObjectFactory) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 97 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project opennms by OpenNMS.

the class LdapAuthTest method assertAccessAllowed.

/**
 * @param request
 * @throws IOException
 * @throws ServletException
 */
private void assertAccessAllowed(MockHttpServletRequest request) throws IOException, ServletException {
    MockHttpServletResponse response = new MockHttpServletResponse();
    m_authFilterChain.doFilter(request, response, m_chain);
    assertEquals(200, response.getStatus());
    m_chain.assertAccessAllowed();
}
Also used : MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 98 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project CzechIdMng by bcvsolutions.

the class NotificationRestTest method testSendNotification.

@Test
public void testSendNotification() throws Exception {
    final IdmIdentityDto sender = createTestIdentity(TEST_SENDER_1);
    final IdmIdentityDto recipient = createTestIdentity(TEST_RECIPIENT_1);
    // 
    final IdmNotificationDto notif = createTestNotification(NotificationLevel.INFO, TEST_SUBJECT, TEST_MESSAGE, TEST_TOPIC, sender, recipient);
    final String jsonContent = jsonify(notif);
    // 
    MockHttpServletResponse response = getMockMvc().perform(MockMvcRequestBuilders.post(BaseDtoController.BASE_PATH + "/notifications").with(authentication(getAuthentication())).contentType(MediaTypes.HAL_JSON).content(jsonContent)).andReturn().getResponse();
    // 
    assertEquals(200, response.getStatus());
}
Also used : IdmNotificationDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest) Test(org.junit.Test)

Example 99 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project CzechIdMng by bcvsolutions.

the class AbstractSwaggerTest method convertSwagger.

/**
 * Converts module's swagger endpoint to json
 *
 * @see ModuleDescriptor#getId()
 * @param moduleId
 * @throws Exception
 */
public void convertSwagger(String moduleId) throws Exception {
    MvcResult mvcResult = getMockMvc().perform(get(String.format("%s?group=%s", path, moduleId)).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn();
    MockHttpServletResponse response = mvcResult.getResponse();
    String swaggerJson = response.getContentAsString();
    Files.createDirectories(Paths.get(outputDir));
    try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputDir, filename), StandardCharsets.UTF_8)) {
        writer.write(swaggerJson);
    }
}
Also used : MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) BufferedWriter(java.io.BufferedWriter)

Example 100 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project vboard by voyages-sncf-technologies.

the class RssControllerTest method modelAndViewAsString.

private String modelAndViewAsString(final ModelAndView mav) throws Exception {
    final MockHttpServletRequest mockReq = new MockHttpServletRequest();
    final MockHttpServletResponse mockResp = new MockHttpServletResponse();
    mav.getView().render(mav.getModel(), mockReq, mockResp);
    return mockResp.getContentAsString();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

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