HttpMessageConverter implements MessageConverter<HttpEntity<?>, HttpEntity<?>, HttpEndpointConfiguration> { @Override public HttpMessage convertInbound(HttpEntity<?> message, HttpEndpointConfiguration endpointConfiguration, TestContext context) { Map<String, Object> mappedHeaders = endpointConfiguration.getHeaderMapper().toHeaders(message.getHeaders()); HttpMessage httpMessage = new HttpMessage(extractMessageBody(message), convertHeaderTypes(mappedHeaders)); for (Map.Entry<String, String> customHeader : getCustomHeaders(message.getHeaders(), mappedHeaders).entrySet()) { httpMessage.setHeader(customHeader.getKey(), customHeader.getValue()); } if (message instanceof ResponseEntity<?>) { httpMessage.status(((ResponseEntity<?>) message).getStatusCode()); httpMessage.version("HTTP/1.1"); if (endpointConfiguration.isHandleCookies()) { httpMessage.setCookies(cookieConverter.convertCookies(message)); } } return httpMessage; } 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 testSpringIntegrationHeaderMapperResultIsSetOnInbound(){ final Map<String, Object> expectedHeaderMap = new HashMap<>(); expectedHeaderMap.put("foo", "bar"); @SuppressWarnings("unchecked") HeaderMapper<HttpHeaders> headersHeaderMapperMock = (HeaderMapper<HttpHeaders>) mock(HeaderMapper.class); when(headersHeaderMapperMock.toHeaders(any(HttpHeaders.class))).thenReturn(expectedHeaderMap); endpointConfiguration.setHeaderMapper(headersHeaderMapperMock); final HttpMessage httpMessage = messageConverter.convertInbound(HttpEntity.EMPTY, endpointConfiguration, testContext); assertEquals("bar", httpMessage.getHeaders().get("foo")); }
@Test public void testCitrusDefaultHeaderAreSetOnInbound(){ final HttpMessage httpMessage = messageConverter.convertInbound(HttpEntity.EMPTY, endpointConfiguration, testContext); assertTrue(httpMessage.getHeaders().containsKey("citrus_message_id")); assertTrue(httpMessage.getHeaders().containsKey("citrus_message_timestamp")); }
@Test public void testHttpEntityMessageBodyIsPreservedOnInbound(){ final HttpEntity<String> httpEntity = new HttpEntity<>(payload); final HttpMessage httpMessage = messageConverter.convertInbound(httpEntity, endpointConfiguration, testContext); assertEquals(httpMessage.getPayload(String.class), payload); }
@Test public void testHttpEntityDefaultMessageBodyIsSetOnInbound(){ final HttpEntity<String> httpEntity = new HttpEntity<>(null); final HttpMessage httpMessage = messageConverter.convertInbound(httpEntity, endpointConfiguration, testContext); assertEquals(httpMessage.getPayload(String.class), ""); }
@Test public void testCustomHeadersAreSetOnInbound(){ final HttpHeaders expectedHttpHeader = new HttpHeaders(); expectedHttpHeader.put("foo", Collections.singletonList("bar")); final HttpEntity<?> httpEntity = new HttpEntity<>(null, expectedHttpHeader); final HttpMessage httpMessage = messageConverter.convertInbound(httpEntity, endpointConfiguration, testContext); assertEquals("bar", httpMessage.getHeaders().get("foo")); }
@Test public void testCustomHeadersListsAreConvertedToStringOnInbound(){ final HttpHeaders expectedHttpHeader = new HttpHeaders(); expectedHttpHeader.put("foo", Arrays.asList("bar", "foobar", "foo")); final HttpEntity<?> httpEntity = new HttpEntity<>(null, expectedHttpHeader); final HttpMessage httpMessage = messageConverter.convertInbound(httpEntity, endpointConfiguration, testContext); assertEquals("bar,foobar,foo", httpMessage.getHeaders().get("foo")); }
@Test public void testStatusCodeIsSetOnInbound(){ final ResponseEntity<?> responseEntity = new ResponseEntity<>(HttpStatus.FORBIDDEN); final HttpMessage httpMessage = messageConverter.convertInbound(responseEntity, endpointConfiguration, testContext); assertEquals(HttpStatus.FORBIDDEN, httpMessage.getStatusCode()); }
@Test public void testHttpVersionIsSetOnInbound(){ final ResponseEntity<?> responseEntity = new ResponseEntity<>(HttpStatus.OK); final HttpMessage httpMessage = messageConverter.convertInbound(responseEntity, endpointConfiguration, testContext); assertEquals("HTTP/1.1", httpMessage.getVersion()); }
@Test public void testNoCookiesPreservedByDefaultOnInbound(){ messageConverter.convertInbound(ResponseEntity.EMPTY, endpointConfiguration, testContext); verify(cookieConverterMock, never()).convertCookies(any()); }
@Test public void testCookiesPreservedOnConfigurationOnInbound(){ Cookie cookieMock = mock(Cookie.class); final ResponseEntity<?> responseEntity = ResponseEntity.ok("foobar"); when(cookieConverterMock.convertCookies(responseEntity)).thenReturn(new Cookie[]{cookieMock}); endpointConfiguration.setHandleCookies(true); final HttpMessage httpMessage = messageConverter.convertInbound(responseEntity, endpointConfiguration, testContext); assertEquals(1, httpMessage.getCookies().size()); assertEquals(cookieMock, httpMessage.getCookies().get(0)); }
@Test public void testSpringIntegrationHeaderMapperListResultIsConvertedOnInbound(){ final Map<String, Object> mockedHeaderMap = new HashMap<>(); mockedHeaderMap.put("foo", Arrays.asList("bar", "foobar")); @SuppressWarnings("unchecked") HeaderMapper<HttpHeaders> headersHeaderMapperMock = (HeaderMapper<HttpHeaders>) mock(HeaderMapper.class); when(headersHeaderMapperMock.toHeaders(any(HttpHeaders.class))).thenReturn(mockedHeaderMap); endpointConfiguration.setHeaderMapper(headersHeaderMapperMock); final HttpMessage httpMessage = messageConverter.convertInbound(HttpEntity.EMPTY, endpointConfiguration, testContext); assertEquals("bar,foobar", httpMessage.getHeaders().get("foo")); }
@Test public void testSpringIntegrationHeaderMapperMediaTypeResultIsConvertedOnInbound(){ final Map<String, Object> mockedHeaderMap = new HashMap<>(); mockedHeaderMap.put("foo", MediaType.APPLICATION_JSON); @SuppressWarnings("unchecked") HeaderMapper<HttpHeaders> headersHeaderMapperMock = (HeaderMapper<HttpHeaders>) mock(HeaderMapper.class); when(headersHeaderMapperMock.toHeaders(any(HttpHeaders.class))).thenReturn(mockedHeaderMap); endpointConfiguration.setHeaderMapper(headersHeaderMapperMock); final HttpMessage httpMessage = messageConverter.convertInbound(HttpEntity.EMPTY, endpointConfiguration, testContext); assertEquals("application/json", httpMessage.getHeaders().get("foo")); }