Java Code Examples for org.springframework.mock.web.MockHttpServletRequest#getAttribute()

The following examples show how to use org.springframework.mock.web.MockHttpServletRequest#getAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Cas30ResponseViewTests.java    From springboot-shiro-cas-mybatis with MIT License 6 votes vote down vote up
private Map<?, ?> renderView() throws Exception{
    final ModelAndView modelAndView = this.getModelAndViewUponServiceValidationWithSecurePgtUrl();
    final JstlView v = (JstlView) resolver.resolveViewName(modelAndView.getViewName(), Locale.getDefault());
    final MockHttpServletRequest req = new MockHttpServletRequest(new MockServletContext());
    v.setServletContext(req.getServletContext());
    req.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            new GenericWebApplicationContext(req.getServletContext()));

    final Cas30ResponseView view = new Cas30ResponseView(v);
    view.setServicesManager(this.servicesManager);
    view.setCasAttributeEncoder(new DefaultCasAttributeEncoder(this.servicesManager));

    final MockHttpServletResponse resp = new MockHttpServletResponse();
    view.render(modelAndView.getModel(), req, resp);
    return (Map<?, ?>) req.getAttribute(CasProtocolConstants.VALIDATION_CAS_MODEL_ATTRIBUTE_NAME_ATTRIBUTES);
}
 
Example 2
Source File: RequestCorrelationFilterTest.java    From request-correlation-spring-cloud-starter with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUseExistingCorrelationId() throws IOException, ServletException {

    // given
    final String requestId = UUID.randomUUID().toString();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockFilterChain chain = new MockFilterChain();

    request.addHeader(RequestCorrelationConsts.HEADER_NAME, requestId);

    // when
    instance.doFilter(request, response, chain);

    // then
    final Object requestCorrelation = request.getAttribute(RequestCorrelationConsts.ATTRIBUTE_NAME);
    assertNotNull(requestCorrelation);
    assertEquals(requestId, ((RequestCorrelation) requestCorrelation).getRequestId());

    final String header = ((HttpServletRequest) chain.getRequest()).getHeader(RequestCorrelationConsts.HEADER_NAME);
    assertNotNull(header);
    assertEquals(requestId, header);
}
 
Example 3
Source File: RequestCorrelationFilterTest.java    From request-correlation-spring-cloud-starter with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUseCustomHeader() throws IOException, ServletException {

    // given
    final String headerName = "X-TraceId";
    final String requestId = UUID.randomUUID().toString();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockFilterChain chain = new MockFilterChain();

    request.addHeader(headerName, requestId);
    properties.setHeaderName(headerName);

    // when
    instance.doFilter(request, response, chain);

    // then
    final Object requestCorrelation = request.getAttribute(RequestCorrelationConsts.ATTRIBUTE_NAME);
    assertNotNull(requestCorrelation);
    assertEquals(requestId, ((RequestCorrelation) requestCorrelation).getRequestId());

    final String header = ((HttpServletRequest) chain.getRequest()).getHeader(headerName);
    assertNotNull(header);
    assertEquals(requestId, header);
}
 
Example 4
Source File: RequestCorrelationFilterTest.java    From request-correlation-spring-cloud-starter with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldInvokeInterceptor() throws IOException, ServletException {

    // given
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockFilterChain chain = new MockFilterChain();

    final RequestCorrelationInterceptor interceptor = mock(RequestCorrelationInterceptor.class);
    interceptors.add(interceptor);

    // when
    instance.doFilter(request, response, chain);

    // then
    final String requestId = ((HttpServletRequest) chain.getRequest()).getHeader(RequestCorrelationConsts.HEADER_NAME);
    final RequestCorrelation correlationId = (RequestCorrelation) request.getAttribute(RequestCorrelationConsts.ATTRIBUTE_NAME);
    assertNotNull(requestId);
    assertNotNull(correlationId);
    assertEquals(requestId, correlationId.getRequestId());

    verify(interceptor).afterCorrelationIdSet(requestId);
    verify(interceptor).cleanUp(requestId);
}
 
Example 5
Source File: ScriptFilterTest.java    From engine with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testFilter() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/mymovies");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = mock(FilterChain.class);

    setCurrentRequestContext(request, response);
    setCurrentSiteContext(siteContext);

    filter.doFilter(request, response, filterChain);

    String greeting = (String)request.getAttribute("greeting");

    assertNotNull(greeting);
    assertEquals("Hello World!", greeting);
    assertEquals(400, response.getStatus());
    assertEquals("You're not a subscriber", response.getErrorMessage());

    verify(filterChain, never()).doFilter(request, response);

    clearCurrentRequestContext();
}
 
Example 6
Source File: ScriptFilterTest.java    From engine with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testFilterExclude() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/static-assets/js/app.js");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = mock(FilterChain.class);

    setCurrentRequestContext(request, response);
    setCurrentSiteContext(siteContext);

    filter.doFilter(request, response, filterChain);

    String greeting = (String)request.getAttribute("greeting");

    assertNotNull(greeting);
    assertEquals("Hello World!", greeting);
    assertEquals(200, response.getStatus());

    verify(filterChain).doFilter(request, response);

    clearCurrentRequestContext();
}
 
Example 7
Source File: MvcTest.java    From ManagementSystem with Apache License 2.0 5 votes vote down vote up
@Test
public void testPage() throws Exception {
    //模拟请求拿到返回值

    MvcResult result =  mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn","1")).andReturn();

    //请求成功后,请求中会有pageInfo,我们可以取出pageInfo进行校验

    MockHttpServletRequest request = result.getRequest();

    PageInfo pi = (PageInfo) request.getAttribute("pageInfo");

    System.out.println("当前页码:" + pi.getPageNum());
    System.out.println("总页码:" + pi.getPages());
    System.out.println("总记录数:" + pi.getTotal());
    System.out.println("连续显示的页码:");
    int[] nums = pi.getNavigatepageNums();
    for (int i : nums){
        System.out.print(" " + i);
    }

    //获取员工数据
    List<Employee> list = pi.getList();
    for (Employee employee : list){
        System.out.println("ID" + employee.getEmpId()+"name" + employee.getEmpName());
    }
}
 
Example 8
Source File: OperationAttributeHelper.java    From spring-auto-restdocs with Apache License 2.0 4 votes vote down vote up
public static void initRequestPattern(MockHttpServletRequest request) {
    String requestPattern = (String) request.getAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE);
    ((Map) request.getAttribute(ATTRIBUTE_NAME_CONFIGURATION))
            .put(REQUEST_PATTERN, requestPattern);
}