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. 4 5 package org.chromium.cronet_test_apk; 6 7 import android.os.ConditionVariable; 8 import android.test.suitebuilder.annotation.SmallTest; 9 import android.util.Log; 10 11 import org.chromium.base.JNINamespace; 12 import org.chromium.base.test.util.Feature; 13 import org.chromium.net.HttpUrlRequest; 14 import org.chromium.net.HttpUrlRequestListener; 15 16 import java.util.HashMap; 17 18 // Tests that use mock URLRequestJobs to simulate URL requests. 19 @JNINamespace("cronet") 20 public class MockUrlRequestJobTest extends CronetTestBase { 21 private static final String TAG = "MockURLRequestJobTest"; 22 private static final String MOCK_CRONET_TEST_SUCCESS_URL = 23 "http://mock.http/success.txt"; 24 private static final String MOCK_CRONET_TEST_REDIRECT_URL = 25 "http://mock.http/redirect.html"; 26 private static final String MOCK_CRONET_TEST_NOTFOUND_URL = 27 "http://mock.http/notfound.html"; 28 private static final String MOCK_CRONET_TEST_FAILED_URL = 29 "http://mock.failed.request/-2"; 30 31 class MockHttpUrlRequestListener implements HttpUrlRequestListener { 32 ConditionVariable mComplete = new ConditionVariable(); 33 public int mHttpStatusCode = 0; 34 public String mUrl; 35 public byte[] mResponseAsBytes; 36 37 public MockHttpUrlRequestListener() { 38 } 39 40 @Override 41 public void onResponseStarted(HttpUrlRequest request) { 42 Log.i(TAG, "****** Response Started, content length is " + 43 request.getContentLength()); 44 Log.i(TAG, "*** Headers Are *** " + request.getAllHeaders()); 45 mHttpStatusCode = request.getHttpStatusCode(); 46 } 47 48 public void blockForComplete() { 49 mComplete.block(); 50 } 51 52 @Override 53 public void onRequestComplete(HttpUrlRequest request) { 54 mUrl = request.getUrl(); 55 mResponseAsBytes = request.getResponseAsBytes(); 56 mComplete.open(); 57 Log.i(TAG, "****** Request Complete, status code is " + 58 request.getHttpStatusCode()); 59 } 60 } 61 62 // Helper function to create a HttpUrlRequest with the specified url. 63 private MockHttpUrlRequestListener createUrlRequestAndWaitForComplete( 64 String url) { 65 CronetTestActivity activity = launchCronetTestApp(); 66 assertNotNull(activity); 67 // AddUrlInterceptors() after native application context is initialized. 68 nativeAddUrlInterceptors(); 69 70 HashMap<String, String> headers = new HashMap<String, String>(); 71 MockHttpUrlRequestListener listener = new MockHttpUrlRequestListener(); 72 73 HttpUrlRequest request = activity.mChromiumRequestFactory.createRequest( 74 url, 75 HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, 76 headers, 77 listener); 78 request.start(); 79 listener.blockForComplete(); 80 return listener; 81 } 82 83 @SmallTest 84 @Feature({"Cronet"}) 85 public void testSuccessURLRequest() throws Exception { 86 MockHttpUrlRequestListener listener = 87 createUrlRequestAndWaitForComplete(MOCK_CRONET_TEST_SUCCESS_URL); 88 assertEquals(MOCK_CRONET_TEST_SUCCESS_URL, listener.mUrl); 89 assertEquals(200, listener.mHttpStatusCode); 90 assertEquals("this is a text file\n", 91 new String(listener.mResponseAsBytes)); 92 } 93 94 @SmallTest 95 @Feature({"Cronet"}) 96 public void testRedirectURLRequest() throws Exception { 97 MockHttpUrlRequestListener listener = 98 createUrlRequestAndWaitForComplete(MOCK_CRONET_TEST_REDIRECT_URL); 99 100 // Currently Cronet does not expose the url after redirect. 101 assertEquals(MOCK_CRONET_TEST_REDIRECT_URL, listener.mUrl); 102 assertEquals(200, listener.mHttpStatusCode); 103 // Expect that the request is redirected to success.txt. 104 assertEquals("this is a text file\n", 105 new String(listener.mResponseAsBytes)); 106 } 107 108 @SmallTest 109 @Feature({"Cronet"}) 110 public void testNotFoundURLRequest() throws Exception { 111 MockHttpUrlRequestListener listener = 112 createUrlRequestAndWaitForComplete(MOCK_CRONET_TEST_NOTFOUND_URL); 113 assertEquals(MOCK_CRONET_TEST_NOTFOUND_URL, listener.mUrl); 114 assertEquals(404, listener.mHttpStatusCode); 115 assertEquals( 116 "<!DOCTYPE html>\n<html>\n<head>\n<title>Not found</title>\n" + 117 "<p>Test page loaded.</p>\n</head>\n</html>\n", 118 new String(listener.mResponseAsBytes)); 119 } 120 121 @SmallTest 122 @Feature({"Cronet"}) 123 public void testFailedURLRequest() throws Exception { 124 MockHttpUrlRequestListener listener = 125 createUrlRequestAndWaitForComplete(MOCK_CRONET_TEST_FAILED_URL); 126 assertEquals(MOCK_CRONET_TEST_FAILED_URL, listener.mUrl); 127 assertEquals(0, listener.mHttpStatusCode); 128 } 129 130 private native void nativeAddUrlInterceptors(); 131 } 132