xref: /aosp_15_r20/external/cronet/components/cronet/android/test/src/org/chromium/net/NativeTestServer.java (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
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.jni_zero.JNINamespace;
10 import org.jni_zero.NativeMethods;
11 
12 import org.chromium.base.test.util.UrlUtils;
13 import org.chromium.net.test.ServerCertificate;
14 
15 /**
16  * Wrapper class to start an in-process native test server, and get URLs needed to talk to it.
17  *
18  * <p>NativeTestServer only supports HTTP/1.
19  */
20 @JNINamespace("cronet")
21 public final class NativeTestServer {
22     // This variable contains the response body of a request to getSuccessURL().
23     public static final String SUCCESS_BODY = "this is a text file\n";
24 
startNativeTestServer(Context context)25     public static boolean startNativeTestServer(Context context) {
26         TestFilesInstaller.installIfNeeded(context);
27         return NativeTestServerJni.get().startNativeTestServer(
28                 TestFilesInstaller.getInstalledPath(context),
29             UrlUtils.getIsolatedTestRoot(),
30             false, // useHttps
31             ServerCertificate.CERT_OK);
32     }
33 
startNativeTestServerWithHTTPS( Context context, @ServerCertificate int serverCertificate)34     public static boolean startNativeTestServerWithHTTPS(
35             Context context, @ServerCertificate int serverCertificate) {
36         TestFilesInstaller.installIfNeeded(context);
37         return NativeTestServerJni.get()
38                 .startNativeTestServer(
39                         TestFilesInstaller.getInstalledPath(context),
40                         UrlUtils.getIsolatedTestRoot(),
41                         true, // useHttps
42                         serverCertificate);
43     }
44 
shutdownNativeTestServer()45     public static void shutdownNativeTestServer() {
46         NativeTestServerJni.get().shutdownNativeTestServer();
47     }
48 
getEchoBodyURL()49     public static String getEchoBodyURL() {
50         return NativeTestServerJni.get().getEchoBodyURL();
51     }
52 
getEchoHeaderURL(String header)53     public static String getEchoHeaderURL(String header) {
54         return NativeTestServerJni.get().getEchoHeaderURL(header);
55     }
56 
getEchoAllHeadersURL()57     public static String getEchoAllHeadersURL() {
58         return NativeTestServerJni.get().getEchoAllHeadersURL();
59     }
60 
getEchoMethodURL()61     public static String getEchoMethodURL() {
62         return NativeTestServerJni.get().getEchoMethodURL();
63     }
64 
getRedirectToEchoBody()65     public static String getRedirectToEchoBody() {
66         return NativeTestServerJni.get().getRedirectToEchoBody();
67     }
68 
getFileURL(String filePath)69     public static String getFileURL(String filePath) {
70         return NativeTestServerJni.get().getFileURL(filePath);
71     }
72 
73     // Returns a URL that the server will return an Exabyte of data
getExabyteResponseURL()74     public static String getExabyteResponseURL() {
75         return NativeTestServerJni.get().getExabyteResponseURL();
76     }
77 
78     // The following URLs will make NativeTestServer serve a response based on
79     // the contents of the corresponding file and its mock-http-headers file.
80 
getSuccessURL()81     public static String getSuccessURL() {
82         return NativeTestServerJni.get().getFileURL("/success.txt");
83     }
84 
getRedirectURL()85     public static String getRedirectURL() {
86         return NativeTestServerJni.get().getFileURL("/redirect.html");
87     }
88 
getMultiRedirectURL()89     public static String getMultiRedirectURL() {
90         return NativeTestServerJni.get().getFileURL("/multiredirect.html");
91     }
92 
getNotFoundURL()93     public static String getNotFoundURL() {
94         return NativeTestServerJni.get().getFileURL("/notfound.html");
95     }
96 
getServerErrorURL()97     public static String getServerErrorURL() {
98         return NativeTestServerJni.get().getFileURL("/server_error.txt");
99     }
100 
getPort()101     public static int getPort() {
102         return NativeTestServerJni.get().getPort();
103     }
104 
getHostPort()105     public static String getHostPort() {
106         return NativeTestServerJni.get().getHostPort();
107     }
108 
109     @NativeMethods("cronet_tests")
110     interface Natives {
startNativeTestServer( String filePath, String testDataDir, boolean useHttps, @ServerCertificate int certificate)111         boolean startNativeTestServer(
112                 String filePath,
113                 String testDataDir,
114                 boolean useHttps,
115                 @ServerCertificate int certificate);
116 
shutdownNativeTestServer()117         void shutdownNativeTestServer();
118 
getEchoBodyURL()119         String getEchoBodyURL();
120 
getEchoHeaderURL(String header)121         String getEchoHeaderURL(String header);
122 
getEchoAllHeadersURL()123         String getEchoAllHeadersURL();
124 
getEchoMethodURL()125         String getEchoMethodURL();
126 
getRedirectToEchoBody()127         String getRedirectToEchoBody();
128 
getFileURL(String filePath)129         String getFileURL(String filePath);
130 
getExabyteResponseURL()131         String getExabyteResponseURL();
132 
getHostPort()133         String getHostPort();
134 
getPort()135         int getPort();
136     }
137 }
138