@Override protected void applyParam(String paramName, String[] paramValues) { request.requestAttr(paramName, paramValues[0]); } }.applyParams();
@Test public void singletonController() throws Exception { this.mockMvc.perform(get("/singletonController").requestAttr(FROM_MVC_TEST_MOCK, FROM_MVC_TEST_MOCK)); }
@Test public void singletonController() throws Exception { this.mockMvc.perform(get("/singletonController").requestAttr(FROM_MVC_TEST_MOCK, FROM_MVC_TEST_MOCK)); }
@Test public void loadConfigWhenDefaultConfigThenWebAsyncManagerIntegrationFilterAdded() throws Exception { this.spring.register(WebAsyncPopulatedByDefaultConfig.class).autowire(); WebAsyncManager webAsyncManager = mock(WebAsyncManager.class); this.mockMvc.perform(get("/").requestAttr(WebAsyncUtils.WEB_ASYNC_MANAGER_ATTRIBUTE, webAsyncManager)); ArgumentCaptor<CallableProcessingInterceptor> callableProcessingInterceptorArgCaptor = ArgumentCaptor.forClass(CallableProcessingInterceptor.class); verify(webAsyncManager, atLeastOnce()).registerCallableInterceptor(any(), callableProcessingInterceptorArgCaptor.capture()); CallableProcessingInterceptor callableProcessingInterceptor = callableProcessingInterceptorArgCaptor.getAllValues().stream() .filter(e -> SecurityContextCallableProcessingInterceptor.class.isAssignableFrom(e.getClass())) .findFirst() .orElse(null); assertThat(callableProcessingInterceptor).isNotNull(); }
@Test public void requestAttribute() { this.builder.requestAttr("foo", "bar"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); assertEquals("bar", request.getAttribute("foo")); }
@Test public void requestScopedController() throws Exception { assertTrue("request-scoped controller must be a CGLIB proxy", AopUtils.isCglibProxy(this.requestScopedController)); this.mockMvc.perform(get("/requestScopedController").requestAttr(FROM_MVC_TEST_MOCK, FROM_MVC_TEST_MOCK)); }
@Test public void requestScopedService() throws Exception { assertTrue("request-scoped service must be a CGLIB proxy", AopUtils.isCglibProxy(this.requestScopedService)); this.mockMvc.perform(get("/requestScopedService").requestAttr(FROM_MVC_TEST_MOCK, FROM_MVC_TEST_MOCK)); }
@Test public void sessionScopedService() throws Exception { assertTrue("session-scoped service must be a CGLIB proxy", AopUtils.isCglibProxy(this.sessionScopedService)); this.mockMvc.perform(get("/sessionScopedService").requestAttr(FROM_MVC_TEST_MOCK, FROM_MVC_TEST_MOCK)); }
@Test public void csrfToken() throws Exception { CsrfToken csrfToken = new DefaultCsrfToken("headerName", "paramName", "token"); MockHttpServletRequestBuilder request = get("/csrf").requestAttr( CsrfToken.class.getName(), csrfToken); mockMvc.perform(request).andExpect(assertResult(csrfToken)); }
@Test public void requestWhenConnectMessageThenUsesCsrfTokenHandshakeInterceptor() throws Exception { this.spring.configLocations(xml("SyncConfig")).autowire(); WebApplicationContext context = (WebApplicationContext) this.spring.getContext(); MockMvc mvc = MockMvcBuilders.webAppContextSetup(context).build(); String csrfAttributeName = CsrfToken.class.getName(); String customAttributeName = this.getClass().getName(); MvcResult result = mvc.perform(get("/app") .requestAttr(csrfAttributeName, this.token) .sessionAttr(customAttributeName, "attributeValue")) .andReturn(); CsrfToken handshakeToken = (CsrfToken) this.testHandshakeHandler.attributes.get(csrfAttributeName); String handshakeValue = (String) this.testHandshakeHandler.attributes.get(customAttributeName); String sessionValue = (String) result.getRequest().getSession().getAttribute(customAttributeName); assertThat(handshakeToken).isEqualTo(this.token) .withFailMessage("CsrfToken is populated"); assertThat(handshakeValue).isEqualTo(sessionValue) .withFailMessage("Explicitly listed session variables are not overridden"); }
@Test public void requestWhenConnectMessageAndUsingSockJsThenUsesCsrfTokenHandshakeInterceptor() throws Exception { this.spring.configLocations(xml("SyncSockJsConfig")).autowire(); WebApplicationContext context = (WebApplicationContext) this.spring.getContext(); MockMvc mvc = MockMvcBuilders.webAppContextSetup(context).build(); String csrfAttributeName = CsrfToken.class.getName(); String customAttributeName = this.getClass().getName(); MvcResult result = mvc.perform(get("/app/289/tpyx6mde/websocket") .requestAttr(csrfAttributeName, this.token) .sessionAttr(customAttributeName, "attributeValue")) .andReturn(); CsrfToken handshakeToken = (CsrfToken) this.testHandshakeHandler.attributes.get(csrfAttributeName); String handshakeValue = (String) this.testHandshakeHandler.attributes.get(customAttributeName); String sessionValue = (String) result.getRequest().getSession().getAttribute(customAttributeName); assertThat(handshakeToken).isEqualTo(this.token) .withFailMessage("CsrfToken is populated"); assertThat(handshakeValue).isEqualTo(sessionValue) .withFailMessage("Explicitly listed session variables are not overridden"); }
@Test public void mergeRequestAttribute() throws Exception { String attrName = "PARENT"; String attrValue = "VALUE"; MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()) .defaultRequest(get("/").requestAttr(attrName, attrValue)) .build(); assertThat(mockMvc.perform(requestBuilder).andReturn().getRequest().getAttribute(attrName), equalTo(attrValue)); }
request.requestAttr(ATTRIBUTE_NAME_URL_TEMPLATE, PathSupport.getPath(uri));
if (status.value() >= 400) { requestBuilder = request(HttpMethod.GET, "/error") .requestAttr(RequestDispatcher.ERROR_STATUS_CODE, status.value()) .requestAttr(RequestDispatcher.ERROR_REQUEST_URI, uri.toString()); if (servletResponse.getErrorMessage() != null) { requestBuilder.requestAttr(RequestDispatcher.ERROR_MESSAGE, servletResponse.getErrorMessage());
@Before public void setup() { this.mockRequest.setAttribute(FROM_TCF_MOCK, FROM_TCF_MOCK); this.mockMvc = webAppContextSetup(this.wac) .addFilters(new RequestFilter(), new RequestAttributesFilter(), this.filterWithSessionScopedService) .defaultRequest(get("/").requestAttr(FROM_MVC_TEST_DEFAULT, FROM_MVC_TEST_DEFAULT)) .alwaysExpect(status().isOk()) .build(); }
/** * Create a {@link MockHttpServletRequestBuilder} for a PUT request. The url template * will be captured and made available for documentation. * @param urlTemplate a URL template; the resulting URL will be encoded * @param urlVariables zero or more URL variables * @return the builder for the PUT request */ public static MockHttpServletRequestBuilder put(String urlTemplate, Object... urlVariables) { return MockMvcRequestBuilders.put(urlTemplate, urlVariables).requestAttr( RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate); }
/** * Create a {@link MockHttpServletRequestBuilder} for a DELETE request. The url * template will be captured and made available for documentation. * @param urlTemplate a URL template; the resulting URL will be encoded * @param urlVariables zero or more URL variables * @return the builder for the DELETE request */ public static MockHttpServletRequestBuilder delete(String urlTemplate, Object... urlVariables) { return MockMvcRequestBuilders.delete(urlTemplate, urlVariables).requestAttr( RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate); }
/** * Create a {@link MockHttpServletRequestBuilder} for an OPTIONS request. The url * template will be captured and made available for documentation. * @param urlTemplate a URL template; the resulting URL will be encoded * @param urlVariables zero or more URL variables * @return the builder for the OPTIONS request */ public static MockHttpServletRequestBuilder options(String urlTemplate, Object... urlVariables) { return MockMvcRequestBuilders.options(urlTemplate, urlVariables).requestAttr( RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate); }
/** * Create a {@link MockHttpServletRequestBuilder} for a POST request. The url template * will be captured and made available for documentation. * @param urlTemplate a URL template; the resulting URL will be encoded * @param urlVariables zero or more URL variables * @return the builder for the POST request */ public static MockHttpServletRequestBuilder post(String urlTemplate, Object... urlVariables) { return MockMvcRequestBuilders.post(urlTemplate, urlVariables).requestAttr( RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate); }
@Before public void setUp() { ServletContext servletContext = new MockServletContext(); MockHttpServletRequest mockRequest = new MockHttpServletRequest(servletContext); mockRequest.setAttribute(FROM_CUSTOM_MOCK, FROM_CUSTOM_MOCK); RequestContextHolder.setRequestAttributes(new ServletWebRequest(mockRequest, new MockHttpServletResponse())); this.wac.setServletContext(servletContext); new AnnotatedBeanDefinitionReader(this.wac).register(WebConfig.class); this.wac.refresh(); this.mockMvc = webAppContextSetup(this.wac) .defaultRequest(get("/").requestAttr(FROM_MVC_TEST_DEFAULT, FROM_MVC_TEST_DEFAULT)) .alwaysExpect(status().isOk()) .build(); }