Commit 17901b95 authored by Caleb Raitto's avatar Caleb Raitto Committed by Commit Bot

Implement request body in FakeUrlRequest

Create FakeDataSink class to retrieve request body from
UploadDataProvider. Add request body byte array as a parameter to
ResponseMatcher.getResponse(). Added ResponseMatchers should
verify the body of the request if that is necessary for the test
before responding.

Patched from original CL: crrev.com/c/1742515 (kirchman@'s internship
ended).

Bug: 669707
Change-Id: I3be8a95e80e82cf74edd7bd5e23e5adf2e5e04cf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1784978
Commit-Queue: Paul Jensen <pauljensen@chromium.org>
Reviewed-by: default avatarPaul Jensen <pauljensen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699875}
parent 6572a39c
...@@ -925,6 +925,7 @@ if (!is_component_build) { ...@@ -925,6 +925,7 @@ if (!is_component_build) {
android_library("cronet_common_javatests") { android_library("cronet_common_javatests") {
testonly = true testonly = true
java_files = [ java_files = [
"test/javatests/src/org/chromium/net/TestUploadDataProvider.java",
"test/javatests/src/org/chromium/net/TestUrlRequestCallback.java", "test/javatests/src/org/chromium/net/TestUrlRequestCallback.java",
"test/javatests/src/org/chromium/net/CronetTestRule.java", "test/javatests/src/org/chromium/net/CronetTestRule.java",
] ]
...@@ -1005,7 +1006,6 @@ if (!is_component_build) { ...@@ -1005,7 +1006,6 @@ if (!is_component_build) {
"test/javatests/src/org/chromium/net/TestDrivenDataProvider.java", "test/javatests/src/org/chromium/net/TestDrivenDataProvider.java",
"test/javatests/src/org/chromium/net/TestNetworkQualityRttListener.java", "test/javatests/src/org/chromium/net/TestNetworkQualityRttListener.java",
"test/javatests/src/org/chromium/net/TestNetworkQualityThroughputListener.java", "test/javatests/src/org/chromium/net/TestNetworkQualityThroughputListener.java",
"test/javatests/src/org/chromium/net/TestUploadDataProvider.java",
"test/javatests/src/org/chromium/net/UploadDataProvidersTest.java", "test/javatests/src/org/chromium/net/UploadDataProvidersTest.java",
"test/javatests/src/org/chromium/net/urlconnection/CronetBufferedOutputStreamTest.java", "test/javatests/src/org/chromium/net/urlconnection/CronetBufferedOutputStreamTest.java",
"test/javatests/src/org/chromium/net/urlconnection/CronetChunkedOutputStreamTest.java", "test/javatests/src/org/chromium/net/urlconnection/CronetChunkedOutputStreamTest.java",
......
...@@ -178,14 +178,15 @@ public final class FakeCronetController { ...@@ -178,14 +178,15 @@ public final class FakeCronetController {
* @param url the URL that the {@link UrlRequest} is connecting to * @param url the URL that the {@link UrlRequest} is connecting to
* @param httpMethod the HTTP method that the {@link UrlRequest} is using to connect with * @param httpMethod the HTTP method that the {@link UrlRequest} is using to connect with
* @param headers the headers supplied by the {@link UrlRequest} * @param headers the headers supplied by the {@link UrlRequest}
* @param body the body of the fake HTTP request
* @return a {@link FakeUrlResponse} if there is one, or a failed "404" response if none found * @return a {@link FakeUrlResponse} if there is one, or a failed "404" response if none found
*/ */
FakeUrlResponse getResponse( FakeUrlResponse getResponse(
String url, String httpMethod, List<Map.Entry<String, String>> headers) { String url, String httpMethod, List<Map.Entry<String, String>> headers, byte[] body) {
synchronized (mResponseMatchers) { synchronized (mResponseMatchers) {
for (ResponseMatcher responseMatcher : mResponseMatchers) { for (ResponseMatcher responseMatcher : mResponseMatchers) {
FakeUrlResponse matchedResponse = FakeUrlResponse matchedResponse =
responseMatcher.getMatchingResponse(url, httpMethod, headers); responseMatcher.getMatchingResponse(url, httpMethod, headers, body);
if (matchedResponse != null) { if (matchedResponse != null) {
return matchedResponse; return matchedResponse;
} }
......
...@@ -110,7 +110,7 @@ public class FakeUrlResponse { ...@@ -110,7 +110,7 @@ public class FakeUrlResponse {
* Sets the HTTP status code. The default value is 200. * Sets the HTTP status code. The default value is 200.
* *
* @param httpStatusCode for {@link UrlResponseInfo.getHttpStatusCode()} * @param httpStatusCode for {@link UrlResponseInfo.getHttpStatusCode()}
* @return a builder with the corresponding HTTP status code set * @return the builder with the corresponding HTTP status code set
*/ */
public Builder setHttpStatusCode(int httpStatusCode) { public Builder setHttpStatusCode(int httpStatusCode) {
mHttpStatusCode = httpStatusCode; mHttpStatusCode = httpStatusCode;
...@@ -122,7 +122,7 @@ public class FakeUrlResponse { ...@@ -122,7 +122,7 @@ public class FakeUrlResponse {
* *
* @param name the name of the header key, for example, "location" for a redirect header * @param name the name of the header key, for example, "location" for a redirect header
* @param value the header value * @param value the header value
* @return a builder with the corresponding header set * @return the builder with the corresponding header set
*/ */
public Builder addHeader(String name, String value) { public Builder addHeader(String name, String value) {
mAllHeadersList.add(new AbstractMap.SimpleEntry<>(name, value)); mAllHeadersList.add(new AbstractMap.SimpleEntry<>(name, value));
...@@ -133,7 +133,7 @@ public class FakeUrlResponse { ...@@ -133,7 +133,7 @@ public class FakeUrlResponse {
* Sets result of {@link UrlResponseInfo.wasCached()}. The default wasCached value is false. * Sets result of {@link UrlResponseInfo.wasCached()}. The default wasCached value is false.
* *
* @param wasCached for {@link UrlResponseInfo.wasCached()} * @param wasCached for {@link UrlResponseInfo.wasCached()}
* @return a builder with the corresponding wasCached field set * @return the builder with the corresponding wasCached field set
*/ */
public Builder setWasCached(boolean wasCached) { public Builder setWasCached(boolean wasCached) {
mWasCached = wasCached; mWasCached = wasCached;
...@@ -145,7 +145,7 @@ public class FakeUrlResponse { ...@@ -145,7 +145,7 @@ public class FakeUrlResponse {
* protocol is an empty string. * protocol is an empty string.
* *
* @param negotiatedProtocol for {@link UrlResponseInfo.getNegotiatedProtocol()} * @param negotiatedProtocol for {@link UrlResponseInfo.getNegotiatedProtocol()}
* @return a builder with the corresponding negotiatedProtocol field set * @return the builder with the corresponding negotiatedProtocol field set
*/ */
public Builder setNegotiatedProtocol(String negotiatedProtocol) { public Builder setNegotiatedProtocol(String negotiatedProtocol) {
mNegotiatedProtocol = negotiatedProtocol; mNegotiatedProtocol = negotiatedProtocol;
...@@ -157,7 +157,7 @@ public class FakeUrlResponse { ...@@ -157,7 +157,7 @@ public class FakeUrlResponse {
* empty string. * empty string.
* *
* @param proxyServer for {@link UrlResponseInfo.getProxyServer()} * @param proxyServer for {@link UrlResponseInfo.getProxyServer()}
* @return a builder with the corresponding proxyServer field set * @return the builder with the corresponding proxyServer field set
*/ */
public Builder setProxyServer(String proxyServer) { public Builder setProxyServer(String proxyServer) {
mProxyServer = proxyServer; mProxyServer = proxyServer;
...@@ -167,8 +167,8 @@ public class FakeUrlResponse { ...@@ -167,8 +167,8 @@ public class FakeUrlResponse {
/** /**
* Sets the response body for a response. The default response body is an empty byte array. * Sets the response body for a response. The default response body is an empty byte array.
* *
* @param responseBody all the information the server returns * @param body all the information the server returns
* @return a builder with the corresponding responseBody field set * @return the builder with the corresponding responseBody field set
*/ */
public Builder setResponseBody(byte[] body) { public Builder setResponseBody(byte[] body) {
mResponseBody = body; mResponseBody = body;
......
...@@ -21,9 +21,10 @@ public interface ResponseMatcher { ...@@ -21,9 +21,10 @@ public interface ResponseMatcher {
* @param url the URL the {@link UrlRequest} is connecting to * @param url the URL the {@link UrlRequest} is connecting to
* @param httpMethod the HTTP method the {@link UrlRequest} is connecting with * @param httpMethod the HTTP method the {@link UrlRequest} is connecting with
* @param headers the {@link UrlRequest} headers * @param headers the {@link UrlRequest} headers
* @param body the body of the request
* @return a {@link FakeUrlResponse} if there is a matching response, or {@code null} otherwise * @return a {@link FakeUrlResponse} if there is a matching response, or {@code null} otherwise
*/ */
@Nullable @Nullable
FakeUrlResponse getMatchingResponse( FakeUrlResponse getMatchingResponse(
String url, String httpMethod, List<Map.Entry<String, String>> headers); String url, String httpMethod, List<Map.Entry<String, String>> headers, byte[] body);
} }
...@@ -35,7 +35,7 @@ public class UrlResponseMatcher implements ResponseMatcher { ...@@ -35,7 +35,7 @@ public class UrlResponseMatcher implements ResponseMatcher {
@Override @Override
public FakeUrlResponse getMatchingResponse( public FakeUrlResponse getMatchingResponse(
String url, String httpMethod, List<Map.Entry<String, String>> headers) { String url, String httpMethod, List<Map.Entry<String, String>> headers, byte[] body) {
return mUrl.equals(url) ? mResponse : null; return mUrl.equals(url) ? mResponse : null;
} }
} }
...@@ -140,7 +140,7 @@ public class FakeCronetControllerTest { ...@@ -140,7 +140,7 @@ public class FakeCronetControllerTest {
mFakeCronetController.addSuccessResponse(url, "different text".getBytes()); mFakeCronetController.addSuccessResponse(url, "different text".getBytes());
FakeUrlResponse foundResponse = FakeUrlResponse foundResponse =
mFakeCronetController.getResponse(new String(url), null, null); mFakeCronetController.getResponse(new String(url), null, null, null);
assertEquals(response, foundResponse); assertEquals(response, foundResponse);
} }
...@@ -155,7 +155,7 @@ public class FakeCronetControllerTest { ...@@ -155,7 +155,7 @@ public class FakeCronetControllerTest {
mFakeCronetController.addResponseMatcher(matcher); mFakeCronetController.addResponseMatcher(matcher);
mFakeCronetController.removeResponseMatcher(matcher); mFakeCronetController.removeResponseMatcher(matcher);
FakeUrlResponse foundResponse = mFakeCronetController.getResponse(url, null, null); FakeUrlResponse foundResponse = mFakeCronetController.getResponse(url, null, null, null);
assertEquals(404, foundResponse.getHttpStatusCode()); assertEquals(404, foundResponse.getHttpStatusCode());
assertNotEquals(response, foundResponse); assertNotEquals(response, foundResponse);
...@@ -171,7 +171,7 @@ public class FakeCronetControllerTest { ...@@ -171,7 +171,7 @@ public class FakeCronetControllerTest {
mFakeCronetController.addResponseMatcher(matcher); mFakeCronetController.addResponseMatcher(matcher);
mFakeCronetController.clearResponseMatchers(); mFakeCronetController.clearResponseMatchers();
FakeUrlResponse foundResponse = mFakeCronetController.getResponse(url, null, null); FakeUrlResponse foundResponse = mFakeCronetController.getResponse(url, null, null, null);
assertEquals(404, foundResponse.getHttpStatusCode()); assertEquals(404, foundResponse.getHttpStatusCode());
assertNotEquals(response, foundResponse); assertNotEquals(response, foundResponse);
...@@ -185,7 +185,7 @@ public class FakeCronetControllerTest { ...@@ -185,7 +185,7 @@ public class FakeCronetControllerTest {
new FakeUrlResponse.Builder().setResponseBody("body text".getBytes()).build(); new FakeUrlResponse.Builder().setResponseBody("body text".getBytes()).build();
mFakeCronetController.addResponseForUrl(response, url); mFakeCronetController.addResponseForUrl(response, url);
FakeUrlResponse foundResponse = mFakeCronetController.getResponse(url, null, null); FakeUrlResponse foundResponse = mFakeCronetController.getResponse(url, null, null, null);
assertEquals(foundResponse, response); assertEquals(foundResponse, response);
} }
...@@ -193,7 +193,7 @@ public class FakeCronetControllerTest { ...@@ -193,7 +193,7 @@ public class FakeCronetControllerTest {
@Test @Test
@SmallTest @SmallTest
public void testDefaultResponseIs404() { public void testDefaultResponseIs404() {
FakeUrlResponse foundResponse = mFakeCronetController.getResponse("url", null, null); FakeUrlResponse foundResponse = mFakeCronetController.getResponse("url", null, null, null);
assertEquals(404, foundResponse.getHttpStatusCode()); assertEquals(404, foundResponse.getHttpStatusCode());
} }
...@@ -205,7 +205,7 @@ public class FakeCronetControllerTest { ...@@ -205,7 +205,7 @@ public class FakeCronetControllerTest {
String location = "/TEST_REDIRECT_LOCATION"; String location = "/TEST_REDIRECT_LOCATION";
mFakeCronetController.addRedirectResponse(location, url); mFakeCronetController.addRedirectResponse(location, url);
FakeUrlResponse foundResponse = mFakeCronetController.getResponse("url", null, null); FakeUrlResponse foundResponse = mFakeCronetController.getResponse("url", null, null, null);
Map.Entry<String, String> headerEntry = new AbstractMap.SimpleEntry<>("location", location); Map.Entry<String, String> headerEntry = new AbstractMap.SimpleEntry<>("location", location);
assertTrue(foundResponse.getAllHeadersList().contains(headerEntry)); assertTrue(foundResponse.getAllHeadersList().contains(headerEntry));
...@@ -220,7 +220,7 @@ public class FakeCronetControllerTest { ...@@ -220,7 +220,7 @@ public class FakeCronetControllerTest {
int httpStatusCode = 400; int httpStatusCode = 400;
mFakeCronetController.addHttpErrorResponse(httpStatusCode, url); mFakeCronetController.addHttpErrorResponse(httpStatusCode, url);
FakeUrlResponse foundResponse = mFakeCronetController.getResponse(url, null, null); FakeUrlResponse foundResponse = mFakeCronetController.getResponse(url, null, null, null);
assertEquals(foundResponse.getHttpStatusCode(), httpStatusCode); assertEquals(foundResponse.getHttpStatusCode(), httpStatusCode);
} }
...@@ -245,7 +245,7 @@ public class FakeCronetControllerTest { ...@@ -245,7 +245,7 @@ public class FakeCronetControllerTest {
String body = "TEST_BODY"; String body = "TEST_BODY";
mFakeCronetController.addSuccessResponse(url, body.getBytes()); mFakeCronetController.addSuccessResponse(url, body.getBytes());
FakeUrlResponse foundResponse = mFakeCronetController.getResponse(url, null, null); FakeUrlResponse foundResponse = mFakeCronetController.getResponse(url, null, null, null);
assertTrue(foundResponse.getHttpStatusCode() >= 200); assertTrue(foundResponse.getHttpStatusCode() >= 200);
assertTrue(foundResponse.getHttpStatusCode() < 300); assertTrue(foundResponse.getHttpStatusCode() < 300);
......
...@@ -51,7 +51,7 @@ public class UrlResponseMatcherTest { ...@@ -51,7 +51,7 @@ public class UrlResponseMatcherTest {
new FakeUrlResponse.Builder().setResponseBody("TestBody".getBytes()).build(); new FakeUrlResponse.Builder().setResponseBody("TestBody".getBytes()).build();
ResponseMatcher matcher = new UrlResponseMatcher(url, response); ResponseMatcher matcher = new UrlResponseMatcher(url, response);
FakeUrlResponse found = matcher.getMatchingResponse(url, null, null); FakeUrlResponse found = matcher.getMatchingResponse(url, null, null, null);
assertNotNull(found); assertNotNull(found);
assertEquals(found, response); assertEquals(found, response);
...@@ -66,7 +66,8 @@ public class UrlResponseMatcherTest { ...@@ -66,7 +66,8 @@ public class UrlResponseMatcherTest {
new FakeUrlResponse.Builder().setResponseBody("TestBody".getBytes()).build(); new FakeUrlResponse.Builder().setResponseBody("TestBody".getBytes()).build();
ResponseMatcher matcher = new UrlResponseMatcher(url, response); ResponseMatcher matcher = new UrlResponseMatcher(url, response);
FakeUrlResponse notFound = matcher.getMatchingResponse(urlWithoutResponse, null, null); FakeUrlResponse notFound =
matcher.getMatchingResponse(urlWithoutResponse, null, null, null);
assertNull(notFound); assertNull(notFound);
} }
......
...@@ -16,22 +16,14 @@ import java.util.concurrent.atomic.AtomicBoolean; ...@@ -16,22 +16,14 @@ import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* An UploadDataProvider implementation used in tests. * An UploadDataProvider implementation used in tests.
*/ */
class TestUploadDataProvider extends UploadDataProvider { public class TestUploadDataProvider extends UploadDataProvider {
// Indicates whether all success callbacks are synchronous or asynchronous. // Indicates whether all success callbacks are synchronous or asynchronous.
// Doesn't apply to errors. // Doesn't apply to errors.
enum SuccessCallbackMode { public enum SuccessCallbackMode { SYNC, ASYNC }
SYNC,
ASYNC
};
// Indicates whether failures should throw exceptions, invoke callbacks // Indicates whether failures should throw exceptions, invoke callbacks
// synchronously, or invoke callback asynchronously. // synchronously, or invoke callback asynchronously.
enum FailMode { public enum FailMode { NONE, THROWN, CALLBACK_SYNC, CALLBACK_ASYNC }
NONE,
THROWN,
CALLBACK_SYNC,
CALLBACK_ASYNC
};
private ArrayList<byte[]> mReads = new ArrayList<byte[]>(); private ArrayList<byte[]> mReads = new ArrayList<byte[]>();
private final SuccessCallbackMode mSuccessCallbackMode; private final SuccessCallbackMode mSuccessCallbackMode;
...@@ -61,7 +53,8 @@ class TestUploadDataProvider extends UploadDataProvider { ...@@ -61,7 +53,8 @@ class TestUploadDataProvider extends UploadDataProvider {
private AtomicBoolean mClosed = new AtomicBoolean(false); private AtomicBoolean mClosed = new AtomicBoolean(false);
private ConditionVariable mAwaitingClose = new ConditionVariable(false); private ConditionVariable mAwaitingClose = new ConditionVariable(false);
TestUploadDataProvider(SuccessCallbackMode successCallbackMode, final Executor executor) { public TestUploadDataProvider(
SuccessCallbackMode successCallbackMode, final Executor executor) {
mSuccessCallbackMode = successCallbackMode; mSuccessCallbackMode = successCallbackMode;
mExecutor = executor; mExecutor = executor;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment