HttpMessageConverter implements MessageConverter<HttpEntity<?>, HttpEntity<?>, HttpEndpointConfiguration> { @Override public HttpEntity<?> convertOutbound(Message message, HttpEndpointConfiguration endpointConfiguration, TestContext context) { HttpMessage httpMessage = convertOutboundMessage(message); HttpHeaders httpHeaders = createHttpHeaders(httpMessage, endpointConfiguration); Object payload = httpMessage.getPayload(); if (httpMessage.getStatusCode() != null) { return new ResponseEntity<>(payload, httpHeaders, httpMessage.getStatusCode()); } else { for (Cookie cookie : httpMessage.getCookies()) { httpHeaders.add( HttpHeaders.COOKIE, cookie.getName() + "=" + context.replaceDynamicContentInString(cookie.getValue())); } } HttpMethod method = determineHttpMethod(endpointConfiguration, httpMessage); return createHttpEntity(httpHeaders, payload, method); } HttpMessageConverter();  HttpMessageConverter(CookieConverter cookieConverter); @Override HttpEntity<?> convertOutbound(Message message,
                                         HttpEndpointConfiguration endpointConfiguration,
                                         TestContext context); @Override HttpMessage convertInbound(HttpEntity<?> message,
                                      HttpEndpointConfiguration endpointConfiguration,
                                      TestContext context); @Override void convertOutbound(HttpEntity externalMessage,
                                Message internalMessage,
                                HttpEndpointConfiguration endpointConfiguration,
                                TestContext context);  }
@Test public void testDefaultMessageIsConvertedOnOutbound(){ Message message = new DefaultMessage(payload); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); assertEquals(payload, httpEntity.getBody()); }
@Test public void testHttpMessageCookiesArePreservedOnOutbound(){ Cookie cookie = new Cookie("foo","bar"); message.cookie(cookie); String expectedCookie = "foo=bar"; final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); final List<String> cookies = httpEntity.getHeaders().get("Cookie"); assert cookies != null; assertEquals(1, cookies.size()); assertEquals(expectedCookie, cookies.get(0)); }
@Test public void testHttpMessageCookiesValuesAreReplacedOnOutbound(){ Cookie cookie = new Cookie("foo","${foobar}"); message.cookie(cookie); testContext.setVariable("foobar", "bar"); String expectedCookie = "foo=bar"; final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); final List<String> cookies = httpEntity.getHeaders().get("Cookie"); assert cookies != null; assertEquals(1, cookies.size()); assertEquals(expectedCookie, cookies.get(0)); }
@Test public void testHttpMessageHeadersAreReplacedOnOutbound(){ message.header("foo","bar"); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); final List<String> fooHeader = httpEntity.getHeaders().get("foo"); assert fooHeader != null; assertEquals(1, fooHeader.size()); assertEquals("bar", fooHeader.get(0)); }
@Test public void testHttpContentTypeIsPresent(){ endpointConfiguration.setContentType("foobar"); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); final List<String> contentTypeHeader = httpEntity.getHeaders().get(HttpMessageHeaders.HTTP_CONTENT_TYPE); assert contentTypeHeader != null; assertEquals(1, contentTypeHeader.size()); assertEquals("foobar;charset=UTF-8", contentTypeHeader.get(0)); }
@Test public void testHttpContentTypeContainsAlteredCharsetIsPresent(){ endpointConfiguration.setContentType("foobar"); endpointConfiguration.setCharset("whatever"); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); final List<String> contentTypeHeader = httpEntity.getHeaders().get(HttpMessageHeaders.HTTP_CONTENT_TYPE); assert contentTypeHeader != null; assertEquals(1, contentTypeHeader.size()); assertEquals("foobar;charset=whatever", contentTypeHeader.get(0)); }
@Test public void testHttpContentTypeCharsetIsMissingWhenEmptyIsPresent(){ endpointConfiguration.setContentType("foobar"); endpointConfiguration.setCharset(""); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); final List<String> contentTypeHeader = httpEntity.getHeaders().get(HttpMessageHeaders.HTTP_CONTENT_TYPE); assert contentTypeHeader != null; assertEquals(1, contentTypeHeader.size()); assertEquals("foobar", contentTypeHeader.get(0)); }
@Test public void testHttpMethodBodyIsSetForPostOnOutbound(){ message.setHeader(HttpMessageHeaders.HTTP_REQUEST_METHOD, HttpMethod.POST); message.setPayload(payload); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); assertEquals(payload, httpEntity.getBody()); }
@Test public void testHttpMethodBodyIsSetForPutOnOutbound(){ message.setHeader(HttpMessageHeaders.HTTP_REQUEST_METHOD, HttpMethod.PUT); message.setPayload(payload); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); assertEquals(payload, httpEntity.getBody()); }
@Test public void testHttpMethodBodyIsSetForDeleteOnOutbound(){ message.setHeader(HttpMessageHeaders.HTTP_REQUEST_METHOD, HttpMethod.DELETE); message.setPayload(payload); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); assertEquals(payload, httpEntity.getBody()); }
@Test public void testHttpMethodBodyIsSetForPatchOnOutbound(){ message.setHeader(HttpMessageHeaders.HTTP_REQUEST_METHOD, HttpMethod.PATCH); message.setPayload(payload); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); assertEquals(payload, httpEntity.getBody()); }
@Test public void testHttpMethodBodyIsNotSetForGetOnOutbound(){ message.setHeader(HttpMessageHeaders.HTTP_REQUEST_METHOD, HttpMethod.GET); message.setPayload(payload); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); assertNull(httpEntity.getBody()); }
@Test public void testHttpMethodBodyIsNotSetForHeadOnOutbound(){ message.setHeader(HttpMessageHeaders.HTTP_REQUEST_METHOD, HttpMethod.HEAD); message.setPayload(payload); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); assertNull(httpEntity.getBody()); }
@Test public void testHttpMethodBodyIsNotSetForOptionsOnOutbound(){ message.setHeader(HttpMessageHeaders.HTTP_REQUEST_METHOD, HttpMethod.OPTIONS); message.setPayload(payload); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); assertNull(httpEntity.getBody()); }
@Test public void testHttpMethodBodyIsNotSetForTraceOnOutbound(){ message.setHeader(HttpMessageHeaders.HTTP_REQUEST_METHOD, HttpMethod.TRACE); message.setPayload(payload); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); assertNull(httpEntity.getBody()); }
@Test public void testHttpMessageWithStatusCodeContainsNoCookiesOnOutbound(){ message.setHeader(HttpMessageHeaders.HTTP_STATUS_CODE, "200"); Cookie cookie = new Cookie("foo","bar"); message.cookie(cookie); final HttpEntity<?> httpEntity = messageConverter.convertOutbound(message, endpointConfiguration, testContext); assertNull(httpEntity.getHeaders().get("Cookie")); }
@Test public void testSpringIntegrationHeaderMapperIsUsedOnOutbound(){ @SuppressWarnings("unchecked") HeaderMapper<HttpHeaders> headersHeaderMapperMock = (HeaderMapper<HttpHeaders>) mock(HeaderMapper.class); endpointConfiguration.setHeaderMapper(headersHeaderMapperMock); messageConverter.convertOutbound(new HttpMessage(), endpointConfiguration, testContext); verify(headersHeaderMapperMock).fromHeaders(any(MessageHeaders.class), any(HttpHeaders.class)); }