Skip to content

Commit

Permalink
Merge pull request Azure#243 from RikkiGibson/ThrowNullBodyParam
Browse files Browse the repository at this point in the history
Throw for null body param
  • Loading branch information
RikkiGibson authored Oct 2, 2017
2 parents af7c901 + 4403a24 commit 70373df
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -445,19 +445,19 @@ public void SyncPutRequestWithUnexpectedResponseAndExceptionType() {

@Host("http://httpbin.org")
private interface Service10 {
@HEAD("get")
@HEAD("anything")
@ExpectedResponses({200})
HttpBinJSON head();

@HEAD("get")
@HEAD("anything")
@ExpectedResponses({200})
void voidHead();

@HEAD("get")
@HEAD("anything")
@ExpectedResponses({200})
Single<HttpBinJSON> headAsync();

@HEAD("get")
@HEAD("anything")
@ExpectedResponses({200})
Completable completableHeadAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@ protected Single<HttpResponse> sendRequestInternalAsync(HttpRequest request) {
final String requestPathLower = requestPath.toLowerCase();
if (requestHost.equalsIgnoreCase("httpbin.org")) {
if (requestPathLower.equals("/anything") || requestPathLower.startsWith("/anything/")) {
final HttpBinJSON json = new HttpBinJSON();
json.url = request.url()
// This is just to mimic the behavior we've seen with httpbin.org.
.replace("%20", " ");
json.headers = toMap(request.headers());
response = new MockAzureHttpResponse(200, json);
if ("HEAD".equals(request.httpMethod())) {
response = new MockAzureHttpResponse(200, "");
} else {
final HttpBinJSON json = new HttpBinJSON();
json.url = request.url()
// This is just to mimic the behavior we've seen with httpbin.org.
.replace("%20", " ");
json.headers = toMap(request.headers());
response = new MockAzureHttpResponse(200, json);
}
}
else if (requestPathLower.startsWith("/bytes/")) {
final String byteCountString = requestPath.substring("/bytes/".length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,12 @@ public HttpResponse call(String responseBody) {
private Single<?> toProxyReturnValue(HttpResponse response, SwaggerMethodParser methodParser, final Type entityType) {
final TypeToken entityTypeToken = TypeToken.of(entityType);
final Single<?> asyncResult;
if (entityTypeToken.isSubtypeOf(void.class) || methodParser.httpMethod().equalsIgnoreCase("HEAD")) {
if (entityTypeToken.isSubtypeOf(void.class) || entityTypeToken.isSubtypeOf(Void.class)) {
asyncResult = Single.just(null);
} else if (methodParser.httpMethod().equalsIgnoreCase("HEAD")
&& (entityTypeToken.isSubtypeOf(boolean.class) || entityTypeToken.isSubtypeOf(Boolean.class))) {
boolean isSuccess = response.statusCode() / 100 == 2;
asyncResult = Single.just(isSuccess);
} else if (entityTypeToken.isSubtypeOf(InputStream.class)) {
asyncResult = response.bodyAsInputStreamAsync();
} else if (entityTypeToken.isSubtypeOf(byte[].class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,12 @@ public Object body(Object[] swaggerMethodArguments) {

if (bodyContentMethodParameterIndex != null
&& swaggerMethodArguments != null
&& 0 <= bodyContentMethodParameterIndex && bodyContentMethodParameterIndex < swaggerMethodArguments.length) {
&& 0 <= bodyContentMethodParameterIndex
&& bodyContentMethodParameterIndex < swaggerMethodArguments.length) {
result = swaggerMethodArguments[bodyContentMethodParameterIndex];
if (result == null) {
throw new IllegalArgumentException("Argument for @BodyParam parameter must be non-null.");
}
}

return result;
Expand Down Expand Up @@ -382,9 +386,8 @@ private static Iterable<EncodedParameter> getEncodedParameters(Iterable<Substitu
final int parameterIndex = substitution.methodParameterIndex();
if (0 <= parameterIndex && parameterIndex < methodArguments.length) {
final Object methodArgument = methodArguments[substitution.methodParameterIndex()];

String parameterValue = methodArgument == null ? "" : methodArgument.toString();
if (parameterValue != null && !parameterValue.isEmpty()) {
String parameterValue = methodArgument == null ? null : methodArgument.toString();
if (parameterValue != null) {
if (substitution.shouldEncode() && escaper != null) {
parameterValue = escaper.escape(parameterValue);
}
Expand Down
91 changes: 52 additions & 39 deletions client-runtime/src/test/java/com/microsoft/rest/RestProxyTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ public void AsyncPostRequestWithStringBody() {
assertEquals("I'm a post body!", (String)json.data);
}

@Test(expected = IllegalArgumentException.class)
public void SyncPostRequestWithNullBody() {
createService(Service8.class).post(null);
}

@Host("http://httpbin.org")
private interface Service9 {
@PUT("put")
Expand Down Expand Up @@ -504,19 +509,27 @@ public void AsyncPutRequestWithUnexpectedResponseAndExceptionType() {

@Host("http://httpbin.org")
private interface Service10 {
@HEAD("get")
@HEAD("anything")
@ExpectedResponses({200})
HttpBinJSON head();

@HEAD("get")
@HEAD("anything")
@ExpectedResponses({200})
boolean headBoolean();

@HEAD("anything")
@ExpectedResponses({200})
void voidHead();

@HEAD("get")
@HEAD("anything")
@ExpectedResponses({200})
Single<HttpBinJSON> headAsync();

@HEAD("get")
@HEAD("anything")
@ExpectedResponses({200})
Single<Boolean> headBooleanAsync();

@HEAD("anything")
@ExpectedResponses({200})
Completable completableHeadAsync();
}
Expand All @@ -528,6 +541,12 @@ public void SyncHeadRequest() {
assertNull(json);
}

@Test
public void SyncHeadBooleanRequest() {
final boolean result = createService(Service10.class).headBoolean();
assertTrue(result);
}

@Test
public void SyncVoidHeadRequest() {
createService(Service10.class)
Expand All @@ -542,6 +561,12 @@ public void AsyncHeadRequest() {
assertNull(json);
}

@Test
public void AsyncHeadBooleanRequest() {
final boolean result = createService(Service10.class).headBooleanAsync().toBlocking().value();
assertTrue(result);
}

@Test
public void AsyncCompletableHeadRequest() {
createService(Service10.class)
Expand Down Expand Up @@ -879,11 +904,10 @@ private interface Service19 {
HttpBinJSON putWithBodyParamApplicationOctetStreamContentTypeAndByteArrayBody(@BodyParam(ContentType.APPLICATION_OCTET_STREAM) byte[] body);
}

@Test
@Test(expected = IllegalArgumentException.class)
public void service19PutWithNoContentTypeAndStringBodyWithNullBody() {
final HttpBinJSON result = createService(Service19.class)
createService(Service19.class)
.putWithNoContentTypeAndStringBody(null);
assertEquals("", result.data);
}

@Test
Expand All @@ -900,11 +924,10 @@ public void service19PutWithNoContentTypeAndStringBodyWithNonEmptyBody() {
assertEquals("hello", result.data);
}

@Test
@Test(expected = IllegalArgumentException.class)
public void service19PutWithNoContentTypeAndByteArrayBodyWithNullBody() {
final HttpBinJSON result = createService(Service19.class)
createService(Service19.class)
.putWithNoContentTypeAndByteArrayBody(null);
assertEquals("", result.data);
}

@Test
Expand All @@ -921,11 +944,10 @@ public void service19PutWithNoContentTypeAndByteArrayBodyWithNonEmptyBody() {
assertEquals(new String(new byte[] { 0, 1, 2, 3, 4 }), result.data);
}

@Test
@Test(expected = IllegalArgumentException.class)
public void service19PutWithHeaderApplicationJsonContentTypeAndStringBodyWithNullBody() {
final HttpBinJSON result = createService(Service19.class)
createService(Service19.class)
.putWithHeaderApplicationJsonContentTypeAndStringBody(null);
assertEquals("", result.data);
}

@Test
Expand All @@ -942,11 +964,10 @@ public void service19PutWithHeaderApplicationJsonContentTypeAndStringBodyWithNon
assertEquals("\"soups and stuff\"", result.data);
}

@Test
@Test(expected = IllegalArgumentException.class)
public void service19PutWithHeaderApplicationJsonContentTypeAndByteArrayBodyWithNullBody() {
final HttpBinJSON result = createService(Service19.class)
createService(Service19.class)
.putWithHeaderApplicationJsonContentTypeAndByteArrayBody(null);
assertEquals("", result.data);
}

@Test
Expand All @@ -963,11 +984,10 @@ public void service19PutWithHeaderApplicationJsonContentTypeAndByteArrayBodyWith
assertEquals("\"AAECAwQ=\"", result.data);
}

@Test
@Test(expected = IllegalArgumentException.class)
public void service19PutWithHeaderApplicationJsonContentTypeAndCharsetAndStringBodyWithNullBody() {
final HttpBinJSON result = createService(Service19.class)
createService(Service19.class)
.putWithHeaderApplicationJsonContentTypeAndCharsetAndStringBody(null);
assertEquals("", result.data);
}

@Test
Expand All @@ -984,11 +1004,10 @@ public void service19PutWithHeaderApplicationJsonContentTypeAndCharsetAndStringB
assertEquals("\"soups and stuff\"", result.data);
}

@Test
@Test(expected = IllegalArgumentException.class)
public void service19PutWithHeaderApplicationOctetStreamContentTypeAndStringBodyWithNullBody() {
final HttpBinJSON result = createService(Service19.class)
createService(Service19.class)
.putWithHeaderApplicationOctetStreamContentTypeAndStringBody(null);
assertEquals("", result.data);
}

@Test
Expand All @@ -1005,11 +1024,10 @@ public void service19PutWithHeaderApplicationOctetStreamContentTypeAndStringBody
assertEquals("penguins", result.data);
}

@Test
@Test(expected = IllegalArgumentException.class)
public void service19PutWithHeaderApplicationOctetStreamContentTypeAndByteArrayBodyWithNullBody() {
final HttpBinJSON result = createService(Service19.class)
.putWithHeaderApplicationOctetStreamContentTypeAndByteArrayBody(null);
assertEquals("", result.data);
}

@Test
Expand All @@ -1026,11 +1044,10 @@ public void service19PutWithHeaderApplicationOctetStreamContentTypeAndByteArrayB
assertEquals(new String(new byte[] { 0, 1, 2, 3, 4 }), result.data);
}

@Test
@Test(expected = IllegalArgumentException.class)
public void service19PutWithBodyParamApplicationJsonContentTypeAndStringBodyWithNullBody() {
final HttpBinJSON result = createService(Service19.class)
createService(Service19.class)
.putWithBodyParamApplicationJsonContentTypeAndStringBody(null);
assertEquals("", result.data);
}

@Test
Expand All @@ -1047,11 +1064,10 @@ public void service19PutWithBodyParamApplicationJsonContentTypeAndStringBodyWith
assertEquals("\"soups and stuff\"", result.data);
}

@Test
@Test(expected = IllegalArgumentException.class)
public void service19PutWithBodyParamApplicationJsonContentTypeAndCharsetAndStringBodyWithNullBody() {
final HttpBinJSON result = createService(Service19.class)
createService(Service19.class)
.putWithBodyParamApplicationJsonContentTypeAndCharsetAndStringBody(null);
assertEquals("", result.data);
}

@Test
Expand All @@ -1068,11 +1084,10 @@ public void service19PutWithBodyParamApplicationJsonContentTypeAndCharsetAndStri
assertEquals("\"soups and stuff\"", result.data);
}

@Test
@Test(expected = IllegalArgumentException.class)
public void service19PutWithBodyParamApplicationJsonContentTypeAndByteArrayBodyWithNullBody() {
final HttpBinJSON result = createService(Service19.class)
createService(Service19.class)
.putWithBodyParamApplicationJsonContentTypeAndByteArrayBody(null);
assertEquals("", result.data);
}

@Test
Expand All @@ -1089,11 +1104,10 @@ public void service19PutWithBodyParamApplicationJsonContentTypeAndByteArrayBodyW
assertEquals("\"AAECAwQ=\"", result.data);
}

@Test
@Test(expected = IllegalArgumentException.class)
public void service19PutWithBodyParamApplicationOctetStreamContentTypeAndStringBodyWithNullBody() {
final HttpBinJSON result = createService(Service19.class)
createService(Service19.class)
.putWithBodyParamApplicationOctetStreamContentTypeAndStringBody(null);
assertEquals("", result.data);
}

@Test
Expand All @@ -1110,11 +1124,10 @@ public void service19PutWithBodyParamApplicationOctetStreamContentTypeAndStringB
assertEquals("penguins", result.data);
}

@Test
@Test(expected = IllegalArgumentException.class)
public void service19PutWithBodyParamApplicationOctetStreamContentTypeAndByteArrayBodyWithNullBody() {
final HttpBinJSON result = createService(Service19.class)
createService(Service19.class)
.putWithBodyParamApplicationOctetStreamContentTypeAndByteArrayBody(null);
assertEquals("", result.data);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ protected Single<HttpResponse> sendRequestInternalAsync(HttpRequest request) {
final String requestPath = requestUrl.getPath();
final String requestPathLower = requestPath.toLowerCase();
if (requestPathLower.equals("/anything") || requestPathLower.startsWith("/anything/")) {
final HttpBinJSON json = new HttpBinJSON();
json.url = request.url()
// This is just to mimic the behavior we've seen with httpbin.org.
.replace("%20", " ");
json.headers = toMap(request.headers());
response = new MockHttpResponse(200, json);
if ("HEAD".equals(request.httpMethod())) {
response = new MockHttpResponse(200, "");
} else {
final HttpBinJSON json = new HttpBinJSON();
json.url = request.url()
// This is just to mimic the behavior we've seen with httpbin.org.
.replace("%20", " ");
json.headers = toMap(request.headers());
response = new MockHttpResponse(200, json);
}
}
else if (requestPathLower.startsWith("/bytes/")) {
final String byteCountString = requestPath.substring("/bytes/".length());
Expand Down

0 comments on commit 70373df

Please sign in to comment.