@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());
 }
  @SuppressWarnings("cast")
  @Test
  public void contentAsByteArray() 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();

    byte[] bytes = "Hello World".getBytes();
    Message<?> message = MessageBuilder.withPayload(bytes).build();
    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 byte[]);
    assertEquals("Hello World", new String(bytes));
    assertEquals(MediaType.APPLICATION_OCTET_STREAM, request.getHeaders().getContentType());
  }
  @Test
  public void compatibleConversionService() throws Exception {
    HttpRequestExecutingMessageHandler handler =
        new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
    ConfigurableListableBeanFactory bf = new DefaultListableBeanFactory();
    ProxyFactory pf =
        new ProxyFactory(new Class<?>[] {ConversionService.class, ConverterRegistry.class});
    final AtomicInteger converterCount = new AtomicInteger();
    pf.addAdvice(
        new MethodInterceptor() {

          public Object invoke(MethodInvocation invocation) throws Throwable {
            if (invocation.getMethod().getName().equals("addConverter")) {
              converterCount.incrementAndGet();
            }
            return null;
          }
        });
    ConversionService mockConversionService = (ConversionService) pf.getProxy();
    bf.registerSingleton("integrationConversionService", mockConversionService);
    handler.setBeanFactory(bf);
    handler.afterPropertiesSet();
    assertEquals(2, converterCount.get());
    assertSame(mockConversionService, TestUtils.getPropertyValue(handler, "conversionService"));
  }
 @Test
 public void nameOnlyWithNullValues() 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, Object> form = new LinkedHashMap<String, Object>();
   form.put("a", null);
   form.put("b", "foo");
   form.put("c", null);
   Message<?> message = MessageBuilder.withPayload(form).build();
   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 MultiValueMap<?, ?>);
   MultiValueMap<?, ?> map = (MultiValueMap<?, ?>) body;
   assertTrue(map.containsKey("a"));
   assertTrue(map.get("a").size() == 1);
   assertNull(map.get("a").get(0));
   List<?> entryB = map.get("b");
   assertEquals("foo", entryB.get(0));
   assertTrue(map.containsKey("c"));
   assertTrue(map.get("c").size() == 1);
   assertNull(map.get("c").get(0));
   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());
 }
  @Test // no asertions just a warn message in a log
  public void testWarnMessageForNonPostPutAndExtractPayload() throws Exception {
    // should see a warn message

    HttpRequestExecutingMessageHandler handler =
        new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
    MockRestTemplate template = new MockRestTemplate();
    new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template);
    handler.setHttpMethod(HttpMethod.GET);
    handler.setExtractPayload(true);
    setBeanFactory(handler);
    handler.afterPropertiesSet();

    // should not see  a warn message since 'setExtractPayload' is not set explicitly

    handler =
        new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
    template = new MockRestTemplate();
    new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template);
    handler.setHttpMethod(HttpMethod.GET);
    setBeanFactory(handler);
    handler.afterPropertiesSet();

    // should not see  a warn message since HTTP method is not GET

    handler =
        new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
    template = new MockRestTemplate();
    new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template);
    handler.setHttpMethod(HttpMethod.POST);
    handler.setExtractPayload(true);
    setBeanFactory(handler);
    handler.afterPropertiesSet();
  }
  @Test
  public void stringKeyPrimitiveArrayValueMixedFormData() 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, Object> form = new LinkedHashMap<String, Object>();
    form.put("a", new int[] {1, 2, 3});
    form.put("b", "4");
    form.put("c", new String[] {"5"});
    form.put("d", "6");
    Message<?> message = MessageBuilder.withPayload(form).build();
    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 MultiValueMap<?, ?>);
    MultiValueMap<?, ?> map = (MultiValueMap<?, ?>) body;

    List<?> aValue = map.get("a");
    assertEquals(1, aValue.size());
    Object value = aValue.get(0);
    assertTrue(value.getClass().isArray());
    int[] y = (int[]) value;
    assertEquals(1, y[0]);
    assertEquals(2, y[1]);
    assertEquals(3, y[2]);

    List<?> bValue = map.get("b");
    assertEquals(1, bValue.size());
    assertEquals("4", bValue.get(0));

    List<?> cValue = map.get("c");
    assertEquals(1, cValue.size());
    assertEquals("5", cValue.get(0));

    List<?> dValue = map.get("d");
    assertEquals(1, dValue.size());
    assertEquals("6", dValue.get(0));
    assertEquals(MediaType.MULTIPART_FORM_DATA, request.getHeaders().getContentType());
  }
 @Test
 public void nonCompatibleConversionService() throws Exception {
   HttpRequestExecutingMessageHandler handler =
       new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
   ConfigurableListableBeanFactory bf = new DefaultListableBeanFactory();
   ConversionService mockConversionService = mock(ConversionService.class);
   bf.registerSingleton("integrationConversionService", mockConversionService);
   handler.setBeanFactory(bf);
   try {
     handler.afterPropertiesSet();
   } catch (Exception e) {
     fail("Unexpected exception during initialization " + e.getMessage());
   }
   assertSame(mockConversionService, TestUtils.getPropertyValue(handler, "conversionService"));
 }
 @Test
 public void testUriExpression() {
   MockRestTemplate restTemplate = new MockRestTemplate();
   HttpRequestExecutingMessageHandler handler =
       new HttpRequestExecutingMessageHandler(
           new SpelExpressionParser().parseExpression("headers['foo']"), restTemplate);
   setBeanFactory(handler);
   handler.afterPropertiesSet();
   String theURL = "http://bar/baz?foo#bar";
   Message<?> message = MessageBuilder.withPayload("").setHeader("foo", theURL).build();
   try {
     handler.handleRequestMessage(message);
   } catch (Exception e) {
   }
   assertEquals(theURL, restTemplate.actualUrl.get());
 }
  @Test
  public void acceptHeaderForSerializableResponseMessageExchange() throws Exception {
    HttpRequestExecutingMessageHandler handler =
        new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");

    handler.setHttpMethod(HttpMethod.GET);
    handler.setExtractPayload(false);
    handler.setExpectedResponseType(GenericMessage.class);

    List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
    converters.add(new SerializingHttpMessageConverter());
    handler.setMessageConverters(converters);
    setBeanFactory(handler);
    handler.afterPropertiesSet();

    RestTemplate restTemplate =
        TestUtils.getPropertyValue(handler, "restTemplate", RestTemplate.class);

    HttpHeaders requestHeaders = setUpMocksToCaptureSentHeaders(restTemplate);

    Message<?> message = MessageBuilder.withPayload("foo").build();
    Exception exception = null;
    try {
      handler.handleMessage(message);
    } catch (Exception e) {
      exception = e;
    }
    assertTrue(requestHeaders.getAccept() != null);
    assertTrue(requestHeaders.getAccept().size() > 0);
    assertEquals("404 Not Found", exception.getCause().getMessage());
    List<MediaType> accept = requestHeaders.getAccept();
    assertTrue(accept != null && accept.size() > 0);
    assertEquals("application", accept.get(0).getType());
    assertEquals("x-java-serialized-object", accept.get(0).getSubtype());
  }
 @Test
 public void testInt2455UriNotEncoded() {
   MockRestTemplate restTemplate = new MockRestTemplate();
   HttpRequestExecutingMessageHandler handler =
       new HttpRequestExecutingMessageHandler(
           new SpelExpressionParser().parseExpression("'http://my.RabbitMQ.com/api/' + payload"),
           restTemplate);
   handler.setEncodeUri(false);
   setBeanFactory(handler);
   handler.afterPropertiesSet();
   Message<?> message = MessageBuilder.withPayload("queues/%2f/si.test.queue?foo#bar").build();
   try {
     handler.handleRequestMessage(message);
   } catch (Exception e) {
   }
   assertEquals(
       "http://my.RabbitMQ.com/api/queues/%2f/si.test.queue?foo#bar",
       restTemplate.actualUrl.get());
 }
  @Test
  public void stringKeyObjectCollectionValueFormData() 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, Object> form = new LinkedHashMap<String, Object>();
    List<Object> listA = new ArrayList<Object>();
    listA.add(new City("Philadelphia"));
    listA.add(new City("Ambler"));
    form.put("a", listA);
    form.put("b", Collections.EMPTY_LIST);
    form.put("c", Collections.singletonList(new City("Mohnton")));
    Message<?> message = MessageBuilder.withPayload(form).build();
    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 MultiValueMap<?, ?>);
    MultiValueMap<?, ?> map = (MultiValueMap<?, ?>) body;

    List<?> aValue = map.get("a");
    assertEquals(2, aValue.size());
    assertEquals("Philadelphia", aValue.get(0).toString());
    assertEquals("Ambler", aValue.get(1).toString());

    List<?> bValue = map.get("b");
    assertEquals(0, bValue.size());

    List<?> cValue = map.get("c");
    assertEquals(1, cValue.size());
    assertEquals("Mohnton", cValue.get(0).toString());

    assertEquals(MediaType.MULTIPART_FORM_DATA, request.getHeaders().getContentType());
  }
  /**
   * This test and the one below might look identical, but they are not. This test injected "5" into
   * the list as String resulting in the Content-TYpe being application/x-www-form-urlencoded
   *
   * @throws Exception
   */
  @Test
  public void stringKeyNullCollectionValueMixedFormDataString() 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, Object> form = new LinkedHashMap<String, Object>();
    List<Object> list = new ArrayList<Object>();
    list.add(null);
    list.add("5");
    list.add(null);
    form.put("a", list);
    form.put("b", "4");
    Message<?> message = MessageBuilder.withPayload(form).build();
    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 MultiValueMap<?, ?>);
    MultiValueMap<?, ?> map = (MultiValueMap<?, ?>) body;

    List<?> aValue = map.get("a");
    assertEquals(3, aValue.size());
    assertNull(aValue.get(0));
    assertEquals("5", aValue.get(1));
    assertNull(aValue.get(2));

    List<?> bValue = map.get("b");
    assertEquals(1, bValue.size());
    assertEquals("4", bValue.get(0));

    assertEquals(MediaType.APPLICATION_FORM_URLENCODED, request.getHeaders().getContentType());
  }
  @Test
  public void contentAsXmlSource() 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();

    Message<?> message = MessageBuilder.withPayload(mock(Source.class)).build();
    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 Source);
    assertEquals(MediaType.TEXT_XML, request.getHeaders().getContentType());
  }
 private void setBeanFactory(HttpRequestExecutingMessageHandler handler) {
   handler.setBeanFactory(mock(BeanFactory.class));
 }
  @Test
  public void contentTypeIsNotSetForGetRequest() throws Exception {
    // GET
    HttpRequestExecutingMessageHandler handler =
        new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
    MockRestTemplate template = new MockRestTemplate();
    new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template);
    handler.setHttpMethod(HttpMethod.GET);
    setBeanFactory(handler);
    handler.afterPropertiesSet();

    Message<?> message = MessageBuilder.withPayload(mock(Source.class)).build();
    Exception exception = null;
    try {
      handler.handleMessage(message);
    } catch (Exception e) {
      exception = e;
    }
    assertEquals("intentional", exception.getCause().getMessage());
    HttpEntity<?> request = template.lastRequestEntity.get();
    assertNull(request.getHeaders().getContentType());

    /* TODO: reconsider the inclusion of content-type for various HttpMethods (only ignoring for GET as of 2.0.5)
     *       uncomment code below accordingly (see INT-1951)
     */

    /*
    //HEAD
    handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
    template = new MockRestTemplate();
    new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template);
    handler.setHttpMethod(HttpMethod.HEAD);

    message = MessageBuilder.withPayload(mock(Source.class)).build();
    exception = null;
    try {
    	handler.handleMessage(message);
    }
    catch (Exception e) {
    	exception = e;
    }
    assertEquals("intentional", exception.getCause().getMessage());
    request = template.lastRequestEntity.get();
    assertNull(request.getHeaders().getContentType());

    //DELETE
    handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
    template = new MockRestTemplate();
    new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template);
    handler.setHttpMethod(HttpMethod.DELETE);

    message = MessageBuilder.withPayload(mock(Source.class)).build();
    exception = null;
    try {
    	handler.handleMessage(message);
    }
    catch (Exception e) {
    	exception = e;
    }
    assertEquals("intentional", exception.getCause().getMessage());
    request = template.lastRequestEntity.get();
    assertNull(request.getHeaders().getContentType());

    //TRACE
    handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
    template = new MockRestTemplate();
    new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template);
    handler.setHttpMethod(HttpMethod.TRACE);

    message = MessageBuilder.withPayload(mock(Source.class)).build();
    exception = null;
    try {
    	handler.handleMessage(message);
    }
    catch (Exception e) {
    	exception = e;
    }
    assertEquals("intentional", exception.getCause().getMessage());
    request = template.lastRequestEntity.get();
    assertNull(request.getHeaders().getContentType());
    */
  }