Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(956)

Unified Diff: components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/urlconnection/CronetHttpURLConnectionTest.java

Issue 725683002: [Cronet] Initial implementation of HttpURLConnection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Matt's comments Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/urlconnection/CronetHttpURLConnectionTest.java
diff --git a/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/urlconnection/CronetHttpURLConnectionTest.java b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/urlconnection/CronetHttpURLConnectionTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..e9557e91728a192dc957aebfbb95750ee0b41b3d
--- /dev/null
+++ b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/urlconnection/CronetHttpURLConnectionTest.java
@@ -0,0 +1,188 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.cronet_test_apk.urlconnection;
+
+import android.test.suitebuilder.annotation.SmallTest;
+
+import org.chromium.base.test.util.Feature;
+import org.chromium.cronet_test_apk.CronetTestActivity;
+import org.chromium.cronet_test_apk.CronetTestBase;
+import org.chromium.cronet_test_apk.UploadTestServer;
+import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Example test that just starts the cronet sample.
mmenke 2014/11/21 16:53:07 nit: capitalize Cronet?
xunjieli 2014/11/21 20:30:56 Done. Just realize that this is a stale comment.
+ */
+public class CronetHttpURLConnectionTest extends CronetTestBase {
mmenke 2014/11/21 16:53:07 Do instrumentation tests support parameterizing te
xunjieli 2014/11/21 20:30:56 I did some search, I think the standard way is to
+ private CronetTestActivity mActivity;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mActivity = launchCronetTestApp();
+ waitForActiveShellToBeDoneLoading();
+ assertTrue(UploadTestServer.startUploadTestServer());
+ // Use Cronet's implementation of HttpURLConnection.
+ CronetURLStreamHandlerFactory factory =
+ new CronetURLStreamHandlerFactory(
+ mActivity.getApplicationContext());
+ URL.setURLStreamHandlerFactory(factory);
+ }
+
+ @Override
+ protected void tearDown() {
+ UploadTestServer.shutdownUploadTestServer();
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ public void testBasicGet() {
+ try {
+ URL url = new URL(UploadTestServer.getEchoMethodURL());
+ HttpURLConnection urlConnection =
+ (HttpURLConnection) url.openConnection();
+ assertEquals(200, urlConnection.getResponseCode());
mmenke 2014/11/21 16:53:07 Should have a non-2xx response test, too, with a b
xunjieli 2014/11/21 20:30:56 Done. Did a 404 response. But I used MockUrlReques
+ assertEquals("GET\n", getResponseAsString(urlConnection));
+ urlConnection.disconnect();
+ } catch (Exception e) {
mmenke 2014/11/21 16:53:07 Why are these needed? Mother other tests just let
xunjieli 2014/11/21 20:30:56 Done. Yes, you are right.
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ public void testAddRequestProperty() {
+ try {
+ URL url = new URL(UploadTestServer.getEchoAllHeadersURL());
+ HttpURLConnection urlConnection =
+ (HttpURLConnection) url.openConnection();
+ urlConnection.addRequestProperty("header1", "foo");
+ urlConnection.addRequestProperty("header2", "bar");
mmenke 2014/11/21 16:53:07 optional: Using foo-header and bar-header instead
xunjieli 2014/11/21 20:30:56 Done.
+ assertEquals(200, urlConnection.getResponseCode());
+ String headers = getResponseAsString(urlConnection);
+ List<String> header1Values =
+ getRequestHeaderValues(headers, "header1");
+ List<String> header2Values =
+ getRequestHeaderValues(headers, "header2");
+ assertEquals(1, header1Values.size());
+ assertEquals("foo", header1Values.get(0));
+ assertEquals(1, header1Values.size());
+ assertEquals("bar", header2Values.get(0));
+ urlConnection.disconnect();
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ public void testAddRequestPropertyWithSameKey() {
+ try {
+ URL url = new URL(UploadTestServer.getEchoAllHeadersURL());
+ HttpURLConnection urlConnection =
+ (HttpURLConnection) url.openConnection();
+ urlConnection.addRequestProperty("header-name", "value1");
+ urlConnection.addRequestProperty("header-name", "value2");
+ assertEquals(200, urlConnection.getResponseCode());
+ String headers = getResponseAsString(urlConnection);
+ List<String> actualValues =
+ getRequestHeaderValues(headers, "header-name");
+ // TODO(xunjieli): Currently Cronet only sends the last header value
+ // if there are multiple entries with the same header key, see
+ // crbug/432719. Fix this test once the bug is fixed.
+ assertEquals(1, actualValues.size());
+ assertEquals("value2", actualValues.get(0));
+ urlConnection.disconnect();
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ public void testSetRequestProperty() {
+ try {
+ URL url = new URL(UploadTestServer.getEchoAllHeadersURL());
+ HttpURLConnection urlConnection =
+ (HttpURLConnection) url.openConnection();
+ urlConnection.setRequestProperty("header-name", "header-value");
+ assertEquals(200, urlConnection.getResponseCode());
+ String headers = getResponseAsString(urlConnection);
+ List<String> actualValues =
+ getRequestHeaderValues(headers, "header-name");
+ assertEquals(1, actualValues.size());
+ assertEquals("header-value", actualValues.get(0));
+ urlConnection.disconnect();
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ public void testAddAndSetRequestProperty() {
+ try {
+ URL url = new URL(UploadTestServer.getEchoAllHeadersURL());
+ HttpURLConnection urlConnection =
+ (HttpURLConnection) url.openConnection();
+ urlConnection.addRequestProperty("header-name", "value1");
+ urlConnection.setRequestProperty("header-name", "value2");
+ assertEquals(200, urlConnection.getResponseCode());
+ String headers = getResponseAsString(urlConnection);
+ List<String> actualValues =
+ getRequestHeaderValues(headers, "header-name");
+ assertEquals(1, actualValues.size());
+ assertEquals("value2", actualValues.get(0));
+ urlConnection.disconnect();
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
mmenke 2014/11/21 16:53:07 Other test suggestion: It's an issue with out oth
mmenke 2014/11/21 16:53:07 Other test suggestion: connect -> disconnect -> t
xunjieli 2014/11/21 20:30:56 Done. Currently the default implementation throws
xunjieli 2014/11/21 20:30:56 What do you mean by multiple body reads? read the
mmenke 2014/11/21 21:26:12 I was thinking multiple reads at the CronetURLRequ
+
+ /**
+ * Helper method to extract response body as a string for testing.
+ */
+ private String getResponseAsString(HttpURLConnection connection)
+ throws Exception {
+ InputStream in = connection.getInputStream();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+ StringBuilder out = new StringBuilder();
+ String line;
+ while ((line = reader.readLine()) != null) {
+ out.append(line + "\n");
mmenke 2014/11/21 16:53:07 Why append a line break here?
xunjieli 2014/11/21 20:30:56 so reader.readLine() returns me a line without the
mmenke 2014/11/21 21:26:12 Ahh...Suggest just reading the data directly, so w
+ }
+ return out.toString();
+ }
+
+ /**
+ * Helper method to extract a list of header values with the give header
+ * name.
+ */
+ private List<String> getRequestHeaderValues(String allHeaders,
+ String headerName) {
+ Pattern pattern = Pattern.compile(headerName + ":\\s(.*)\\n");
+ Matcher matcher = pattern.matcher(allHeaders);
+ List<String> headerValues = new ArrayList<String>();
+ while (matcher.find()) {
+ headerValues.add(matcher.group(1));
+ }
+ return headerValues;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698