Bug 1881128 - Add additional headers for Google requests
[gecko.git] / mobile / android / fenix / app / src / test / java / org / mozilla / fenix / components / UrlRequestInterceptorTest.kt
blob61edbff0a1745e65c2f90679ac6f9480973eeac2
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.fenix.components
7 import io.mockk.mockk
8 import mozilla.components.concept.engine.EngineSession
9 import mozilla.components.concept.engine.EngineSession.LoadUrlFlags
10 import mozilla.components.concept.engine.EngineSession.LoadUrlFlags.Companion.ALLOW_ADDITIONAL_HEADERS
11 import mozilla.components.concept.engine.EngineSession.LoadUrlFlags.Companion.LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE
12 import mozilla.components.concept.engine.request.RequestInterceptor
13 import org.junit.Assert.assertEquals
14 import org.junit.Assert.assertFalse
15 import org.junit.Assert.assertNull
16 import org.junit.Assert.assertTrue
17 import org.junit.Before
18 import org.junit.Test
19 import org.junit.runner.RunWith
20 import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
22 @RunWith(FenixRobolectricTestRunner::class)
23 class UrlRequestInterceptorTest {
25     private lateinit var engineSession: EngineSession
27     @Before
28     fun setup() {
29         engineSession = mockk(relaxed = true)
30     }
32     @Test
33     fun `GIVEN device is above threshold WHEN get additional headers is called THEN return the correct map of additional headers`() {
34         val isDeviceRamAboveThreshold = true
35         val urlRequestInterceptor = getUrlRequestInterceptor(
36             isDeviceRamAboveThreshold = isDeviceRamAboveThreshold,
37         )
39         assertEquals(
40             mapOf("X-Search-Subdivision" to "1"),
41             urlRequestInterceptor.getAdditionalHeaders(isDeviceRamAboveThreshold),
42         )
43     }
45     @Test
46     fun `GIVEN device is not above threshold WHEN get additional headers is called THEN return the correct map of additional headers`() {
47         val isDeviceRamAboveThreshold = false
48         val urlRequestInterceptor = getUrlRequestInterceptor(
49             isDeviceRamAboveThreshold = isDeviceRamAboveThreshold,
50         )
52         assertEquals(
53             mapOf("X-Search-Subdivision" to "0"),
54             urlRequestInterceptor.getAdditionalHeaders(isDeviceRamAboveThreshold),
55         )
56     }
58     @Test
59     fun `WHEN should intercept request is called THEN return the correct boolean value`() {
60         val urlRequestInterceptor = getUrlRequestInterceptor()
62         assertTrue(
63             urlRequestInterceptor.shouldInterceptRequest(
64                 uri = "https://www.google.com",
65                 isSubframeRequest = false,
66             ),
67         )
68         assertTrue(
69             urlRequestInterceptor.shouldInterceptRequest(
70                 uri = "https://www.google.com/webhp",
71                 isSubframeRequest = false,
72             ),
73         )
74         assertTrue(
75             urlRequestInterceptor.shouldInterceptRequest(
76                 uri = "https://www.google.com/preferences",
77                 isSubframeRequest = false,
78             ),
79         )
80         assertTrue(
81             urlRequestInterceptor.shouldInterceptRequest(
82                 uri = "https://www.google.com/search?q=blue",
83                 isSubframeRequest = false,
84             ),
85         )
86         assertTrue(
87             urlRequestInterceptor.shouldInterceptRequest(
88                 uri = "https://www.google.ca/search?q=red",
89                 isSubframeRequest = false,
90             ),
91         )
92         assertTrue(
93             urlRequestInterceptor.shouldInterceptRequest(
94                 uri = "https://www.google.co.jp/search?q=red",
95                 isSubframeRequest = false,
96             ),
97         )
99         assertFalse(
100             urlRequestInterceptor.shouldInterceptRequest(
101                 uri = "https://getpocket.com",
102                 isSubframeRequest = false,
103             ),
104         )
105         assertFalse(
106             urlRequestInterceptor.shouldInterceptRequest(
107                 uri = "https://www.google.com/search?q=blue",
108                 isSubframeRequest = true,
109             ),
110         )
111         assertFalse(
112             urlRequestInterceptor.shouldInterceptRequest(
113                 uri = "https://www.google.com/recaptcha",
114                 isSubframeRequest = true,
115             ),
116         )
117     }
119     @Test
120     fun `WHEN a Pocket request is loaded THEN request is not intercepted`() {
121         val uri = "https://getpocket.com"
122         val response = getUrlRequestInterceptor().onLoadRequest(
123             uri = uri,
124         )
126         assertNull(response)
127     }
129     @Test
130     fun `WHEN a Google request is loaded THEN request is intercepted`() {
131         val uri = "https://www.google.com"
133         assertEquals(
134             RequestInterceptor.InterceptionResponse.Url(
135                 url = uri,
136                 flags = LoadUrlFlags.select(
137                     LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE,
138                     ALLOW_ADDITIONAL_HEADERS,
139                 ),
140                 additionalHeaders = mapOf(
141                     "X-Search-Subdivision" to "0",
142                 ),
143             ),
144             getUrlRequestInterceptor().onLoadRequest(
145                 uri = uri,
146             ),
147         )
148     }
150     @Test
151     fun `WHEN a Google search request is loaded THEN request is intercepted`() {
152         val uri = "https://www.google.com/search?q=blue"
154         assertEquals(
155             RequestInterceptor.InterceptionResponse.Url(
156                 url = uri,
157                 flags = LoadUrlFlags.select(
158                     LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE,
159                     ALLOW_ADDITIONAL_HEADERS,
160                 ),
161                 additionalHeaders = mapOf(
162                     "X-Search-Subdivision" to "1",
163                 ),
164             ),
165             getUrlRequestInterceptor(isDeviceRamAboveThreshold = true).onLoadRequest(
166                 uri = uri,
167             ),
168         )
170         assertEquals(
171             RequestInterceptor.InterceptionResponse.Url(
172                 url = uri,
173                 flags = LoadUrlFlags.select(
174                     LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE,
175                     ALLOW_ADDITIONAL_HEADERS,
176                 ),
177                 additionalHeaders = mapOf(
178                     "X-Search-Subdivision" to "0",
179                 ),
180             ),
181             getUrlRequestInterceptor(isDeviceRamAboveThreshold = false).onLoadRequest(
182                 uri = uri,
183             ),
184         )
185     }
187     @Test
188     fun `WHEN a Google search request with a ca TLD request is loaded THEN request is intercepted`() {
189         val uri = "https://www.google.ca/search?q=red"
191         assertEquals(
192             RequestInterceptor.InterceptionResponse.Url(
193                 url = uri,
194                 flags = LoadUrlFlags.select(
195                     LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE,
196                     ALLOW_ADDITIONAL_HEADERS,
197                 ),
198                 additionalHeaders = mapOf(
199                     "X-Search-Subdivision" to "1",
200                 ),
201             ),
202             getUrlRequestInterceptor(isDeviceRamAboveThreshold = true).onLoadRequest(
203                 uri = uri,
204             ),
205         )
207         assertEquals(
208             RequestInterceptor.InterceptionResponse.Url(
209                 url = uri,
210                 flags = LoadUrlFlags.select(
211                     LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE,
212                     ALLOW_ADDITIONAL_HEADERS,
213                 ),
214                 additionalHeaders = mapOf(
215                     "X-Search-Subdivision" to "0",
216                 ),
217             ),
218             getUrlRequestInterceptor(isDeviceRamAboveThreshold = false).onLoadRequest(
219                 uri = uri,
220             ),
221         )
222     }
224     @Test
225     fun `WHEN a Google subframe request is loaded THEN request is not intercepted`() {
226         val uri = "https://www.google.com/search?q=blue"
228         assertNull(
229             getUrlRequestInterceptor(isDeviceRamAboveThreshold = true).onLoadRequest(
230                 uri = uri,
231                 isSubframeRequest = true,
232             ),
233         )
234     }
236     private fun getUrlRequestInterceptor(isDeviceRamAboveThreshold: Boolean = false) =
237         UrlRequestInterceptor(
238             isDeviceRamAboveThreshold = isDeviceRamAboveThreshold,
239         )
241     private fun UrlRequestInterceptor.onLoadRequest(
242         uri: String,
243         lastUri: String? = null,
244         hasUserGesture: Boolean = false,
245         isSameDomain: Boolean = false,
246         isRedirect: Boolean = false,
247         isDirectNavigation: Boolean = false,
248         isSubframeRequest: Boolean = false,
249     ) = this.onLoadRequest(
250         engineSession = engineSession,
251         uri = uri,
252         lastUri = lastUri,
253         hasUserGesture = hasUserGesture,
254         isSameDomain = isSameDomain,
255         isRedirect = isRedirect,
256         isDirectNavigation = isDirectNavigation,
257         isSubframeRequest = isSubframeRequest,
258     )