Consistent tolerance of unknown HTTP status codes behind RestTemplate
Issue: SPR-15978
This commit is contained in:
@@ -38,6 +38,7 @@ import org.springframework.web.client.RequestCallback;
|
||||
import org.springframework.web.client.ResponseExtractor;
|
||||
import org.springframework.web.client.RestOperations;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.client.UnknownHttpStatusCodeException;
|
||||
import org.springframework.web.socket.CloseStatus;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
@@ -154,7 +155,7 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport {
|
||||
private final static ResponseExtractor<ResponseEntity<String>> textResponseExtractor =
|
||||
response -> {
|
||||
String body = StreamUtils.copyToString(response.getBody(), SockJsFrame.CHARSET);
|
||||
return new ResponseEntity<>(body, response.getHeaders(), response.getStatusCode());
|
||||
return ResponseEntity.status(response.getRawStatusCode()).headers(response.getHeaders()).body(body);
|
||||
};
|
||||
|
||||
|
||||
@@ -200,14 +201,22 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport {
|
||||
|
||||
@Override
|
||||
public Object extractData(ClientHttpResponse response) throws IOException {
|
||||
if (!HttpStatus.OK.equals(response.getStatusCode())) {
|
||||
throw new HttpServerErrorException(response.getStatusCode());
|
||||
HttpStatus httpStatus = HttpStatus.resolve(response.getRawStatusCode());
|
||||
if (httpStatus == null) {
|
||||
throw new UnknownHttpStatusCodeException(
|
||||
response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), null, null);
|
||||
}
|
||||
if (httpStatus != HttpStatus.OK) {
|
||||
throw new HttpServerErrorException(
|
||||
httpStatus, response.getStatusText(), response.getHeaders(), null, null);
|
||||
}
|
||||
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("XHR receive headers: " + response.getHeaders());
|
||||
}
|
||||
InputStream is = response.getBody();
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
|
||||
while (true) {
|
||||
if (this.sockJsSession.isDisconnected()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -26,7 +26,6 @@ import java.util.Queue;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.task.SyncTaskExecutor;
|
||||
@@ -68,13 +67,7 @@ public class RestTemplateXhrTransportTests {
|
||||
|
||||
private static final Jackson2SockJsMessageCodec CODEC = new Jackson2SockJsMessageCodec();
|
||||
|
||||
private WebSocketHandler webSocketHandler;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.webSocketHandler = mock(WebSocketHandler.class);
|
||||
}
|
||||
private final WebSocketHandler webSocketHandler = mock(WebSocketHandler.class);
|
||||
|
||||
|
||||
@Test
|
||||
@@ -192,7 +185,7 @@ public class RestTemplateXhrTransportTests {
|
||||
private ClientHttpResponse response(HttpStatus status, String body) throws IOException {
|
||||
ClientHttpResponse response = mock(ClientHttpResponse.class);
|
||||
InputStream inputStream = getInputStream(body);
|
||||
given(response.getStatusCode()).willReturn(status);
|
||||
given(response.getRawStatusCode()).willReturn(status.value());
|
||||
given(response.getBody()).willReturn(inputStream);
|
||||
return response;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user