• 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;
6 
7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertFalse;
9 import static org.junit.Assert.assertTrue;
10 
11 import static org.chromium.net.CronetTestRule.SERVER_CERT_PEM;
12 import static org.chromium.net.CronetTestRule.SERVER_KEY_PKCS8_PEM;
13 import static org.chromium.net.CronetTestRule.getContext;
14 
15 import android.net.http.HttpEngine;
16 import android.net.http.ExperimentalHttpEngine;
17 import android.net.http.UrlRequest;
18 
19 import androidx.test.ext.junit.runners.AndroidJUnit4;
20 import androidx.test.filters.SmallTest;
21 
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Ignore;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 import org.chromium.net.CronetTestRule.OnlyRunNativeCronet;
30 import org.chromium.net.CronetTestRule.RequiresMinApi;
31 
32 /**
33  * Simple test for Brotli support.
34  */
35 @RunWith(AndroidJUnit4.class)
36 @RequiresMinApi(5) // Brotli support added in API version 5: crrev.com/465216
37 public class BrotliTest {
38     @Rule
39     public final CronetTestRule mTestRule = new CronetTestRule();
40 
41     private HttpEngine mCronetEngine;
42 
43     @Before
setUp()44     public void setUp() throws Exception {
45         TestFilesInstaller.installIfNeeded(getContext());
46         assertTrue(NativeTestServer.startNativeTestServer(getContext()));
47     }
48 
49     @After
tearDown()50     public void tearDown() throws Exception {
51         NativeTestServer.shutdownNativeTestServer();
52         if (mCronetEngine != null) {
53             mCronetEngine.shutdown();
54         }
55     }
56 
57     @Test
58     @SmallTest
59     @OnlyRunNativeCronet
testBrotliAdvertised()60     public void testBrotliAdvertised() throws Exception {
61         ExperimentalHttpEngine.Builder builder =
62                 new ExperimentalHttpEngine.Builder(getContext());
63         builder.setEnableBrotli(true);
64         mCronetEngine = builder.build();
65         String url = NativeTestServer.getEchoAllHeadersURL();
66         TestUrlRequestCallback callback = startAndWaitForComplete(url);
67         assertEquals(200, callback.mResponseInfo.getHttpStatusCode());
68         assertTrue(
69                 "[" + callback.mResponseAsString + "] should reference Brotli",
70                 callback.mResponseAsString.matches("(?is).*accept-encoding: gzip, deflate, br.*"));
71     }
72 
73     @Test
74     @SmallTest
75     @OnlyRunNativeCronet
testBrotliNotAdvertised()76     public void testBrotliNotAdvertised() throws Exception {
77         ExperimentalHttpEngine.Builder builder =
78                 new ExperimentalHttpEngine.Builder(getContext());
79         mCronetEngine = builder.build();
80         String url = NativeTestServer.getEchoAllHeadersURL();
81         TestUrlRequestCallback callback = startAndWaitForComplete(url);
82         assertEquals(200, callback.mResponseInfo.getHttpStatusCode());
83         assertFalse(callback.mResponseAsString.contains("br"));
84     }
85 
86     @Test
87     @SmallTest
88     @OnlyRunNativeCronet
89     @Ignore // TODO(danstahr): Add test server support for setting the Brotli header
testBrotliDecoded()90     public void testBrotliDecoded() throws Exception {
91         ExperimentalHttpEngine.Builder builder =
92                 new ExperimentalHttpEngine.Builder(getContext());
93         builder.setEnableBrotli(true);
94         mCronetEngine = builder.build();
95         String url = Http2TestServer.getServeSimpleBrotliResponse();
96         TestUrlRequestCallback callback = startAndWaitForComplete(url);
97         assertEquals(200, callback.mResponseInfo.getHttpStatusCode());
98         String expectedResponse = "The quick brown fox jumps over the lazy dog";
99         assertEquals(expectedResponse, callback.mResponseAsString);
100         assertEquals(
101                 callback.mResponseInfo.getHeaders().getAsMap().get("content-encoding").get(0),
102                 "br");
103     }
104 
startAndWaitForComplete(String url)105     private TestUrlRequestCallback startAndWaitForComplete(String url) {
106         TestUrlRequestCallback callback = new TestUrlRequestCallback();
107         UrlRequest.Builder builder =
108                 mCronetEngine.newUrlRequestBuilder(url, callback, callback.getExecutor());
109         builder.build().start();
110         callback.blockForDone();
111         return callback;
112     }
113 }
114