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.net; 6 7 import android.content.Context; 8 9 import org.chromium.base.annotations.JNINamespace; 10 import org.chromium.base.test.util.UrlUtils; 11 12 /** 13 * Wrapper class to start an in-process native test server, and get URLs 14 * needed to talk to it. 15 */ 16 @JNINamespace("cronet") 17 public final class NativeTestServer { 18 // This variable contains the response body of a request to getSuccessURL(). 19 public static final String SUCCESS_BODY = "this is a text file\n"; 20 startNativeTestServer(Context context)21 public static boolean startNativeTestServer(Context context) { 22 TestFilesInstaller.installIfNeeded(context); 23 return nativeStartNativeTestServer( 24 TestFilesInstaller.getInstalledPath(context), UrlUtils.getIsolatedTestRoot()); 25 } 26 shutdownNativeTestServer()27 public static void shutdownNativeTestServer() { 28 nativeShutdownNativeTestServer(); 29 } 30 getEchoBodyURL()31 public static String getEchoBodyURL() { 32 return nativeGetEchoBodyURL(); 33 } 34 getEchoHeaderURL(String header)35 public static String getEchoHeaderURL(String header) { 36 return nativeGetEchoHeaderURL(header); 37 } 38 getEchoAllHeadersURL()39 public static String getEchoAllHeadersURL() { 40 return nativeGetEchoAllHeadersURL(); 41 } 42 getEchoMethodURL()43 public static String getEchoMethodURL() { 44 return nativeGetEchoMethodURL(); 45 } 46 getRedirectToEchoBody()47 public static String getRedirectToEchoBody() { 48 return nativeGetRedirectToEchoBody(); 49 } 50 getFileURL(String filePath)51 public static String getFileURL(String filePath) { 52 return nativeGetFileURL(filePath); 53 } 54 55 // Returns a URL that the server will return an Exabyte of data getExabyteResponseURL()56 public static String getExabyteResponseURL() { 57 return nativeGetExabyteResponseURL(); 58 } 59 60 // The following URLs will make NativeTestServer serve a response based on 61 // the contents of the corresponding file and its mock-http-headers file. 62 getSuccessURL()63 public static String getSuccessURL() { 64 return nativeGetFileURL("/success.txt"); 65 } 66 getRedirectURL()67 public static String getRedirectURL() { 68 return nativeGetFileURL("/redirect.html"); 69 } 70 getMultiRedirectURL()71 public static String getMultiRedirectURL() { 72 return nativeGetFileURL("/multiredirect.html"); 73 } 74 getNotFoundURL()75 public static String getNotFoundURL() { 76 return nativeGetFileURL("/notfound.html"); 77 } 78 getPort()79 public static int getPort() { 80 return nativeGetPort(); 81 } 82 getHostPort()83 public static String getHostPort() { 84 return nativeGetHostPort(); 85 } 86 nativeStartNativeTestServer(String filePath, String testDataDir)87 private static native boolean nativeStartNativeTestServer(String filePath, String testDataDir); nativeShutdownNativeTestServer()88 private static native void nativeShutdownNativeTestServer(); nativeGetEchoBodyURL()89 private static native String nativeGetEchoBodyURL(); nativeGetEchoHeaderURL(String header)90 private static native String nativeGetEchoHeaderURL(String header); nativeGetEchoAllHeadersURL()91 private static native String nativeGetEchoAllHeadersURL(); nativeGetEchoMethodURL()92 private static native String nativeGetEchoMethodURL(); nativeGetRedirectToEchoBody()93 private static native String nativeGetRedirectToEchoBody(); nativeGetFileURL(String filePath)94 private static native String nativeGetFileURL(String filePath); nativeGetExabyteResponseURL()95 private static native String nativeGetExabyteResponseURL(); nativeGetHostPort()96 private static native String nativeGetHostPort(); nativeGetPort()97 private static native int nativeGetPort(); 98 } 99