Skip to content

Commit

Permalink
Merge pull request #23855
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Nov 8, 2019
2 parents d394c7a + 1261e64 commit c7b9988
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
Expand Up @@ -91,6 +91,14 @@ protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] buffere
* @param headers the headers to add
*/
static void addHeaders(HttpURLConnection connection, HttpHeaders headers) {
String method = connection.getRequestMethod();
if (method.equals("PUT") || method.equals("DELETE")) {
if (!StringUtils.hasText(headers.getFirst(HttpHeaders.ACCEPT))) {
// Avoid "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
// from HttpUrlConnection which prevents JSON error response details.
headers.set(HttpHeaders.ACCEPT, "*/*");
}
}
headers.forEach((headerName, headerValues) -> {
if (HttpHeaders.COOKIE.equalsIgnoreCase(headerName)) { // RFC 6265
String headerValue = StringUtils.collectionToDelimitedString(headerValues, "; ");
Expand Down
Expand Up @@ -22,6 +22,7 @@

import org.springframework.http.HttpHeaders;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -31,9 +32,11 @@
*/
public class SimpleClientHttpRequestFactoryTests {

@Test // SPR-13225

@Test // SPR-13225
public void headerWithNullValue() {
HttpURLConnection urlConnection = mock(HttpURLConnection.class);
given(urlConnection.getRequestMethod()).willReturn("GET");
HttpHeaders headers = new HttpHeaders();
headers.set("foo", null);
SimpleBufferingClientHttpRequest.addHeaders(urlConnection, headers);
Expand Down
Expand Up @@ -25,8 +25,13 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -43,6 +48,7 @@
import org.springframework.http.client.ClientHttpRequestInitializer;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.GenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StreamUtils;
Expand Down Expand Up @@ -486,6 +492,49 @@ public void putNull() throws Exception {
verify(response).close();
}

@Test // gh-23740
public void headerAcceptAllOnPut() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setResponseCode(500).setBody("internal server error"));
server.start();
try {
template.setRequestFactory(new SimpleClientHttpRequestFactory());
template.put(server.url("/internal/server/error").uri(), null);
assertThat(server.takeRequest().getHeader("Accept")).isEqualTo("*/*");
}
finally {
server.shutdown();
}
}

@Test // gh-23740
public void keepGivenAcceptHeaderOnPut() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setResponseCode(500).setBody("internal server error"));
server.start();
try {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<>(null, headers);
template.setRequestFactory(new SimpleClientHttpRequestFactory());
template.exchange(server.url("/internal/server/error").uri(), PUT, entity, Void.class);

RecordedRequest request = server.takeRequest();

final List<List<String>> accepts = request.getHeaders().toMultimap().entrySet().stream()
.filter(entry -> entry.getKey().equalsIgnoreCase("accept"))
.map(Entry::getValue)
.collect(Collectors.toList());

assertThat(accepts).hasSize(1);
assertThat(accepts.get(0)).hasSize(1);
assertThat(accepts.get(0).get(0)).isEqualTo("application/json");
}
finally {
server.shutdown();
}
}

@Test
public void patchForObject() throws Exception {
mockTextPlainHttpMessageConverter();
Expand Down Expand Up @@ -532,6 +581,21 @@ public void delete() throws Exception {
verify(response).close();
}

@Test // gh-23740
public void headerAcceptAllOnDelete() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setResponseCode(500).setBody("internal server error"));
server.start();
try {
template.setRequestFactory(new SimpleClientHttpRequestFactory());
template.delete(server.url("/internal/server/error").uri());
assertThat(server.takeRequest().getHeader("Accept")).isEqualTo("*/*");
}
finally {
server.shutdown();
}
}

@Test
public void optionsForAllow() throws Exception {
mockSentRequest(OPTIONS, "https://example.com");
Expand Down

0 comments on commit c7b9988

Please sign in to comment.