From 8f7f3b29a73e3e7dcdef893a2094aa47e2951046 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Mon, 9 Jun 2014 22:02:58 -0400 Subject: [PATCH] Blow up on an unexpected request body. --- .../src/test/java/com/squareup/okhttp/CallTest.java | 11 +++++++++++ okhttp/src/main/java/com/squareup/okhttp/Request.java | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/okhttp-tests/src/test/java/com/squareup/okhttp/CallTest.java b/okhttp-tests/src/test/java/com/squareup/okhttp/CallTest.java index f5f5560c4..a3c425eab 100644 --- a/okhttp-tests/src/test/java/com/squareup/okhttp/CallTest.java +++ b/okhttp-tests/src/test/java/com/squareup/okhttp/CallTest.java @@ -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(); diff --git a/okhttp/src/main/java/com/squareup/okhttp/Request.java b/okhttp/src/main/java/com/squareup/okhttp/Request.java index 5d1ec8648..39adb8386 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/Request.java +++ b/okhttp/src/main/java/com/squareup/okhttp/Request.java @@ -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;