HttpRequester { Object extractResponse(ClientHttpResponse response) throws IOException { MediaType mediaType = response.getHeaders().getContentType(); if (mediaType.equals(MediaType.APPLICATION_JSON)) { return org.apache.commons.io.IOUtils.toString(response.getBody(), "UTF-8"); } else { Path tempFile = Files.createTempFile("fileDownload", ""); if (tempFile.toFile().exists()) { FileUtils.deleteQuietly(tempFile.toFile()); } Files.copy(response.getBody(), tempFile); return tempFile; } } HttpRequester();  HttpRequester(Properties securityProperties);   HttpRequester(Properties securityProperties, RestTemplate restTemplate); RestTemplate getRestTemplate(); T get(URI url, Class<T> responseType); T post(URI url, Object content, Class<T> responseType); Object executeWithResponseExtractor(URI url); static URI createURI(String baseUrl, String path, String... queryParams);  }
@Test public void extractResponseOfJsonString() throws Exception { String responseString = "my response"; requester = new HttpRequester(); response = new MockClientHttpResponse(IOUtils.toInputStream(responseString, "UTF-8"), HttpStatus.OK); response.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); Object result = requester.extractResponse(response); assertThat(result).isEqualTo(responseString); }
@Test public void extractResponseOfFileDownload() throws Exception { File responseFile = temporaryFolder.newFile(); FileUtils.writeStringToFile(responseFile, "some file contents", "UTF-8"); requester = new HttpRequester(); response = new MockClientHttpResponse(new FileInputStream(responseFile), HttpStatus.OK); response.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE); Object result = requester.extractResponse(response); Path fileResult = (Path) result; assertThat(fileResult).hasSameTextualContentAs(responseFile.toPath()); }