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
;
14 import java
.util
.HashMap
;
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";
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());
35 public void testInvalidUrl() throws Exception
{
36 CronetTestActivity activity
= launchCronetTestAppWithUrl(
39 // The load should fail.
40 assertEquals(0, activity
.getHttpStatusCode());
45 public void testPostData() throws Exception
{
46 String
[] commandLineArgs
= {
47 CronetTestActivity
.POST_DATA_KEY
, "test" };
48 CronetTestActivity activity
=
49 launchCronetTestAppWithUrlAndCommandLineArgs(URL
,
52 // Make sure that the URL is set as expected.
53 assertEquals(URL
, activity
.getUrl());
54 assertEquals(200, activity
.getHttpStatusCode());
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(
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);
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
);
76 listener
.blockForComplete();
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() {
91 public void onResponseStarted(HttpUrlRequest request
) {
92 throw new NullPointerException(THROW_TAG
);
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
);
108 listener
.blockForComplete();
109 assertTrue(request
.isCanceled());
110 assertNotNull(request
.getException());
111 assertEquals(BadHttpUrlRequestListener
.THROW_TAG
,
112 request
.getException().getCause().getMessage());
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();
124 HttpUrlRequest request
= activity
.mRequestFactory
.createRequest(
125 URL
, HttpUrlRequest
.REQUEST_PRIORITY_MEDIUM
, headers
, listener
);
126 byte[] uploadData
= new byte[] {1, 2, 3};
128 request
.setUploadData(null, uploadData
);
129 fail("setUploadData should throw on null content type");
130 } catch (NullPointerException e
) {
131 // Nothing to do here.
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
,
149 // Make sure that the URL is set as expected.
150 assertEquals(URL
, activity
.getUrl());
151 assertEquals(200, activity
.getHttpStatusCode());
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();
163 HttpUrlRequest request
= activity
.mRequestFactory
.createRequest(
164 URL
, HttpUrlRequest
.REQUEST_PRIORITY_MEDIUM
, headers
, listener
);
165 request
.setHttpMethod("HEAD");
167 listener
.blockForComplete();
168 assertEquals(200, listener
.mHttpStatusCode
);
169 // HEAD requests do not get any response data and Content-Length must be
171 assertEquals(0, listener
.mResponseAsBytes
.length
);