mirror of
https://github.com/square/okhttp.git
synced 2026-01-22 15:42:00 +03:00
Merge pull request #583 from square/jwilson_0301_method
Fix and test PATCH.
This commit is contained in:
@@ -1340,6 +1340,7 @@ public final class URLConnectionTest {
|
||||
assertValidRequestMethod("POST");
|
||||
assertValidRequestMethod("PUT");
|
||||
assertValidRequestMethod("TRACE");
|
||||
assertValidRequestMethod("PATCH");
|
||||
}
|
||||
|
||||
private void assertValidRequestMethod(String requestMethod) throws Exception {
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.squareup.okhttp;
|
||||
|
||||
import com.squareup.okhttp.internal.DiskLruCache;
|
||||
import com.squareup.okhttp.internal.Util;
|
||||
import com.squareup.okhttp.internal.http.HttpMethod;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
@@ -195,8 +196,7 @@ public final class HttpResponseCache extends ResponseCache implements OkResponse
|
||||
}
|
||||
|
||||
@Override public boolean maybeRemove(Request request) {
|
||||
String method = request.method();
|
||||
if (method.equals("POST") || method.equals("PUT") || method.equals("DELETE")) {
|
||||
if (HttpMethod.invalidatesCache(request.method())) {
|
||||
try {
|
||||
cache.remove(urlToKey(request));
|
||||
} catch (IOException ignored) {
|
||||
|
||||
@@ -254,8 +254,7 @@ public class HttpEngine {
|
||||
}
|
||||
|
||||
boolean hasRequestBody() {
|
||||
String method = request.method();
|
||||
return method.equals("POST") || method.equals("PUT") || method.equals("PATCH");
|
||||
return HttpMethod.hasRequestBody(request.method());
|
||||
}
|
||||
|
||||
/** Returns the request body or null if this request doesn't have a body. */
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (C) 2014 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 com.squareup.okhttp.internal.http;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public final class HttpMethod {
|
||||
public static final Set<String> METHODS = new LinkedHashSet<String>(Arrays.asList(
|
||||
"OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "PATCH"));
|
||||
|
||||
public static boolean invalidatesCache(String method) {
|
||||
return method.equals("POST")
|
||||
|| method.equals("PATCH")
|
||||
|| method.equals("PUT")
|
||||
|| method.equals("DELETE");
|
||||
}
|
||||
|
||||
public static boolean hasRequestBody(String method) {
|
||||
return method.equals("POST")
|
||||
|| method.equals("PUT")
|
||||
|| method.equals("PATCH");
|
||||
}
|
||||
|
||||
private HttpMethod() {
|
||||
}
|
||||
}
|
||||
@@ -265,7 +265,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
|
||||
if (method.equals("GET")) {
|
||||
// they are requesting a stream to write to. This implies a POST method
|
||||
method = "POST";
|
||||
} else if (!method.equals("POST") && !method.equals("PUT") && !method.equals("PATCH")) {
|
||||
} else if (!HttpMethod.hasRequestBody(method)) {
|
||||
// If the request method is neither POST nor PUT nor PATCH, then you're not writing
|
||||
throw new ProtocolException(method + " does not support writing");
|
||||
}
|
||||
@@ -570,6 +570,14 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
|
||||
client.setProtocols(protocolsList);
|
||||
}
|
||||
|
||||
@Override public void setRequestMethod(String method) throws ProtocolException {
|
||||
if (!HttpMethod.METHODS.contains(method)) {
|
||||
throw new ProtocolException(
|
||||
"Expected one of " + HttpMethod.METHODS + " but was " + method);
|
||||
}
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
@Override public void setFixedLengthStreamingMode(int contentLength) {
|
||||
setFixedLengthStreamingMode((long) contentLength);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user