Skip to content

Commit

Permalink
Improve error message for null keys/values in getParams(). (#203)
Browse files Browse the repository at this point in the history
Fixes #201
  • Loading branch information
jpd236 committed Jun 18, 2018
1 parent 2695d3c commit b080a34
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/android/volley/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,14 @@ private byte[] encodeParameters(Map<String, String> params, String paramsEncodin
StringBuilder encodedParams = new StringBuilder();
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
if (entry.getKey() == null || entry.getValue() == null) {
throw new IllegalArgumentException(
String.format(
"Request#getParams() or Request#getPostParams() returned a map "
+ "containing a null key or value: (%s, %s). All keys "
+ "and values must be non-null.",
entry.getKey(), entry.getValue()));
}
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/com/android/volley/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import com.android.volley.Request.Method;
import com.android.volley.Request.Priority;
import java.util.Collections;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
Expand Down Expand Up @@ -119,4 +121,72 @@ protected Response<Object> parseNetworkResponse(NetworkResponse response) {
return null;
}
}

@Test
public void nullKeyInPostParams() throws Exception {
Request<Object> request =
new Request<Object>(Method.POST, "url", null) {
@Override
protected void deliverResponse(Object response) {}

@Override
protected Response<Object> parseNetworkResponse(NetworkResponse response) {
return null;
}

@Override
protected Map<String, String> getParams() {
return Collections.singletonMap(null, "value");
}

@Override
protected Map<String, String> getPostParams() {
return Collections.singletonMap(null, "value");
}
};
try {
request.getBody();
} catch (IllegalArgumentException e) {
// expected
}
try {
request.getPostBody();
} catch (IllegalArgumentException e) {
// expected
}
}

@Test
public void nullValueInPostParams() throws Exception {
Request<Object> request =
new Request<Object>(Method.POST, "url", null) {
@Override
protected void deliverResponse(Object response) {}

@Override
protected Response<Object> parseNetworkResponse(NetworkResponse response) {
return null;
}

@Override
protected Map<String, String> getParams() {
return Collections.singletonMap("key", null);
}

@Override
protected Map<String, String> getPostParams() {
return Collections.singletonMap("key", null);
}
};
try {
request.getBody();
} catch (IllegalArgumentException e) {
// expected
}
try {
request.getPostBody();
} catch (IllegalArgumentException e) {
// expected
}
}
}

0 comments on commit b080a34

Please sign in to comment.