Fix HttpUrlConnection DELETE without body

The following commit allowed HTTP DELETE with body:
584b831bb9

However it broke buffered requests even without a body since JDK 1.6
and 1.7 do not support calls to getOutputStream with HTTP DELETE.

This commit set the doOutput flag back to false if the actual buffered
body is 0 length.

Issue: SPR-12361
This commit is contained in:
Rossen Stoyanchev
2014-11-10 14:14:02 -05:00
parent 18033486ae
commit 53eec48ffd
3 changed files with 21 additions and 0 deletions

View File

@@ -18,9 +18,12 @@ package org.springframework.http.client;
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URI;
import java.net.URL;
import org.junit.Test;
@@ -56,6 +59,14 @@ public class BufferedSimpleHttpRequestFactoryTests extends AbstractHttpRequestFa
testRequestBodyAllowed(uri, "DELETE", true);
}
@Test
public void deleteWithoutBodyDoesNotRaiseException() throws Exception {
HttpURLConnection connection = new TestHttpURLConnection(new URL("http://example.com"));
((SimpleClientHttpRequestFactory) this.factory).prepareConnection(connection, "DELETE");
SimpleBufferingClientHttpRequest request = new SimpleBufferingClientHttpRequest(connection, false);
request.execute();
}
private void testRequestBodyAllowed(URL uri, String httpMethod, boolean allowed) throws IOException {
HttpURLConnection connection = new TestHttpURLConnection(uri);
((SimpleClientHttpRequestFactory) this.factory).prepareConnection(connection, httpMethod);