• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.google.common.truth.Truth.assertThat;
8 
9 import org.jni_zero.JNINamespace;
10 import org.jni_zero.NativeMethods;
11 
12 import org.chromium.net.impl.CronetUrlRequestContext;
13 import org.chromium.net.test.FailurePhase;
14 
15 /** Helper class to set up url interceptors for testing purposes. */
16 @JNINamespace("cronet")
17 public final class MockUrlRequestJobFactory {
18     private final long mInterceptorHandle;
19     private final CronetTestUtil.NetworkThreadTestConnector mNetworkThreadTestConnector;
20 
21     /** Sets up URL interceptors. */
MockUrlRequestJobFactory(CronetEngine cronetEngine)22     public MockUrlRequestJobFactory(CronetEngine cronetEngine) {
23         mNetworkThreadTestConnector = new CronetTestUtil.NetworkThreadTestConnector(cronetEngine);
24 
25         mInterceptorHandle =
26                 MockUrlRequestJobFactoryJni.get()
27                         .addUrlInterceptors(
28                                 ((CronetUrlRequestContext) cronetEngine)
29                                         .getUrlRequestContextAdapter());
30     }
31 
32     /** Remove URL Interceptors. */
shutdown()33     public void shutdown() {
34         MockUrlRequestJobFactoryJni.get().removeUrlInterceptorJobFactory(mInterceptorHandle);
35         mNetworkThreadTestConnector.shutdown();
36     }
37 
38     /**
39      * Constructs a mock URL that hangs or fails at certain phase.
40      *
41      * @param phase at which request fails. It should be a value in
42      *              org.chromium.net.test.FailurePhase.
43      * @param netError reported by UrlRequestJob. Passing -1, results in hang.
44      */
getMockUrlWithFailure(int phase, int netError)45     public static String getMockUrlWithFailure(int phase, int netError) {
46         assertThat(netError).isLessThan(0);
47         switch (phase) {
48             case FailurePhase.START:
49             case FailurePhase.READ_SYNC:
50             case FailurePhase.READ_ASYNC:
51                 break;
52             default:
53                 throw new IllegalArgumentException(
54                         "phase not in org.chromium.net.test.FailurePhase");
55         }
56         return MockUrlRequestJobFactoryJni.get().getMockUrlWithFailure(phase, netError);
57     }
58 
59     /**
60      * Constructs a mock URL that synchronously responds with data repeated many
61      * times.
62      *
63      * @param data to return in response.
64      * @param dataRepeatCount number of times to repeat the data.
65      */
getMockUrlForData(String data, int dataRepeatCount)66     public static String getMockUrlForData(String data, int dataRepeatCount) {
67         return MockUrlRequestJobFactoryJni.get().getMockUrlForData(data, dataRepeatCount);
68     }
69 
70     /**
71      * Constructs a mock URL that will request client certificate and return
72      * the string "data" as the response.
73      */
getMockUrlForClientCertificateRequest()74     public static String getMockUrlForClientCertificateRequest() {
75         return MockUrlRequestJobFactoryJni.get().getMockUrlForClientCertificateRequest();
76     }
77 
78     /** Constructs a mock URL that will fail with an SSL certificate error. */
getMockUrlForSSLCertificateError()79     public static String getMockUrlForSSLCertificateError() {
80         return MockUrlRequestJobFactoryJni.get().getMockUrlForSSLCertificateError();
81     }
82 
83     /** Constructs a mock URL that will hang when try to read response body from the remote. */
getMockUrlForHangingRead()84     public static String getMockUrlForHangingRead() {
85         return MockUrlRequestJobFactoryJni.get().getMockUrlForHangingRead();
86     }
87 
88     @NativeMethods("cronet_tests")
89     interface Natives {
addUrlInterceptors(long requestContextAdapter)90         long addUrlInterceptors(long requestContextAdapter);
91 
removeUrlInterceptorJobFactory(long interceptorHandle)92         void removeUrlInterceptorJobFactory(long interceptorHandle);
93 
getMockUrlWithFailure(int phase, int netError)94         String getMockUrlWithFailure(int phase, int netError);
95 
getMockUrlForData(String data, int dataRepeatCount)96         String getMockUrlForData(String data, int dataRepeatCount);
97 
getMockUrlForClientCertificateRequest()98         String getMockUrlForClientCertificateRequest();
99 
getMockUrlForSSLCertificateError()100         String getMockUrlForSSLCertificateError();
101 
getMockUrlForHangingRead()102         String getMockUrlForHangingRead();
103     }
104 }
105