Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / cronet / android / test / javatests / src / org / chromium / net / CronetUrlTest.java
blob945ec7fd340f0337bab192de0aeb889ae280ef87
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.net;
7 import android.content.Context;
8 import android.test.suitebuilder.annotation.SmallTest;
10 import org.chromium.base.PathUtils;
11 import org.chromium.base.test.util.Feature;
13 import java.io.File;
14 import java.util.HashMap;
16 /**
17 * Example test that just starts the cronet sample.
19 public class CronetUrlTest extends CronetTestBase {
20 // URL used for base tests.
21 private static final String URL = "http://127.0.0.1:8000";
23 @SmallTest
24 @Feature({"Cronet"})
25 public void testLoadUrl() throws Exception {
26 CronetTestActivity activity = launchCronetTestAppWithUrl(URL);
28 // Make sure that the URL is set as expected.
29 assertEquals(URL, activity.getUrl());
30 assertEquals(200, activity.getHttpStatusCode());
33 @SmallTest
34 @Feature({"Cronet"})
35 public void testInvalidUrl() throws Exception {
36 CronetTestActivity activity = launchCronetTestAppWithUrl(
37 "127.0.0.1:8000");
39 // The load should fail.
40 assertEquals(0, activity.getHttpStatusCode());
43 @SmallTest
44 @Feature({"Cronet"})
45 public void testPostData() throws Exception {
46 String[] commandLineArgs = {
47 CronetTestActivity.POST_DATA_KEY, "test" };
48 CronetTestActivity activity =
49 launchCronetTestAppWithUrlAndCommandLineArgs(URL,
50 commandLineArgs);
52 // Make sure that the URL is set as expected.
53 assertEquals(URL, activity.getUrl());
54 assertEquals(200, activity.getHttpStatusCode());
57 @SmallTest
58 @Feature({"Cronet"})
59 public void testNetLog() throws Exception {
60 Context context = getInstrumentation().getTargetContext();
61 File directory = new File(PathUtils.getDataDirectory(context));
62 File file = File.createTempFile("cronet", "json", directory);
63 HttpUrlRequestFactory factory = HttpUrlRequestFactory.createFactory(
64 context,
65 new UrlRequestContextConfig().setLibraryName("cronet_tests"));
66 // Start NetLog immediately after the request context is created to make
67 // sure that the call won't crash the app even when the native request
68 // context is not fully initialized. See crbug.com/470196.
69 factory.startNetLogToFile(file.getPath(), false);
70 // Starts a request.
71 HashMap<String, String> headers = new HashMap<String, String>();
72 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener();
73 HttpUrlRequest request = factory.createRequest(
74 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
75 request.start();
76 listener.blockForComplete();
77 factory.stopNetLog();
78 assertTrue(file.exists());
79 assertTrue(file.length() != 0);
80 assertTrue(file.delete());
81 assertTrue(!file.exists());
84 static class BadHttpUrlRequestListener extends TestHttpUrlRequestListener {
85 static final String THROW_TAG = "BadListener";
87 public BadHttpUrlRequestListener() {
90 @Override
91 public void onResponseStarted(HttpUrlRequest request) {
92 throw new NullPointerException(THROW_TAG);
96 @SmallTest
97 @Feature({"Cronet"})
98 public void testCalledByNativeException() throws Exception {
99 CronetTestActivity activity = launchCronetTestAppWithUrl(URL);
101 HashMap<String, String> headers = new HashMap<String, String>();
102 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener();
104 // Create request with bad listener to trigger an exception.
105 HttpUrlRequest request = activity.mRequestFactory.createRequest(
106 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
107 request.start();
108 listener.blockForComplete();
109 assertTrue(request.isCanceled());
110 assertNotNull(request.getException());
111 assertEquals(BadHttpUrlRequestListener.THROW_TAG,
112 request.getException().getCause().getMessage());
115 @SmallTest
116 @Feature({"Cronet"})
117 public void testSetUploadDataWithNullContentType() throws Exception {
118 CronetTestActivity activity = launchCronetTestAppWithUrl(URL);
120 HashMap<String, String> headers = new HashMap<String, String>();
121 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener();
123 // Create request.
124 HttpUrlRequest request = activity.mRequestFactory.createRequest(
125 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
126 byte[] uploadData = new byte[] {1, 2, 3};
127 try {
128 request.setUploadData(null, uploadData);
129 fail("setUploadData should throw on null content type");
130 } catch (NullPointerException e) {
131 // Nothing to do here.
135 @SmallTest
136 @Feature({"Cronet"})
137 public void testLegacyLoadUrl() throws Exception {
138 HttpUrlRequestFactoryConfig config = new HttpUrlRequestFactoryConfig();
139 config.enableLegacyMode(true);
140 // TODO(mef) fix tests so that library isn't loaded for legacy stack
141 config.setLibraryName("cronet_tests");
143 String[] commandLineArgs = {
144 CronetTestActivity.CONFIG_KEY, config.toString() };
145 CronetTestActivity activity =
146 launchCronetTestAppWithUrlAndCommandLineArgs(URL,
147 commandLineArgs);
149 // Make sure that the URL is set as expected.
150 assertEquals(URL, activity.getUrl());
151 assertEquals(200, activity.getHttpStatusCode());
154 @SmallTest
155 @Feature({"Cronet"})
156 public void testRequestHead() throws Exception {
157 CronetTestActivity activity = launchCronetTestAppWithUrl(URL);
159 HashMap<String, String> headers = new HashMap<String, String>();
160 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener();
162 // Create request.
163 HttpUrlRequest request = activity.mRequestFactory.createRequest(
164 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
165 request.setHttpMethod("HEAD");
166 request.start();
167 listener.blockForComplete();
168 assertEquals(200, listener.mHttpStatusCode);
169 // HEAD requests do not get any response data and Content-Length must be
170 // ignored.
171 assertEquals(0, listener.mResponseAsBytes.length);