@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void exchangeGet() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyHeader", "MyValue"); HttpEntity<?> requestEntity = new HttpEntity(requestHeaders); Future<ResponseEntity<String>> responseFuture = template.exchange(baseUrl + "/{method}", HttpMethod.GET, requestEntity, String.class, "get"); ResponseEntity<String> response = responseFuture.get(); assertEquals("Invalid content", helloWorld, response.getBody()); }
@Test public void handleForm() throws Exception { MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(); formData.add("name", "George"); formData.add("age", "5"); assertEquals("Processed form: Foo[id=1, name='George', age=5]", performPost("/foos/1", MediaType.APPLICATION_FORM_URLENCODED, formData, MediaType.TEXT_PLAIN, String.class).getBody()); }
@Test public void getNoContentTypeHeader() throws Exception { Future<ResponseEntity<byte[]>> futureEntity = template.getForEntity(baseUrl + "/get/nocontenttype", byte[].class); ResponseEntity<byte[]> responseEntity = futureEntity.get(); assertArrayEquals("Invalid content", helloWorld.getBytes("UTF-8"), responseEntity.getBody()); }
@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 statusCodeAsInt() { Integer entity = 42; ResponseEntity<Integer> responseEntity = ResponseEntity.status(200).body(entity); assertEquals(200, responseEntity.getStatusCode().value()); assertEquals(entity, responseEntity.getBody()); }
@Test public void handleDateParam() throws Exception { assertEquals("Processed date!", performPost("/date-param?date=2016-10-31&date-pattern=YYYY-mm-dd", new HttpHeaders(), null, String.class).getBody()); }
@Test public void handleWithParam() throws Exception { String expected = "Hello George!"; assertEquals(expected, performGet("/param?name=George", new HttpHeaders(), String.class).getBody()); }
@Test public void matrixVariable() throws Exception { String expected = "p=11, q2=22, q4=44"; String url = "/first;p=11/second;q=22/third-fourth;q=44"; assertEquals(expected, performGet(url, new HttpHeaders(), String.class).getBody()); }
@Test public void okEntity() { Integer entity = 42; ResponseEntity<Integer> responseEntity = ResponseEntity.ok(entity); assertNotNull(responseEntity); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(entity, responseEntity.getBody()); }
@Test public void jsonViewWithFluxResponse() throws Exception { String expected = "[{\"withView1\":\"with\"},{\"withView1\":\"with\"}]"; assertEquals(expected, performGet("/response/flux", MediaType.APPLICATION_JSON_UTF8, String.class).getBody()); }
@Test public void personResponseBodyWithObservable() throws Exception { List<?> expected = asList(new Person("Robert"), new Person("Marie")); assertEquals(expected, performGet("/person-response/observable", JSON, PERSON_LIST).getBody()); }
@Test public void jsonViewWithRequest() throws Exception { String expected = "{\"withView1\":\"with\",\"withView2\":null,\"withoutView\":null}"; assertEquals(expected, performPost("/request/raw", MediaType.APPLICATION_JSON, new JacksonViewBean("with", "with", "without"), MediaType.APPLICATION_JSON_UTF8, String.class).getBody()); }
@Test public void byteBufferResponseBodyWithMono() throws Exception { String expected = "Hello!"; ResponseEntity<String> responseEntity = performGet("/raw-response/mono", new HttpHeaders(), String.class); assertEquals(6, responseEntity.getHeaders().getContentLength()); assertEquals(expected, responseEntity.getBody()); }
@Test public void acceptedNoBody() throws URISyntaxException { ResponseEntity<Void> responseEntity = ResponseEntity.accepted().build(); assertNotNull(responseEntity); assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode()); assertNull(responseEntity.getBody()); }
@Test public void noContent() throws URISyntaxException { ResponseEntity<Void> responseEntity = ResponseEntity.noContent().build(); assertNotNull(responseEntity); assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode()); assertNull(responseEntity.getBody()); }
@Test public void personTransformWithCompletableFuture() throws Exception { assertEquals(new Person("ROBERT"), performPost("/person-transform/completable-future", JSON, new Person("Robert"), JSON, Person.class).getBody()); }
@Test // SPR-16759 public void personTransformWithMonoAndXml() throws Exception { assertEquals(new Person("ROBERT"), performPost("/person-transform/mono", MediaType.APPLICATION_XML, new Person("Robert"), MediaType.APPLICATION_XML, Person.class).getBody()); }
@Test public void getAndInterceptResponse() throws Exception { RequestInterceptor interceptor = new RequestInterceptor(); template.setInterceptors(Collections.singletonList(interceptor)); ListenableFuture<ResponseEntity<String>> future = template.getForEntity(baseUrl + "/get", String.class); interceptor.latch.await(5, TimeUnit.SECONDS); assertNotNull(interceptor.response); assertEquals(HttpStatus.OK, interceptor.response.getStatusCode()); assertNull(interceptor.exception); assertEquals(helloWorld, future.get().getBody()); }
@Test public void personResponseBody() throws Exception { Person expected = new Person("Robert"); ResponseEntity<Person> responseEntity = performGet("/person-response/person", JSON, Person.class); assertEquals(17, responseEntity.getHeaders().getContentLength()); assertEquals(expected, responseEntity.getBody()); }