• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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.test;
6 
7 import android.content.Context;
8 
9 import androidx.test.InstrumentationRegistry;
10 
11 import org.junit.rules.TestRule;
12 import org.junit.runner.Description;
13 import org.junit.runners.model.Statement;
14 
15 /**
16  * Junit4 rule for starting embedded test server when necessary (i.e. when accessed via
17  * {@link #getServer()}), and shutting it down when the test finishes.
18  */
19 public class EmbeddedTestServerRule implements TestRule {
20     private EmbeddedTestServer mServer;
21 
22     // The default value of 0 will result in the same behavior as createAndStartServer
23     // (auto-selected port).
24     private int mServerPort;
25 
26     private boolean mUseHttps;
27 
28     @ServerCertificate private int mCertificateType = ServerCertificate.CERT_OK;
29 
30     @Override
apply(Statement base, Description description)31     public Statement apply(Statement base, Description description) {
32         return new Statement() {
33             @Override
34             public void evaluate() throws Throwable {
35                 try {
36                     base.evaluate();
37                 } finally {
38                     if (mServer != null) mServer.stopAndDestroyServer();
39                 }
40             }
41         };
42     }
43 
44     /**
45      * Get the test server, creating and starting it if it doesn't exist yet.
46      *
47      * @return the test server.
48      */
49     public EmbeddedTestServer getServer() {
50         if (mServer == null) {
51             Context context = InstrumentationRegistry.getContext();
52             // Need to disable ResettersForTesting because it will destroy the server too early in
53             // the case where this rule is initialized via @ClassRule and getServer() is not called
54             // until one of the tests is executing.
55             mServer = new EmbeddedTestServer();
56             mServer.mDisableResetterForTesting = true;
57             if (mUseHttps) {
58                 EmbeddedTestServer.initializeAndStartHTTPSServer(
59                         mServer, context, mCertificateType, mServerPort);
60             } else {
61                 EmbeddedTestServer.initializeAndStartServer(mServer, context, mServerPort);
62             }
63         }
64         return mServer;
65     }
66 
67     public String getOrigin() {
68         return getServer().getURL("/");
69     }
70 
71     /**
72      * Sets the port that the server will be started with. Must be called before the first
73      * {@link #getServer()} call.
74      *
75      * @param port the port to start the server with, or 0 for an automatically selected one.
76      */
77     public void setServerPort(int port) {
78         assert mServer == null;
79         mServerPort = port;
80     }
81 
82     /** Sets whether to create an HTTPS (vs HTTP) server. */
83     public void setServerUsesHttps(boolean useHttps) {
84         assert mServer == null;
85         mUseHttps = useHttps;
86     }
87 
88     /** Sets what type of certificate the server uses when running as an HTTPS server. */
89     public void setCertificateType(@ServerCertificate int certificateType) {
90         assert mServer == null;
91         mCertificateType = certificateType;
92     }
93 }
94