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