diff --git a/src/main/java/org/scribe/model/Request.java b/src/main/java/org/scribe/model/Request.java index 4a68b91..4e78bcc 100644 --- a/src/main/java/org/scribe/model/Request.java +++ b/src/main/java/org/scribe/model/Request.java @@ -17,6 +17,8 @@ import org.scribe.utils.*; class Request { private static final String CONTENT_LENGTH = "Content-Length"; + private static final String CONTENT_TYPE = "Content-Type"; + public static final String DEFAULT_CONTENT_TYPE = "application/x-www-form-urlencoded"; private String url; private Verb verb; @@ -108,6 +110,12 @@ class Request void addBody(HttpURLConnection conn, byte[] content) throws IOException { conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.length)); + + // Set default content type if none is set. + if (conn.getRequestProperty(CONTENT_TYPE) == null) + { + conn.setRequestProperty(CONTENT_TYPE, DEFAULT_CONTENT_TYPE); + } conn.setDoOutput(true); conn.getOutputStream().write(content); } diff --git a/src/test/java/org/scribe/model/ConnectionStub.java b/src/test/java/org/scribe/model/ConnectionStub.java index 934f633..7e93313 100644 --- a/src/test/java/org/scribe/model/ConnectionStub.java +++ b/src/test/java/org/scribe/model/ConnectionStub.java @@ -22,6 +22,12 @@ public class ConnectionStub extends HttpURLConnection headers.put(key, value); } + @Override + public String getRequestProperty(String s) + { + return headers.get(s); + } + public Map getHeaders() { return headers; diff --git a/src/test/java/org/scribe/model/RequestTest.java b/src/test/java/org/scribe/model/RequestTest.java index 2a2e90e..5bf46db 100644 --- a/src/test/java/org/scribe/model/RequestTest.java +++ b/src/test/java/org/scribe/model/RequestTest.java @@ -6,7 +6,6 @@ import org.junit.*; public class RequestTest { - private Request getRequest; private Request postRequest; private ConnectionStub connection; @@ -80,4 +79,42 @@ public class RequestTest { assertTrue(getRequest.getQueryStringParams().get("other param").equals("value with spaces")); } + + @Test + public void shouldAutomaticallyAddContentTypeForPostRequestsWithBytePayload() + { + postRequest.addPayload("PAYLOAD".getBytes()); + postRequest.send(); + assertEquals(Request.DEFAULT_CONTENT_TYPE, connection.getHeaders().get("Content-Type")); + } + + @Test + public void shouldAutomaticallyAddContentTypeForPostRequestsWithStringPayload() + { + postRequest.addPayload("PAYLOAD"); + postRequest.send(); + assertEquals(Request.DEFAULT_CONTENT_TYPE, connection.getHeaders().get("Content-Type")); + } + + @Test + public void shouldAutomaticallyAddContentTypeForPostRequestsWithBodyParameters() + { + postRequest.send(); + assertEquals(Request.DEFAULT_CONTENT_TYPE, connection.getHeaders().get("Content-Type")); + } + + @Test + public void shouldBeAbleToOverrideItsContentType() + { + postRequest.addHeader("Content-Type", "my-content-type"); + postRequest.send(); + assertEquals("my-content-type", connection.getHeaders().get("Content-Type")); + } + + @Test + public void shouldNotAddContentTypeForGetRequests() + { + getRequest.send(); + assertFalse(connection.getHeaders().containsKey("Content-Type")); + } } \ No newline at end of file