@Test public void simpleStringKeyStringValueFormData() throws Exception { HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration"); MockRestTemplate template = new MockRestTemplate(); new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template); handler.setHttpMethod(HttpMethod.POST); setBeanFactory(handler); handler.afterPropertiesSet(); Map<String, String> form = new LinkedHashMap<String, String>(); form.put("a", "1"); form.put("b", "2"); form.put("c", "3"); Message<?> message = MessageBuilder.withPayload(form).build(); QueueChannel replyChannel = new QueueChannel(); handler.setOutputChannel(replyChannel); Exception exception = null; try { handler.handleMessage(message); } catch (Exception e) { exception = e; } assertEquals("intentional", exception.getCause().getMessage()); HttpEntity<?> request = template.lastRequestEntity.get(); Object body = request.getBody(); assertNotNull(request.getHeaders().getContentType()); assertTrue(body instanceof MultiValueMap<?, ?>); MultiValueMap<?, ?> map = (MultiValueMap<?, ?>) body; assertEquals("1", map.get("a").iterator().next()); assertEquals("2", map.get("b").iterator().next()); assertEquals("3", map.get("c").iterator().next()); assertEquals(MediaType.APPLICATION_FORM_URLENCODED, request.getHeaders().getContentType()); }
@Test public void simpleObjectKeyObjectValueFormData() throws Exception { HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration"); MockRestTemplate template = new MockRestTemplate(); new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template); handler.setHttpMethod(HttpMethod.POST); setBeanFactory(handler); handler.afterPropertiesSet(); Map<Object, Object> form = new LinkedHashMap<Object, Object>(); form.put(1, new City("Philadelphia")); form.put(2, new City("Ambler")); form.put(3, new City("Mohnton")); Message<?> message = MessageBuilder.withPayload(form).build(); QueueChannel replyChannel = new QueueChannel(); handler.setOutputChannel(replyChannel); Exception exception = null; try { handler.handleMessage(message); } catch (Exception e) { exception = e; } assertEquals("intentional", exception.getCause().getMessage()); HttpEntity<?> request = template.lastRequestEntity.get(); Object body = request.getBody(); assertTrue(body instanceof Map<?, ?>); Map<?, ?> map = (Map<?, ?>) body; assertEquals("Philadelphia", map.get(1).toString()); assertEquals("Ambler", map.get(2).toString()); assertEquals("Mohnton", map.get(3).toString()); assertEquals("application", request.getHeaders().getContentType().getType()); assertEquals("x-java-serialized-object", request.getHeaders().getContentType().getSubtype()); }