1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-18 20:40:58 +03:00

Merge pull request #908 from square/jwilson_0609_blow_up_on_unexpected_request_body

Blow up on an unexpected request body.
This commit is contained in:
Jesse Wilson
2014-06-10 01:11:53 -04:00
2 changed files with 15 additions and 0 deletions

View File

@@ -129,6 +129,17 @@ public final class CallTest {
get();
}
@Test public void getWithRequestBody() throws Exception {
server.enqueue(new MockResponse());
server.play();
try {
new Request.Builder().method("GET", RequestBody.create(MediaType.parse("text/plain"), "abc"));
fail();
} catch (IllegalArgumentException expected) {
}
}
@Test public void head() throws Exception {
server.enqueue(new MockResponse().addHeader("Content-Type: text/plain"));
server.play();

View File

@@ -16,6 +16,7 @@
package com.squareup.okhttp;
import com.squareup.okhttp.internal.Platform;
import com.squareup.okhttp.internal.http.HttpMethod;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
@@ -204,6 +205,9 @@ public final class Request {
if (method == null || method.length() == 0) {
throw new IllegalArgumentException("method == null || method.length() == 0");
}
if (body != null && !HttpMethod.hasRequestBody(method)) {
throw new IllegalArgumentException("method " + method + " must not have a request body.");
}
this.method = method;
this.body = body;
return this;