@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 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); } }