@Test public void exchangeGet() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyHeader", "MyValue"); HttpEntity<String> requestEntity = new HttpEntity<>(requestHeaders); ResponseEntity<String> response = template.exchange(baseUrl + "/{method}", HttpMethod.GET, requestEntity, String.class, "get"); assertEquals("Invalid content", helloWorld, response.getBody()); }
<T> ResponseEntity<T> performGet(String url, MediaType out, ParameterizedTypeReference<T> type) throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(out)); return this.restTemplate.exchange(prepareGet(url, headers), type); }
<T> ResponseEntity<T> performPost(String url, MediaType in, Object body, MediaType out, ParameterizedTypeReference<T> type) throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(in); if (out != null) { headers.setAccept(Collections.singletonList(out)); } return getRestTemplate().exchange(preparePost(url, headers, body), type); }
<T> ResponseEntity<T> performPost(String url, MediaType in, Object body, MediaType out, Class<T> type) throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(in); if (out != null) { headers.setAccept(Collections.singletonList(out)); } return getRestTemplate().exchange(preparePost(url, headers, body), type); }
<T> ResponseEntity<T> performGet(String url, MediaType out, Class<T> type) throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(out)); return getRestTemplate().exchange(prepareGet(url, headers), type); }
@Test public void testRequestToFooHandler() throws Exception { URI url = new URI("http://localhost:" + this.port + "/foo"); RequestEntity<Void> request = RequestEntity.get(url).build(); ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertArrayEquals("foo".getBytes("UTF-8"), response.getBody()); }
@Test public void testRequestToBarHandler() throws Exception { URI url = new URI("http://localhost:" + this.port + "/bar"); RequestEntity<Void> request = RequestEntity.get(url).build(); ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertArrayEquals("bar".getBytes("UTF-8"), response.getBody()); }
@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 exchangePost() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyHeader", "MyValue"); requestHeaders.setContentType(MediaType.TEXT_PLAIN); HttpEntity<String> entity = new HttpEntity<>(helloWorld, requestHeaders); HttpEntity<Void> result = template.exchange(baseUrl + "/{method}", POST, entity, Void.class, "post"); assertEquals("Invalid location", new URI(baseUrl + "/post/1"), result.getHeaders().getLocation()); assertFalse(result.hasBody()); }
@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 flux() { ParameterizedTypeReference<List<Person>> reference = new ParameterizedTypeReference<List<Person>>() {}; ResponseEntity<List<Person>> result = restTemplate.exchange("http://localhost:" + port + "/flux", HttpMethod.GET, null, reference); assertEquals(HttpStatus.OK, result.getStatusCode()); List<Person> body = result.getBody(); assertEquals(2, body.size()); assertEquals("John", body.get(0).getName()); assertEquals("Jane", body.get(1).getName()); }
@Test public void flux() { ParameterizedTypeReference<List<Person>> reference = new ParameterizedTypeReference<List<Person>>() {}; ResponseEntity<List<Person>> result = this.restTemplate .exchange("http://localhost:" + this.port + "/flux", HttpMethod.GET, null, reference); assertEquals(HttpStatus.OK, result.getStatusCode()); List<Person> body = result.getBody(); assertEquals(2, body.size()); assertEquals("John", body.get(0).getName()); assertEquals("Jane", body.get(1).getName()); }
@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 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 postMono() { URI uri = URI.create("http://localhost:" + port + "/mono"); Person person = new Person("Jack"); RequestEntity<Person> requestEntity = RequestEntity.post(uri).body(person); ResponseEntity<Person> result = restTemplate.exchange(requestEntity, Person.class); assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals("Jack", result.getBody().getName()); }
@Test public void etagCheckWithNotModifiedResponse() throws Exception { URI uri = new URI("http://localhost:" + this.port + "/html"); RequestEntity<Void> request = RequestEntity.get(uri).ifNoneMatch("\"deadb33f8badf00d\"").build(); ResponseEntity<String> response = getRestTemplate().exchange(request, String.class); assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode()); assertNull(response.getBody()); }
@Test public void forwardedHeaders() { // One integration test to verify triggering of Forwarded header support. // More fine-grained tests in ForwardedHeaderTransformerTests. RequestEntity<Void> request = RequestEntity .get(URI.create("http://localhost:" + this.port + "/uri")) .header("Forwarded", "host=84.198.58.199;proto=https") .build(); ResponseEntity<String> entity = getRestTemplate().exchange(request, String.class); assertEquals("https://84.198.58.199/uri", entity.getBody()); }
@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); }