Skip to content

Commit

Permalink
Merge branch 'patch' of https://github.com/adriancole/retrofit into a…
Browse files Browse the repository at this point in the history
…driancole-patch
  • Loading branch information
JakeWharton committed Aug 8, 2013
2 parents c5124b4 + e704b80 commit a98a834
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
27 changes: 26 additions & 1 deletion retrofit/src/main/java/retrofit/client/UrlConnectionClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()) {
Expand Down
30 changes: 30 additions & 0 deletions retrofit/src/main/java/retrofit/http/PATCH.java
Original file line number Diff line number Diff line change
@@ -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();
}
17 changes: 17 additions & 0 deletions retrofit/src/test/java/retrofit/RestMethodInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -315,6 +316,22 @@ class Example {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ public class UrlConnectionClientTest {
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);
Expand Down

0 comments on commit a98a834

Please sign in to comment.