Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Issue #696: browser-engine-system: Clear "X-Requested-With" header when loading URLs. #699

Merged
merged 1 commit into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import kotlinx.coroutines.experimental.launch
import mozilla.components.concept.engine.Settings
import mozilla.components.concept.engine.request.RequestInterceptor

internal val additionalHeaders = mapOf(
// For every request WebView sends a "X-requested-with" header with the package name of the
// application. We can't really prevent that but we can at least send an empty value.
// Unfortunately the additional headers will not be propagated to subsequent requests
// (e.g. redirects). See issue #696.
"X-Requested-With" to ""
)

/**
* WebView-based EngineSession implementation.
*/
Expand All @@ -34,7 +42,7 @@ class SystemEngineSession(private val defaultSettings: Settings? = null) : Engin
scheduledLoad = ScheduledLoad(url)
} else {
view?.get()?.currentUrl = url
internalView.loadUrl(url)
internalView.loadUrl(url, additionalHeaders)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class SystemEngineView @JvmOverloads constructor(
}

internalSession.scheduledLoad.url?.let {
currentWebView.loadUrl(it)
currentWebView.loadUrl(it, additionalHeaders)
internalSession.scheduledLoad = ScheduledLoad()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,31 @@ class SystemEngineSessionTest {

@Test
fun testLoadUrl() {
var loadedUrl: String? = null
var loadHeaders: Map<String, String>? = null

val engineSession = spy(SystemEngineSession())
val webView = mock(WebView::class.java)
val webView = spy(object : WebView(RuntimeEnvironment.application) {
override fun loadUrl(url: String?, additionalHttpHeaders: MutableMap<String, String>?) {
loadedUrl = url
loadHeaders = additionalHttpHeaders
}
})

engineSession.loadUrl("")
verify(webView, never()).loadUrl(anyString())

`when`(engineSession.currentView()).thenReturn(webView)

engineSession.loadUrl("http://mozilla.org")
verify(webView).loadUrl("http://mozilla.org")
verify(webView).loadUrl(eq("http://mozilla.org"), any())

assertEquals("http://mozilla.org", loadedUrl)

assertNotNull(loadHeaders)
assertEquals(1, loadHeaders!!.size)
assertTrue(loadHeaders!!.containsKey("X-Requested-With"))
assertEquals("", loadHeaders!!["X-Requested-With"])
}

@Test
Expand Down