@Override public HttpHeaders extractData(ClientHttpResponse response) { return response.getHeaders(); } }
@Override public HttpHeaders getHeaders() { return this.response.getHeaders(); }
/** * Determine the charset of the response (for inclusion in a status exception). * @param response the response to inspect * @return the associated charset, or {@code null} if none * @since 4.3.8 */ @Nullable protected Charset getCharset(ClientHttpResponse response) { HttpHeaders headers = response.getHeaders(); MediaType contentType = headers.getContentType(); return (contentType != null ? contentType.getCharset() : null); }
@Override public ResponseEntity<T> extractData(ClientHttpResponse response) throws IOException { if (this.delegate != null) { T body = this.delegate.extractData(response); return ResponseEntity.status(response.getRawStatusCode()).headers(response.getHeaders()).body(body); } else { return ResponseEntity.status(response.getRawStatusCode()).headers(response.getHeaders()).build(); } } }
private void mockResponseBody(String expectedBody, MediaType mediaType) throws Exception { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(mediaType); responseHeaders.setContentLength(expectedBody.length()); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(new ByteArrayInputStream(expectedBody.getBytes())); given(converter.read(eq(String.class), any(HttpInputMessage.class))).willReturn(expectedBody); }
@Test public void handleNoMatchOverride() throws Exception { this.errorHandler.setSeriesMapping(Collections .singletonMap(HttpStatus.Series.CLIENT_ERROR, null)); given(this.response.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value()); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.APPLICATION_JSON); given(this.response.getHeaders()).willReturn(responseHeaders); byte[] body = "{\"foo\":\"bar\"}".getBytes(StandardCharsets.UTF_8); responseHeaders.setContentLength(body.length); given(this.response.getBody()).willReturn(new ByteArrayInputStream(body)); this.errorHandler.handleError(this.response); }
@Test(expected = HttpClientErrorException.class) public void handleErrorNullResponse() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); given(response.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value()); given(response.getStatusText()).willReturn("Not Found"); given(response.getHeaders()).willReturn(headers); handler.handleError(response); }
@Test(expected = HttpClientErrorException.class) public void handleErrorIOException() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); given(response.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value()); given(response.getStatusText()).willReturn("Not Found"); given(response.getHeaders()).willReturn(headers); given(response.getBody()).willThrow(new IOException()); handler.handleError(response); }
@Test @SuppressWarnings("unchecked") public void emptyMessageBody() throws IOException { HttpMessageConverter<String> converter = mock(HttpMessageConverter.class); HttpHeaders responseHeaders = new HttpHeaders(); extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter)); given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value()); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(new ByteArrayInputStream("".getBytes())); Object result = extractor.extractData(response); assertNull(result); }
@Test(expected = UnknownHttpStatusCodeException.class) // SPR-9406 public void handleErrorUnknownStatusCode() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); given(response.getRawStatusCode()).willReturn(999); given(response.getStatusText()).willReturn("Custom status code"); given(response.getHeaders()).willReturn(headers); handler.handleError(response); }
@Test(expected = UnknownHttpStatusCodeException.class) public void handleErrorForCustomClientError() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); given(response.getRawStatusCode()).willReturn(499); given(response.getStatusText()).willReturn("Custom status code"); given(response.getHeaders()).willReturn(headers); handler.handleError(response); }
@Test public void optionsForAllow() throws Exception { mockSentRequest(OPTIONS, "http://example.com"); mockResponseStatus(HttpStatus.OK); HttpHeaders responseHeaders = new HttpHeaders(); EnumSet<HttpMethod> expected = EnumSet.of(GET, POST); responseHeaders.setAllow(expected); given(response.getHeaders()).willReturn(responseHeaders); Set<HttpMethod> result = template.optionsForAllow("http://example.com"); assertEquals("Invalid OPTIONS result", expected, result); verify(response).close(); }
@Test(expected = UnknownHttpStatusCodeException.class) public void handleErrorForCustomServerError() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); given(response.getRawStatusCode()).willReturn(599); given(response.getStatusText()).willReturn("Custom status code"); given(response.getHeaders()).willReturn(headers); handler.handleError(response); }
@Test // SPR-17461 public void hasErrorForCustomServerError() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); given(response.getRawStatusCode()).willReturn(599); given(response.getStatusText()).willReturn("Custom status code"); given(response.getHeaders()).willReturn(headers); assertTrue(handler.hasError(response)); }
@Test // SPR-16108 public void hasErrorForUnknownStatusCode() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); given(response.getRawStatusCode()).willReturn(999); given(response.getStatusText()).willReturn("Custom status code"); given(response.getHeaders()).willReturn(headers); assertFalse(handler.hasError(response)); }
@Test // SPR-17461 public void hasErrorForCustomClientError() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); given(response.getRawStatusCode()).willReturn(499); given(response.getStatusText()).willReturn("Custom status code"); given(response.getHeaders()).willReturn(headers); assertTrue(handler.hasError(response)); }
@Test public void headForHeaders() throws Exception { mockSentRequest(HEAD, "http://example.com"); mockResponseStatus(HttpStatus.OK); HttpHeaders responseHeaders = new HttpHeaders(); given(response.getHeaders()).willReturn(responseHeaders); HttpHeaders result = template.headForHeaders("http://example.com"); assertSame("Invalid headers returned", responseHeaders, result); verify(response).close(); }
@Test public void zeroContentLength() throws IOException { HttpMessageConverter<?> converter = mock(HttpMessageConverter.class); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentLength(0); extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter)); given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value()); given(response.getHeaders()).willReturn(responseHeaders); Object result = extractor.extractData(response); assertNull(result); }
@Test // gh-22265 @SuppressWarnings("unchecked") public void nullMessageBody() throws IOException { HttpMessageConverter<String> converter = mock(HttpMessageConverter.class); HttpHeaders responseHeaders = new HttpHeaders(); extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter)); given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value()); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(null); Object result = extractor.extractData(response); assertNull(result); }
@Test // SPR-16604 public void bodyAvailableAfterHasErrorForUnknownStatusCode() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); TestByteArrayInputStream body = new TestByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8)); given(response.getRawStatusCode()).willReturn(999); given(response.getStatusText()).willReturn("Custom status code"); given(response.getHeaders()).willReturn(headers); given(response.getBody()).willReturn(body); assertFalse(handler.hasError(response)); assertFalse(body.isClosed()); assertEquals("Hello World", StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8)); }