Search in sources :

Example 56 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-security by spring-projects.

the class DefaultLoginPageGeneratingFilterTests method generatingPageWithOpenIdFilterOnlyIsSuccessFul.

@Test
public void generatingPageWithOpenIdFilterOnlyIsSuccessFul() throws Exception {
    DefaultLoginPageGeneratingFilter filter = new DefaultLoginPageGeneratingFilter(new MockProcessingFilter());
    filter.doFilter(new MockHttpServletRequest("GET", "/login"), new MockHttpServletResponse(), chain);
}
Also used : DefaultLoginPageGeneratingFilter(org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 57 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-security by spring-projects.

the class RequestCacheAwareFilterTests method savedRequestIsRemovedAfterMatch.

@Test
public void savedRequestIsRemovedAfterMatch() throws Exception {
    RequestCacheAwareFilter filter = new RequestCacheAwareFilter();
    HttpSessionRequestCache cache = new HttpSessionRequestCache();
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/destination");
    MockHttpServletResponse response = new MockHttpServletResponse();
    cache.saveRequest(request, response);
    assertThat(request.getSession().getAttribute(HttpSessionRequestCache.SAVED_REQUEST)).isNotNull();
    filter.doFilter(request, response, new MockFilterChain());
    assertThat(request.getSession().getAttribute(HttpSessionRequestCache.SAVED_REQUEST)).isNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 58 with MockHttpServletResponse

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

the class RTQueryApisTest method retrieveVersionInfo.

@Test
public void retrieveVersionInfo() throws Exception {
    try {
        RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rtquery/v1/version-info").accept(MediaType.APPLICATION_JSON);
        ResultActions actions = mockMvc.perform(requestBuilder);
        MvcResult result = actions.andReturn();
        MockHttpServletResponse response = result.getResponse();
        assertEquals(200, response.getStatus());
        String body = response.getContentAsString();
        // System.out.println(body);
        Map<String, Object> map = Utils.toMap(body);
        assertTrue(map.containsKey("timestamp"));
        assertTrue(map.containsKey("duration"));
        assertTrue(map.containsKey("data"));
        assertTrue(map.containsKey("trace_id"));
        Map<String, Object> data = (Map<String, Object>) map.get("data");
        assertTrue(data.size() > 0);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) ResultActions(org.springframework.test.web.servlet.ResultActions) MvcResult(org.springframework.test.web.servlet.MvcResult) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 59 with MockHttpServletResponse

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

the class RTQueryApisTest method retrieveLocalCacheData.

@Test
public void retrieveLocalCacheData() throws Exception {
    try {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("test-data.json");
        assertNotNull(inputStream);
        String text = null;
        try (final Reader reader = new InputStreamReader(inputStream)) {
            text = CharStreams.toString(reader);
        }
        assertNotNull(text);
        Map<String, Object> testData = Utils.toMap(text);
        assertNotNull(testData);
        Mockito.when(localCache.listAllData()).thenReturn(testData);
        PowerMockito.mockStatic(AppCtx.class);
        BDDMockito.when(AppCtx.getLocalCache()).thenReturn(localCache);
        RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rtquery/v1/cache/data").accept(MediaType.APPLICATION_JSON);
        ResultActions actions = mockMvc.perform(requestBuilder);
        MvcResult result = actions.andReturn();
        MockHttpServletResponse response = result.getResponse();
        assertEquals(response.getStatus(), 200);
        String body = response.getContentAsString();
        // System.out.println(body);
        Map<String, Object> map = Utils.toMap(body);
        assertTrue(map.containsKey("timestamp"));
        assertTrue(map.containsKey("duration"));
        assertTrue(map.containsKey("data"));
        assertTrue(map.containsKey("trace_id"));
        Map<String, Object> data = (Map<String, Object>) map.get("data");
        assertTrue(data.size() > 0);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ResultActions(org.springframework.test.web.servlet.ResultActions) MvcResult(org.springframework.test.web.servlet.MvcResult) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 60 with MockHttpServletResponse

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

the class RTQueryApisTest method retrieveLocalCacheConfig.

@Test
public void retrieveLocalCacheConfig() throws Exception {
    RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rtquery/v1/cache/config").accept(MediaType.APPLICATION_JSON);
    ResultActions actions = mockMvc.perform(requestBuilder);
    MvcResult result = actions.andReturn();
    MockHttpServletResponse response = result.getResponse();
    assertEquals(200, response.getStatus());
    String body = response.getContentAsString();
    // System.out.println(body);
    Map<String, Object> map = Utils.toMap(body);
    assertTrue(map.containsKey("timestamp"));
    assertTrue(map.containsKey("duration"));
    assertTrue(map.containsKey("data"));
    assertTrue(map.containsKey("trace_id"));
    Map<String, Object> data = (Map<String, Object>) map.get("data");
    assertTrue(data.size() > 0);
}
Also used : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) ResultActions(org.springframework.test.web.servlet.ResultActions) MvcResult(org.springframework.test.web.servlet.MvcResult) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

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