@Test public void testOverwriteContentTypeHeader() { HttpMessageSender messageSender = new HttpMessageSender(); String requestUrl = "http://localhost:8088/test"; final String responseBody = "<TestResponse><Message>Hello World!</Message></TestResponse>"; messageSender.setReplyMessageHandler(replyMessageHandler); messageSender.setRequestMethod(HttpMethod.POST); messageSender.setRequestUrl(requestUrl); messageSender.setContentType("text/xml"); messageSender.setCharset("ISO-8859-1"); Message<?> requestMessage = MessageBuilder.withPayload("<TestRequest><Message>Hello World!</Message></TestRequest>") .setHeader("Content-Type", "application/xml;charset=UTF-8") .setHeader("Accept", "application/xml") .build(); messageSender.setRestTemplate(restTemplate); reset(restTemplate, replyMessageHandler); restTemplate.setErrorHandler(anyObject(ResponseErrorHandler.class)); expectLastCall().once(); expect( restTemplate.exchange( eq(requestUrl), eq(HttpMethod.POST), anyObject(HttpEntity.class), eq(String.class))) .andAnswer( new IAnswer<ResponseEntity<String>>() { public ResponseEntity<String> answer() throws Throwable { HttpEntity<?> httpRequest = (HttpEntity<?>) getCurrentArguments()[2]; Assert.assertEquals( httpRequest.getBody().toString(), "<TestRequest><Message>Hello World!</Message></TestRequest>"); Assert.assertEquals(httpRequest.getHeaders().size(), 2); Assert.assertEquals( httpRequest.getHeaders().getContentType().toString(), "application/xml;charset=UTF-8"); Assert.assertEquals( httpRequest.getHeaders().getAccept().get(0).toString(), "application/xml"); return new ResponseEntity<String>(responseBody, HttpStatus.OK); } }) .once(); replyMessageHandler.onReplyMessage(anyObject(Message.class)); expectLastCall().once(); replay(restTemplate, replyMessageHandler); messageSender.send(requestMessage); verify(restTemplate, replyMessageHandler); }
@Test public void testErrorResponsePropagateStrategy() { HttpMessageSender messageSender = new HttpMessageSender(); String requestUrl = "http://localhost:8088/test"; final String responseBody = "<TestResponse><Message>Hello World!</Message></TestResponse>"; messageSender.setReplyMessageHandler(replyMessageHandler); messageSender.setRequestMethod(HttpMethod.POST); messageSender.setRequestUrl(requestUrl); messageSender.setErrorHandlingStrategy(ErrorHandlingStrategy.PROPAGATE); Message<?> requestMessage = MessageBuilder.withPayload("<TestRequest><Message>Hello World!</Message></TestRequest>") .build(); messageSender.setRestTemplate(restTemplate); reset(restTemplate, replyMessageHandler); restTemplate.setErrorHandler(anyObject(ResponseErrorHandler.class)); expectLastCall().once(); expect( restTemplate.exchange( eq(requestUrl), eq(HttpMethod.POST), anyObject(HttpEntity.class), eq(String.class))) .andReturn(new ResponseEntity<String>(responseBody, HttpStatus.FORBIDDEN)) .once(); replyMessageHandler.onReplyMessage(anyObject(Message.class)); expectLastCall() .andAnswer( new IAnswer<Object>() { public Object answer() throws Throwable { Message<?> responseMessage = (Message<?>) getCurrentArguments()[0]; Assert.assertEquals( responseMessage.getHeaders().get(CitrusHttpMessageHeaders.HTTP_STATUS_CODE), HttpStatus.FORBIDDEN); Assert.assertEquals( responseMessage.getHeaders().get(CitrusHttpMessageHeaders.HTTP_REASON_PHRASE), "FORBIDDEN"); return null; } }) .once(); replay(restTemplate, replyMessageHandler); messageSender.send(requestMessage); verify(restTemplate, replyMessageHandler); }
@Test public void testOverwriteRequestMethod() { HttpMessageSender messageSender = new HttpMessageSender(); String requestUrl = "http://localhost:8088/test"; final String responseBody = "<TestResponse><Message>Hello World!</Message></TestResponse>"; messageSender.setReplyMessageHandler(replyMessageHandler); messageSender.setRequestMethod(HttpMethod.GET); messageSender.setRequestUrl(requestUrl); Message<?> requestMessage = MessageBuilder.withPayload("<TestRequest><Message>Hello World!</Message></TestRequest>") .setHeader(CitrusHttpMessageHeaders.HTTP_REQUEST_METHOD, "GET") .build(); messageSender.setRestTemplate(restTemplate); reset(restTemplate, replyMessageHandler); restTemplate.setErrorHandler(anyObject(ResponseErrorHandler.class)); expectLastCall().once(); expect( restTemplate.exchange( eq(requestUrl), eq(HttpMethod.GET), anyObject(HttpEntity.class), eq(String.class))) .andAnswer( new IAnswer<ResponseEntity<String>>() { public ResponseEntity<String> answer() throws Throwable { HttpEntity<?> httpRequest = (HttpEntity<?>) getCurrentArguments()[2]; Assert.assertNull(httpRequest.getBody()); // null because of GET Assert.assertEquals(httpRequest.getHeaders().size(), 1); Assert.assertEquals( httpRequest.getHeaders().getContentType().toString(), "text/plain;charset=UTF-8"); return new ResponseEntity<String>(responseBody, HttpStatus.OK); } }) .once(); replyMessageHandler.onReplyMessage(anyObject(Message.class)); expectLastCall().once(); replay(restTemplate, replyMessageHandler); messageSender.send(requestMessage); verify(restTemplate, replyMessageHandler); }
@Test public void testEndpointUriResolver() { HttpMessageSender messageSender = new HttpMessageSender(); String requestUrl = "http://localhost:8088/test"; String responseBody = "<TestResponse><Message>Hello World!</Message></TestResponse>"; messageSender.setReplyMessageHandler(replyMessageHandler); messageSender.setRequestMethod(HttpMethod.GET); messageSender.setRequestUrl(requestUrl); Message<?> requestMessage = MessageBuilder.withPayload("<TestRequest><Message>Hello World!</Message></TestRequest>") .build(); EndpointUriResolver endpointUriResolver = EasyMock.createMock(EndpointUriResolver.class); messageSender.setEndpointUriResolver(endpointUriResolver); messageSender.setRestTemplate(restTemplate); reset(restTemplate, replyMessageHandler, endpointUriResolver); restTemplate.setErrorHandler(anyObject(ResponseErrorHandler.class)); expectLastCall().once(); expect(endpointUriResolver.resolveEndpointUri(requestMessage, "http://localhost:8088/test")) .andReturn("http://localhost:8081/new") .once(); expect( restTemplate.exchange( eq("http://localhost:8081/new"), eq(HttpMethod.GET), anyObject(HttpEntity.class), eq(String.class))) .andReturn(new ResponseEntity<String>(responseBody, HttpStatus.OK)) .once(); replyMessageHandler.onReplyMessage(anyObject(Message.class)); expectLastCall().once(); replay(restTemplate, replyMessageHandler, endpointUriResolver); messageSender.send(requestMessage); verify(restTemplate, replyMessageHandler, endpointUriResolver); }
@Test public void testErrorResponseExceptionStrategy() { HttpMessageSender messageSender = new HttpMessageSender(); String requestUrl = "http://localhost:8088/test"; messageSender.setReplyMessageHandler(replyMessageHandler); messageSender.setRequestMethod(HttpMethod.POST); messageSender.setRequestUrl(requestUrl); messageSender.setErrorHandlingStrategy(ErrorHandlingStrategy.THROWS_EXCEPTION); Message<?> requestMessage = MessageBuilder.withPayload("<TestRequest><Message>Hello World!</Message></TestRequest>") .build(); messageSender.setRestTemplate(restTemplate); reset(restTemplate, replyMessageHandler); restTemplate.setErrorHandler(anyObject(ResponseErrorHandler.class)); expectLastCall().once(); expect( restTemplate.exchange( eq(requestUrl), eq(HttpMethod.POST), anyObject(HttpEntity.class), eq(String.class))) .andThrow(new HttpClientErrorException(HttpStatus.FORBIDDEN)) .once(); replay(restTemplate, replyMessageHandler); try { messageSender.send(requestMessage); Assert.fail("Missing exception due to http error status code"); } catch (HttpClientErrorException e) { Assert.assertEquals(e.getMessage(), "403 FORBIDDEN"); verify(restTemplate, replyMessageHandler); } }
/** * @see com.consol.citrus.message.MessageSender#send(org.springframework.integration.Message) * @throws CitrusRuntimeException */ public void send(Message<?> message) { String endpointUri; if (endpointUriResolver != null) { endpointUri = endpointUriResolver.resolveEndpointUri(message, getRequestUrl()); } else { endpointUri = getRequestUrl(); } log.info("Sending HTTP message to: '" + endpointUri + "'"); if (log.isDebugEnabled()) { log.debug("Message to be sent:\n" + message.getPayload().toString()); } HttpMethod method = requestMethod; if (message.getHeaders().containsKey(CitrusHttpMessageHeaders.HTTP_REQUEST_METHOD)) { method = HttpMethod.valueOf( (String) message.getHeaders().get(CitrusHttpMessageHeaders.HTTP_REQUEST_METHOD)); } HttpEntity<?> requestEntity = generateRequest(message, method); restTemplate.setErrorHandler(new InternalResponseErrorHandler(message)); ResponseEntity<?> response = restTemplate.exchange(endpointUri, method, requestEntity, String.class); log.info("HTTP message was successfully sent to endpoint: '" + endpointUri + "'"); informReplyMessageHandler( buildResponseMessage( response.getHeaders(), response.getBody() != null ? response.getBody() : "", response.getStatusCode()), message); }
/** * Constructor using custom client request factory. * * @param requestFactory the custom request factory. */ public HttpMessageSender(ClientHttpRequestFactory requestFactory) { restTemplate = new RestTemplate(); restTemplate.setRequestFactory(requestFactory); }
/** Default constructor. */ public HttpMessageSender() { restTemplate = new RestTemplate(); restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); }
/** * Sets the interceptors on this implementation's rest template. * * @param interceptors the interceptors to set */ public void setInterceptors(List<ClientHttpRequestInterceptor> interceptors) { restTemplate.setInterceptors(interceptors); }