提交 51cda416 编写于 作者: J Jesse Wilson

Merge pull request #616 from square/jw/fetch-client-content-type

Include body mime type on AppEngine client requests.
...@@ -52,7 +52,7 @@ ...@@ -52,7 +52,7 @@
<gson.version>2.3</gson.version> <gson.version>2.3</gson.version>
<okhttp.version>2.0.0</okhttp.version> <okhttp.version>2.0.0</okhttp.version>
<rxjava.version>0.19.2</rxjava.version> <rxjava.version>0.19.2</rxjava.version>
<appengine.version>1.9.6</appengine.version> <appengine.version>1.9.12</appengine.version>
<!-- Converter Dependencies --> <!-- Converter Dependencies -->
<protobuf.version>2.5.0</protobuf.version> <protobuf.version>2.5.0</protobuf.version>
......
...@@ -71,6 +71,11 @@ public class UrlFetchClient implements Client { ...@@ -71,6 +71,11 @@ public class UrlFetchClient implements Client {
TypedOutput body = request.getBody(); TypedOutput body = request.getBody();
if (body != null) { if (body != null) {
String mimeType = body.mimeType();
if (mimeType != null) {
fetchRequest.addHeader(new HTTPHeader("Content-Type", mimeType));
}
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
body.writeTo(baos); body.writeTo(baos);
fetchRequest.setPayload(baos.toByteArray()); fetchRequest.setPayload(baos.toByteArray());
......
...@@ -4,12 +4,13 @@ package retrofit; ...@@ -4,12 +4,13 @@ package retrofit;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Map; import java.util.Map;
import retrofit.mime.MimeHelper;
import retrofit.mime.MultipartTypedOutput; import retrofit.mime.MultipartTypedOutput;
import retrofit.mime.TypedOutput; import retrofit.mime.TypedOutput;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public abstract class TestingUtils { public final class TestingUtils {
public static Method getMethod(Class c, String name) { public static Method getMethod(Class c, String name) {
for (Method method : c.getDeclaredMethods()) { for (Method method : c.getDeclaredMethods()) {
if (method.getName().equals(name)) { if (method.getName().equals(name)) {
...@@ -20,7 +21,7 @@ public abstract class TestingUtils { ...@@ -20,7 +21,7 @@ public abstract class TestingUtils {
} }
public static TypedOutput createMultipart(Map<String, TypedOutput> parts) { public static TypedOutput createMultipart(Map<String, TypedOutput> parts) {
MultipartTypedOutput typedOutput = new MultipartTypedOutput(); MultipartTypedOutput typedOutput = MimeHelper.newMultipart("foobarbaz");
for (Map.Entry<String, TypedOutput> part : parts.entrySet()) { for (Map.Entry<String, TypedOutput> part : parts.entrySet()) {
typedOutput.addPart(part.getKey(), part.getValue()); typedOutput.addPart(part.getKey(), part.getValue());
} }
......
...@@ -48,7 +48,8 @@ public class UrlFetchClientTest { ...@@ -48,7 +48,8 @@ public class UrlFetchClientTest {
assertThat(fetchRequest.getMethod()).isEqualTo(POST); assertThat(fetchRequest.getMethod()).isEqualTo(POST);
assertThat(fetchRequest.getURL().toString()).isEqualTo(HOST + "/foo/bar/"); assertThat(fetchRequest.getURL().toString()).isEqualTo(HOST + "/foo/bar/");
List<HTTPHeader> fetchHeaders = fetchRequest.getHeaders(); List<HTTPHeader> fetchHeaders = fetchRequest.getHeaders();
assertThat(fetchHeaders).hasSize(0); assertThat(fetchHeaders).hasSize(1);
assertHeader(fetchHeaders.get(0), "Content-Type", "text/plain; charset=UTF-8");
assertBytes(fetchRequest.getPayload(), "hi"); assertBytes(fetchRequest.getPayload(), "hi");
} }
...@@ -63,7 +64,8 @@ public class UrlFetchClientTest { ...@@ -63,7 +64,8 @@ public class UrlFetchClientTest {
assertThat(fetchRequest.getMethod()).isEqualTo(POST); assertThat(fetchRequest.getMethod()).isEqualTo(POST);
assertThat(fetchRequest.getURL().toString()).isEqualTo(HOST + "/that/"); assertThat(fetchRequest.getURL().toString()).isEqualTo(HOST + "/that/");
List<HTTPHeader> fetchHeaders = fetchRequest.getHeaders(); List<HTTPHeader> fetchHeaders = fetchRequest.getHeaders();
assertThat(fetchHeaders).hasSize(0); assertThat(fetchHeaders).hasSize(1);
assertHeader(fetchHeaders.get(0), "Content-Type", "multipart/form-data; boundary=foobarbaz");
assertThat(fetchRequest.getPayload()).isNotEmpty(); assertThat(fetchRequest.getPayload()).isNotEmpty();
} }
......
...@@ -11,4 +11,8 @@ public class MimeHelper { ...@@ -11,4 +11,8 @@ public class MimeHelper {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public static MultipartTypedOutput newMultipart(String boundary) {
return new MultipartTypedOutput(boundary);
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册