Skip to content

Commit

Permalink
Issue mozilla-mobile#696: browser-engine-system: Clear "X-Requested-W…
Browse files Browse the repository at this point in the history
…ith" header when loading URLs.
  • Loading branch information
pocmo committed Sep 4, 2018
1 parent c6b57a3 commit 91038db
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
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

0 comments on commit 91038db

Please sign in to comment.