Bug 1881128 - Add additional headers for Google requests

(cherry picked from commit 9bc558c887908470ee1746518cc9926a930ecc3e)
fenix/124.1.0
Gabriel Luong 3 months ago committed by mergify[bot]
parent ae5f908543
commit 36886c0d0a

@ -19,8 +19,8 @@ import mozilla.components.concept.engine.request.RequestInterceptor
*/ */
class UrlRequestInterceptor(private val isDeviceRamAboveThreshold: Boolean) : RequestInterceptor { class UrlRequestInterceptor(private val isDeviceRamAboveThreshold: Boolean) : RequestInterceptor {
private val isGoogleSearchRequest by lazy { private val isGoogleRequest by lazy {
Regex("^https://www\\.google\\.(?:.+)/search") Regex("^https://www\\.google\\..+")
} }
@VisibleForTesting @VisibleForTesting
@ -41,7 +41,7 @@ class UrlRequestInterceptor(private val isDeviceRamAboveThreshold: Boolean) : Re
uri: String, uri: String,
isSubframeRequest: Boolean, isSubframeRequest: Boolean,
): Boolean { ): Boolean {
return !isSubframeRequest && isGoogleSearchRequest.containsMatchIn(uri) return !isSubframeRequest && isGoogleRequest.containsMatchIn(uri)
} }
override fun onLoadRequest( override fun onLoadRequest(

@ -59,15 +59,21 @@ class UrlRequestInterceptorTest {
fun `WHEN should intercept request is called THEN return the correct boolean value`() { fun `WHEN should intercept request is called THEN return the correct boolean value`() {
val urlRequestInterceptor = getUrlRequestInterceptor() val urlRequestInterceptor = getUrlRequestInterceptor()
assertFalse( assertTrue(
urlRequestInterceptor.shouldInterceptRequest( urlRequestInterceptor.shouldInterceptRequest(
uri = "https://getpocket.com", uri = "https://www.google.com",
isSubframeRequest = false, isSubframeRequest = false,
), ),
) )
assertFalse( assertTrue(
urlRequestInterceptor.shouldInterceptRequest( urlRequestInterceptor.shouldInterceptRequest(
uri = "https://www.google.com", uri = "https://www.google.com/webhp",
isSubframeRequest = false,
),
)
assertTrue(
urlRequestInterceptor.shouldInterceptRequest(
uri = "https://www.google.com/preferences",
isSubframeRequest = false, isSubframeRequest = false,
), ),
) )
@ -89,6 +95,13 @@ class UrlRequestInterceptorTest {
isSubframeRequest = false, isSubframeRequest = false,
), ),
) )
assertFalse(
urlRequestInterceptor.shouldInterceptRequest(
uri = "https://getpocket.com",
isSubframeRequest = false,
),
)
assertFalse( assertFalse(
urlRequestInterceptor.shouldInterceptRequest( urlRequestInterceptor.shouldInterceptRequest(
uri = "https://www.google.com/search?q=blue", uri = "https://www.google.com/search?q=blue",
@ -114,13 +127,24 @@ class UrlRequestInterceptorTest {
} }
@Test @Test
fun `WHEN a Google request is loaded THEN request is not intercepted`() { fun `WHEN a Google request is loaded THEN request is intercepted`() {
val uri = "https://www.google.com" val uri = "https://www.google.com"
val response = getUrlRequestInterceptor().onLoadRequest(
uri = uri,
)
assertNull(response) assertEquals(
RequestInterceptor.InterceptionResponse.Url(
url = uri,
flags = LoadUrlFlags.select(
LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE,
ALLOW_ADDITIONAL_HEADERS,
),
additionalHeaders = mapOf(
"X-Search-Subdivision" to "0",
),
),
getUrlRequestInterceptor().onLoadRequest(
uri = uri,
),
)
} }
@Test @Test

Loading…
Cancel
Save