Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,6 +37,7 @@
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @author Denys Ivano
* @since 3.0
* @see RestTemplate#setErrorHandler
*/
Expand All @@ -47,12 +48,14 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
*/
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode;
try {
return hasError(getHttpStatusCode(response));
statusCode = response.getStatusCode();
}
catch (UnknownHttpStatusCodeException ex) {
catch (IllegalArgumentException ex) {
return false;
}
return hasError(statusCode);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,13 +18,15 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;

import org.junit.Test;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StreamUtils;

import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
Expand All @@ -34,9 +36,12 @@
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @author Denys Ivano
*/
public class DefaultResponseErrorHandlerTests {

private final Charset UTF8 = Charset.forName("UTF-8");

private final DefaultResponseErrorHandler handler = new DefaultResponseErrorHandler();

private final ClientHttpResponse response = mock(ClientHttpResponse.class);
Expand Down Expand Up @@ -122,4 +127,55 @@ public void hasErrorForUnknownStatusCode() throws Exception {
assertFalse(handler.hasError(response));
}

@Test // SPR-16604
public void bodyAvailableAfterHasErrorForUnknownStatusCode() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
TestByteArrayInputStream body = new TestByteArrayInputStream("Hello World".getBytes("UTF-8"));

given(response.getStatusCode()).willThrow(new IllegalArgumentException("No matching constant for 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(), UTF8));
}

class TestByteArrayInputStream extends ByteArrayInputStream {

private boolean closed;

public TestByteArrayInputStream(byte[] buf) {
super(buf);
this.closed = false;
}

public boolean isClosed() {
return closed;
}

@Override
public boolean markSupported() {
return false;
}

@Override
public synchronized void mark(int readlimit) {
throw new UnsupportedOperationException("mark/reset not supported");
}

@Override
public synchronized void reset() {
throw new UnsupportedOperationException("mark/reset not supported");
}

@Override
public void close() throws IOException {
super.close();
this.closed = true;
}
}

}