private ModelAndView getModelAndViewSettingsPage( Map<String, String> parameters, SettingsForm settingsForm, MockHttpServletRequest request) { if (settingsForm == null) { settingsForm = new SettingsForm(); } WebDataBinder binder = new WebDataBinder(settingsForm, "settingsform"); if (parameters != null) { request.setParameters(parameters); binder.bind(new MutablePropertyValues(request.getParameterMap())); } SessionStatus status = new SimpleSessionStatus(); return controller.submitSettingsPage(settingsForm, binder.getBindingResult(), status, request); }
@Test public void setMultipleParameters() { request.setParameter("key1", "value1"); request.setParameter("key2", "value2"); Map<String, Object> params = new HashMap<String, Object>(2); params.put("key1", "newValue1"); params.put("key3", new String[] {"value3A", "value3B"}); request.setParameters(params); String[] values1 = request.getParameterValues("key1"); assertEquals(1, values1.length); assertEquals("newValue1", request.getParameter("key1")); assertEquals("value2", request.getParameter("key2")); String[] values3 = request.getParameterValues("key3"); assertEquals(2, values3.length); assertEquals("value3A", values3[0]); assertEquals("value3B", values3[1]); }
protected RepositoryRestRequest getRequest(Class<?> domainType, RequestParameters parameters) { Assert.notNull(domainType, "Domain type must not be null!"); MockHttpServletRequest request = new MockHttpServletRequest(); request.setParameters(parameters.asMap()); ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); PersistentEntity<?, ?> entity = repositories.getPersistentEntity(domainType); return new RepositoryRestRequest( entity, new ServletWebRequest(request), mappings.getMappingFor(domainType), invokerFactory.getInvokerFor(domainType)); }
@Test @SuppressWarnings("unchecked") // INT-1362 public void testURIVariablesAndHeaders() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("GET"); String testRequest = "aBc;q1=1;q2=2"; String requestURI = "/test/" + testRequest; request.setRequestURI(requestURI); request.setContentType("text/plain"); final Map<String, String> params = new HashMap<String, String>(); params.put("foo", "bar"); request.setParameters(params); request.setContent("hello".getBytes()); final Cookie cookie = new Cookie("foo", "bar"); request.setCookies(cookie); request.addHeader("toLowerCase", true); // See org.springframework.web.servlet.FrameworkServlet#initContextHolders final RequestAttributes attributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(attributes); this.toLowerCaseChannel.subscribe( new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { MessageHeaders headers = message.getHeaders(); assertEquals(attributes, headers.get("requestAttributes")); Object requestParams = headers.get("requestParams"); assertNotNull(requestParams); assertEquals( params, ((MultiValueMap<String, String>) requestParams).toSingleValueMap()); Object matrixVariables = headers.get("matrixVariables"); assertThat(matrixVariables, Matchers.instanceOf(Map.class)); Object value = ((Map<?, ?>) matrixVariables).get("value"); assertThat(value, Matchers.instanceOf(MultiValueMap.class)); assertEquals("1", ((MultiValueMap<String, ?>) value).getFirst("q1")); assertEquals("2", ((MultiValueMap<String, ?>) value).getFirst("q2")); Object requestHeaders = headers.get("requestHeaders"); assertNotNull(requestParams); assertEquals(MediaType.TEXT_PLAIN, ((HttpHeaders) requestHeaders).getContentType()); Map<String, Cookie> cookies = (Map<String, Cookie>) headers.get("cookies"); assertEquals(1, cookies.size()); Cookie foo = cookies.get("foo"); assertNotNull(foo); assertEquals(cookie, foo); } }); MockHttpServletResponse response = new MockHttpServletResponse(); Object handler = this.handlerMapping.getHandler(request).getHandler(); this.handlerAdapter.handle(request, response, handler); final String testResponse = response.getContentAsString(); assertEquals(testRequest.split(";")[0].toLowerCase(), testResponse); RequestContextHolder.resetRequestAttributes(); }