Permalink
Browse files

fixes #282 add PATCH support

  • Loading branch information...
1 parent 613e85c commit e704b800878b2e37f5ac98b0139cb4994618ace0 @adriancole committed Aug 5, 2013
@@ -17,16 +17,31 @@
import java.io.IOException;
import java.io.InputStream;
+import java.lang.reflect.Field;
import java.net.HttpURLConnection;
+import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+
+import retrofit.RetrofitError;
import retrofit.mime.TypedInput;
import retrofit.mime.TypedOutput;
/** Retrofit client that uses {@link HttpURLConnection} for communication. */
public class UrlConnectionClient implements Client {
+ private final Field methodField;
+
+ public UrlConnectionClient() {
+ try {
+ this.methodField = HttpURLConnection.class.getDeclaredField("method");
+ this.methodField.setAccessible(true);
+ } catch (NoSuchFieldException e) {
+ throw RetrofitError.unexpectedError(null, e);
+ }
+ }
+
@Override public Response execute(Request request) throws IOException {
HttpURLConnection connection = openConnection(request);
prepareRequest(connection, request);
@@ -42,7 +57,17 @@ protected HttpURLConnection openConnection(Request request) throws IOException {
}
void prepareRequest(HttpURLConnection connection, Request request) throws IOException {
- connection.setRequestMethod(request.getMethod());
+ // HttpURLConnection artificially restricts request method
+ try {
+ connection.setRequestMethod(request.getMethod());
+ } catch (ProtocolException e) {
+ try {
+ methodField.set(connection, request.getMethod());
+ } catch (IllegalAccessException e1) {
+ throw RetrofitError.unexpectedError(request.getUrl(), e1);
+ }
+ }
+
connection.setDoInput(true);
for (Header header : request.getHeaders()) {
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2013 Square, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package retrofit.http;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/** Make a PATCH request to a REST path relative to base URL. */
+@Target(METHOD)
+@Retention(RUNTIME)
+@RestMethod(value = "PATCH", hasBody = true)
+public @interface PATCH {
+ String value();
+}
@@ -20,6 +20,7 @@
import retrofit.http.Header;
import retrofit.http.Headers;
import retrofit.http.Multipart;
+import retrofit.http.PATCH;
import retrofit.http.POST;
import retrofit.http.PUT;
import retrofit.http.Part;
@@ -315,6 +316,22 @@ Response a() {
assertThat(methodInfo.requestUrl).isEqualTo("/foo");
}
+ @Test public void patchMethod() {
+ class Example {
+ @PATCH("/foo") Response a() {
+ return null;
+ }
+ }
+
+ Method method = TestingUtils.getMethod(Example.class, "a");
+ RestMethodInfo methodInfo = new RestMethodInfo(method);
+ methodInfo.init();
+
+ assertThat(methodInfo.requestMethod).isEqualTo("PATCH");
+ assertThat(methodInfo.requestHasBody).isTrue();
+ assertThat(methodInfo.requestUrl).isEqualTo("/foo");
+ }
+
@RestMethod("CUSTOM1")
@Target(METHOD) @Retention(RUNTIME)
private @interface CUSTOM1 {
@@ -37,6 +37,22 @@
assertThat(connection.getHeaderFields()).isEmpty();
}
+ @Test public void patch() throws Exception {
+ TypedString body = new TypedString("hi");
+ Request request = new Request("PATCH", HOST + "/foo/bar/", null, body);
+
+ DummyHttpUrlConnection connection = (DummyHttpUrlConnection) client.openConnection(request);
+ client.prepareRequest(connection, request);
+
+ assertThat(connection.getRequestMethod()).isEqualTo("PATCH");
+ assertThat(connection.getURL().toString()).isEqualTo(HOST + "/foo/bar/");
+ assertThat(connection.getRequestProperties()).hasSize(2);
+ assertThat(connection.getRequestProperty("Content-Type")) //
+ .isEqualTo("text/plain; charset=UTF-8");
+ assertThat(connection.getRequestProperty("Content-Length")).isEqualTo("2");
+ assertBytes(connection.getOutputStream().toByteArray(), "hi");
+ }
+
@Test public void post() throws Exception {
TypedString body = new TypedString("hi");
Request request = new Request("POST", HOST + "/foo/bar/", null, body);

0 comments on commit e704b80

Please sign in to comment.