/** * Construct a new instance of the {@link RestGatewaySupport}, with default parameters. */ public RestGatewaySupport() { this.restTemplate = new RestTemplate(); }
/** * Creates a new instance of the {@code AsyncRestTemplate} using the given * asynchronous and synchronous request factories. * @param asyncRequestFactory the asynchronous request factory * @param syncRequestFactory the synchronous request factory */ public AsyncRestTemplate(org.springframework.http.client.AsyncClientHttpRequestFactory asyncRequestFactory, ClientHttpRequestFactory syncRequestFactory) { this(asyncRequestFactory, new RestTemplate(syncRequestFactory)); }
/** * Construct a new instance of the {@link RestGatewaySupport}, with the given {@link ClientHttpRequestFactory}. * @see RestTemplate#RestTemplate(ClientHttpRequestFactory) */ public RestGatewaySupport(ClientHttpRequestFactory requestFactory) { Assert.notNull(requestFactory, "'requestFactory' must not be null"); this.restTemplate = new RestTemplate(requestFactory); }
@Override protected RestTemplate initRestTemplate() { // JDK default HTTP client blacklist headers like Origin return new RestTemplate(new HttpComponentsClientHttpRequestFactory()); }
@Before public void setup() { List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new StringHttpMessageConverter()); converters.add(new MappingJackson2HttpMessageConverter()); this.restTemplate = new RestTemplate(); this.restTemplate.setMessageConverters(converters); this.mockServer = MockRestServiceServer.createServer(this.restTemplate); }
@Before public void setup() { List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new StringHttpMessageConverter()); converters.add(new MappingJackson2HttpMessageConverter()); this.restTemplate = new RestTemplate(); this.restTemplate.setMessageConverters(converters); this.mockServer = MockRestServiceServer.createServer(this.restTemplate); }
@Override protected RestTemplate initRestTemplate() { // JDK default HTTP client blacklists headers like Origin return new RestTemplate(new HttpComponentsClientHttpRequestFactory()); }
@Test public void checkUri() throws Exception { URI url = new URI("http://localhost:" + port + "/foo?param=bar"); RequestEntity<Void> request = RequestEntity.post(url).build(); ResponseEntity<Void> response = new RestTemplate().exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); }
@Test public void writeOnly() throws Exception { RestTemplate restTemplate = new RestTemplate(); this.body = randomBytes(); RequestEntity<byte[]> request = RequestEntity.post( new URI("http://localhost:" + port)).body( "".getBytes(StandardCharsets.UTF_8)); ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class); assertArrayEquals(body, response.getBody()); }
@Test @Ignore // TODO: fragile due to socket failures public void basicTest() throws Exception { URI url = new URI("http://localhost:" + port); ResponseEntity<String> response = new RestTemplate().exchange( RequestEntity.get(url).build(), String.class); assertThat(response.getBody(), Matchers.equalTo("hello")); }
@Test public void responseBodyError() throws Exception { RestTemplate restTemplate = new RestTemplate(); restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER); URI url = new URI("http://localhost:" + port + "/response-body-error"); ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); }
@Test // SPR-15560 public void emptyPathSegments() throws Exception { RestTemplate restTemplate = new RestTemplate(); restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER); URI url = new URI("http://localhost:" + port + "//"); ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); assertEquals(HttpStatus.OK, response.getStatusCode()); }
@Test public void handlingError() throws Exception { RestTemplate restTemplate = new RestTemplate(); restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER); URI url = new URI("http://localhost:" + port + "/handling-error"); ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); }
@Test public void echo() throws Exception { RestTemplate restTemplate = new RestTemplate(); byte[] body = randomBytes(); RequestEntity<byte[]> request = RequestEntity.post(new URI("http://localhost:" + port)).body(body); ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class); assertArrayEquals(body, response.getBody()); }
@Test public void test() throws Exception { RestTemplate template = new RestTemplate(new MockMvcClientHttpRequestFactory(this.mockMvc)); String result = template.getForObject("/foo", String.class); assertEquals("bar", result); }
@Test public void getFormParts() throws Exception { RestTemplate restTemplate = new RestTemplate(); RequestEntity<MultiValueMap<String, Object>> request = RequestEntity .post(new URI("http://localhost:" + port + "/form-parts")) .contentType(MediaType.MULTIPART_FORM_DATA) .body(generateBody()); ResponseEntity<Void> response = restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); }
@Test public void testHandlerNotFound() throws Exception { URI url = new URI("http://localhost:" + this.port + "/oops"); RequestEntity<Void> request = RequestEntity.get(url).build(); try { new RestTemplate().exchange(request, byte[].class); } catch (HttpClientErrorException ex) { assertEquals(HttpStatus.NOT_FOUND, ex.getStatusCode()); } }
@Test public void testRequestToHeaderSettingHandler() throws Exception { URI url = new URI("http://localhost:" + this.port + "/header"); RequestEntity<Void> request = RequestEntity.get(url).build(); ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals("bar", response.getHeaders().getFirst("foo")); }
@Test public void random() throws Throwable { // TODO: fix Reactor support RestTemplate restTemplate = new RestTemplate(); byte[] body = randomBytes(); RequestEntity<byte[]> request = RequestEntity.post(new URI("http://localhost:" + port)).body(body); ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class); assertNotNull(response.getBody()); assertEquals(RESPONSE_SIZE, response.getHeaders().getContentLength()); assertEquals(RESPONSE_SIZE, response.getBody().length); }
@Before public void setup() { this.restTemplate = new RestTemplate(); this.mockServer = MockRestServiceServer.bindTo(this.restTemplate).ignoreExpectOrder(true).build(); }