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
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
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
29 engineSession = mockk(relaxed = true)
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,
40 mapOf("X-Search-Subdivision" to "1"),
41 urlRequestInterceptor.getAdditionalHeaders(isDeviceRamAboveThreshold),
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,
53 mapOf("X-Search-Subdivision" to "0"),
54 urlRequestInterceptor.getAdditionalHeaders(isDeviceRamAboveThreshold),
59 fun `WHEN should intercept request is called THEN return the correct boolean value`() {
60 val urlRequestInterceptor = getUrlRequestInterceptor()
63 urlRequestInterceptor.shouldInterceptRequest(
64 uri = "https://www.google.com",
65 isSubframeRequest = false,
69 urlRequestInterceptor.shouldInterceptRequest(
70 uri = "https://www.google.com/webhp",
71 isSubframeRequest = false,
75 urlRequestInterceptor.shouldInterceptRequest(
76 uri = "https://www.google.com/preferences",
77 isSubframeRequest = false,
81 urlRequestInterceptor.shouldInterceptRequest(
82 uri = "https://www.google.com/search?q=blue",
83 isSubframeRequest = false,
87 urlRequestInterceptor.shouldInterceptRequest(
88 uri = "https://www.google.ca/search?q=red",
89 isSubframeRequest = false,
93 urlRequestInterceptor.shouldInterceptRequest(
94 uri = "https://www.google.co.jp/search?q=red",
95 isSubframeRequest = false,
100 urlRequestInterceptor.shouldInterceptRequest(
101 uri = "https://getpocket.com",
102 isSubframeRequest = false,
106 urlRequestInterceptor.shouldInterceptRequest(
107 uri = "https://www.google.com/search?q=blue",
108 isSubframeRequest = true,
112 urlRequestInterceptor.shouldInterceptRequest(
113 uri = "https://www.google.com/recaptcha",
114 isSubframeRequest = true,
120 fun `WHEN a Pocket request is loaded THEN request is not intercepted`() {
121 val uri = "https://getpocket.com"
122 val response = getUrlRequestInterceptor().onLoadRequest(
130 fun `WHEN a Google request is loaded THEN request is intercepted`() {
131 val uri = "https://www.google.com"
134 RequestInterceptor.InterceptionResponse.Url(
136 flags = LoadUrlFlags.select(
137 LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE,
138 ALLOW_ADDITIONAL_HEADERS,
140 additionalHeaders = mapOf(
141 "X-Search-Subdivision" to "0",
144 getUrlRequestInterceptor().onLoadRequest(
151 fun `WHEN a Google search request is loaded THEN request is intercepted`() {
152 val uri = "https://www.google.com/search?q=blue"
155 RequestInterceptor.InterceptionResponse.Url(
157 flags = LoadUrlFlags.select(
158 LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE,
159 ALLOW_ADDITIONAL_HEADERS,
161 additionalHeaders = mapOf(
162 "X-Search-Subdivision" to "1",
165 getUrlRequestInterceptor(isDeviceRamAboveThreshold = true).onLoadRequest(
171 RequestInterceptor.InterceptionResponse.Url(
173 flags = LoadUrlFlags.select(
174 LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE,
175 ALLOW_ADDITIONAL_HEADERS,
177 additionalHeaders = mapOf(
178 "X-Search-Subdivision" to "0",
181 getUrlRequestInterceptor(isDeviceRamAboveThreshold = false).onLoadRequest(
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"
192 RequestInterceptor.InterceptionResponse.Url(
194 flags = LoadUrlFlags.select(
195 LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE,
196 ALLOW_ADDITIONAL_HEADERS,
198 additionalHeaders = mapOf(
199 "X-Search-Subdivision" to "1",
202 getUrlRequestInterceptor(isDeviceRamAboveThreshold = true).onLoadRequest(
208 RequestInterceptor.InterceptionResponse.Url(
210 flags = LoadUrlFlags.select(
211 LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE,
212 ALLOW_ADDITIONAL_HEADERS,
214 additionalHeaders = mapOf(
215 "X-Search-Subdivision" to "0",
218 getUrlRequestInterceptor(isDeviceRamAboveThreshold = false).onLoadRequest(
225 fun `WHEN a Google subframe request is loaded THEN request is not intercepted`() {
226 val uri = "https://www.google.com/search?q=blue"
229 getUrlRequestInterceptor(isDeviceRamAboveThreshold = true).onLoadRequest(
231 isSubframeRequest = true,
236 private fun getUrlRequestInterceptor(isDeviceRamAboveThreshold: Boolean = false) =
237 UrlRequestInterceptor(
238 isDeviceRamAboveThreshold = isDeviceRamAboveThreshold,
241 private fun UrlRequestInterceptor.onLoadRequest(
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,
253 hasUserGesture = hasUserGesture,
254 isSameDomain = isSameDomain,
255 isRedirect = isRedirect,
256 isDirectNavigation = isDirectNavigation,
257 isSubframeRequest = isSubframeRequest,