-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Feature/google analytics surrogates #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
175a257
WIP implementation of google analytics surrogates
CDRussell 81f030b
Quieter logging
CDRussell 6e2dd09
Surrogate logic occurs only after identifying a url as one to block
CDRussell 58209ad
Merge branch 'develop' into feature/google_analytics_surrogates
CDRussell 69eee77
Biggish refactor to allow better testing of web view request intercepor
CDRussell 79527db
Add more tests for the "should intercept request" logic
CDRussell 5b45749
Lowering noise of log statement
CDRussell 2f73d3b
Mark AnalyticsSurrogates as a singleton
CDRussell bbdf3d3
Clear the string builder between surrogates
CDRussell 6b1aaea
Update surrogate test files to be just test functions; not the real t…
CDRussell ff87c4c
Remove word Analytics; more generically replaced with ResourceSurrogate
CDRussell bea9dba
Replace all real comments with fake comments for surrogate test data
CDRussell 5336ba2
Add extra tests and catch all errors for extra parsing safety
CDRussell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
315 changes: 315 additions & 0 deletions
315
app/src/androidTest/java/com/duckduckgo/app/browser/WebViewRequestInterceptorTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,315 @@ | ||
| /* | ||
| * Copyright (c) 2018 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.app.browser | ||
|
|
||
| import android.net.Uri | ||
| import android.support.test.InstrumentationRegistry | ||
| import android.support.test.annotation.UiThreadTest | ||
| import android.webkit.WebResourceRequest | ||
| import android.webkit.WebResourceResponse | ||
| import android.webkit.WebView | ||
| import com.duckduckgo.app.surrogates.ResourceSurrogates | ||
| import com.duckduckgo.app.surrogates.SurrogateResponse | ||
| import com.duckduckgo.app.httpsupgrade.HttpsUpgrader | ||
| import com.duckduckgo.app.trackerdetection.TrackerDetector | ||
| import com.duckduckgo.app.trackerdetection.model.TrackingEvent | ||
| import com.nhaarman.mockito_kotlin.* | ||
| import org.junit.Assert.* | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.mockito.ArgumentMatchers.anyString | ||
| import org.mockito.Mock | ||
| import org.mockito.MockitoAnnotations | ||
|
|
||
| class WebViewRequestInterceptorTest { | ||
|
|
||
| private lateinit var testee: WebViewRequestInterceptor | ||
|
|
||
| @Mock | ||
| private lateinit var mockTrackerDetector: TrackerDetector | ||
| @Mock | ||
| private lateinit var mockHttpsUpgrader: HttpsUpgrader | ||
| @Mock | ||
| private lateinit var mockResourceSurrogates: ResourceSurrogates | ||
| @Mock | ||
| private lateinit var mockRequest: WebResourceRequest | ||
|
|
||
| private lateinit var webView: WebView | ||
|
|
||
| @UiThreadTest | ||
| @Before | ||
| fun setup() { | ||
| MockitoAnnotations.initMocks(this) | ||
|
|
||
| testee = WebViewRequestInterceptor( | ||
| trackerDetector = mockTrackerDetector, | ||
| httpsUpgrader = mockHttpsUpgrader, | ||
| resourceSurrogates = mockResourceSurrogates | ||
| ) | ||
|
|
||
| val context = InstrumentationRegistry.getTargetContext() | ||
|
|
||
| webView = WebView(context) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlShouldBeUpgradedThenUpgraderInvoked() { | ||
| configureShouldUpgrade() | ||
| testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = null, | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
|
|
||
| verify(mockHttpsUpgrader).upgrade(any()) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlShouldBeUpgradedThenCancelledResponseReturned() { | ||
| configureShouldUpgrade() | ||
| val response = testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = null, | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
|
|
||
| assertCancelledResponse(response) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlShouldBeUpgradedButNotOnMainFrameThenNotUpgraded() { | ||
| configureShouldUpgrade() | ||
| whenever(mockRequest.isForMainFrame).thenReturn(false) | ||
| testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = null, | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
|
|
||
| verify(mockHttpsUpgrader, never()).upgrade(any()) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlShouldBeUpgradedButUrlIsNullThenNotUpgraded() { | ||
| configureShouldUpgrade() | ||
| whenever(mockRequest.url).thenReturn(null) | ||
| testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = null, | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
|
|
||
| verify(mockHttpsUpgrader, never()).upgrade(any()) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlShouldNotBeUpgradedThenUpgraderNotInvoked() { | ||
| whenever(mockHttpsUpgrader.shouldUpgrade(any())).thenReturn(false) | ||
| testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = null, | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
|
|
||
| verify(mockHttpsUpgrader, never()).upgrade(any()) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenCurrentUrlIsNullThenShouldContinueToLoad() { | ||
| configureShouldNotUpgrade() | ||
| val response = testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = null, | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
| assertRequestCanContinueToLoad(response) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenIsTrustedSite_DuckDuckGo_ThenShouldContinueToLoad() { | ||
| configureShouldNotUpgrade() | ||
| val response = testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = "duckduckgo.com/a/b/c?q=123", | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
|
|
||
| assertRequestCanContinueToLoad(response) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenIsTrustedSite_DontTrack_ThenShouldContinueToLoad() { | ||
| configureShouldNotUpgrade() | ||
| val response = testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = "donttrack.us/a/b/c?q=123", | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
|
|
||
| assertRequestCanContinueToLoad(response) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenIsTrustedSite_SpreadPrivacy_ThenShouldContinueToLoad() { | ||
| configureShouldNotUpgrade() | ||
| val response = testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = "spreadprivacy.com/a/b/c?q=123", | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
|
|
||
| assertRequestCanContinueToLoad(response) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenIsTrustedSite_DuckDuckHack_ThenShouldContinueToLoad() { | ||
| configureShouldNotUpgrade() | ||
| val response = testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = "duckduckhack.com/a/b/c?q=123", | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
|
|
||
| assertRequestCanContinueToLoad(response) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenIsTrustedSite_PrivateBrowsingMyths_ThenShouldContinueToLoad() { | ||
| configureShouldNotUpgrade() | ||
| val response = testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = "privatebrowsingmyths.com/a/b/c?q=123", | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
|
|
||
| assertRequestCanContinueToLoad(response) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenIsTrustedSite_DuckDotCo_ThenShouldContinueToLoad() { | ||
| configureShouldNotUpgrade() | ||
| val response = testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = "duck.co/a/b/c?q=123", | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
|
|
||
| assertRequestCanContinueToLoad(response) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenIsHttpRequestThenHttpRequestListenerCalled() { | ||
| configureShouldNotUpgrade() | ||
| whenever(mockRequest.url).thenReturn(Uri.parse("http://example.com")) | ||
| val mockListener = mock<WebViewClientListener>() | ||
|
|
||
| val response = testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = "foo.com", | ||
| webView = webView, | ||
| webViewClientListener = mockListener) | ||
|
|
||
| verify(mockListener).pageHasHttpResources(anyString()) | ||
| assertRequestCanContinueToLoad(response) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenIsHttpsRequestThenHttpRequestListenerNotCalled() { | ||
| configureShouldNotUpgrade() | ||
| whenever(mockRequest.url).thenReturn(Uri.parse("https://example.com")) | ||
| val mockListener = mock<WebViewClientListener>() | ||
|
|
||
| val response = testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = "foo.com", | ||
| webView = webView, | ||
| webViewClientListener = mockListener) | ||
|
|
||
| verify(mockListener, never()).pageHasHttpResources(anyString()) | ||
| assertRequestCanContinueToLoad(response) | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| fun whenRequestShouldBlockAndNoSurrogateThenCancellingResponseReturned() { | ||
| whenever(mockResourceSurrogates.get(any())).thenReturn(SurrogateResponse(responseAvailable = false)) | ||
|
|
||
| configureShouldNotUpgrade() | ||
| configureShouldBlock() | ||
| val response = testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = "foo.com", | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
|
|
||
| assertCancelledResponse(response) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenRequestShouldBlockButThereIsASurrogateThen() { | ||
| val availableSurrogate = SurrogateResponse( | ||
| responseAvailable = true, | ||
| mimeType = "application/javascript", | ||
| jsFunction = "javascript replacement function goes here") | ||
| whenever(mockResourceSurrogates.get(any())).thenReturn(availableSurrogate) | ||
|
|
||
| configureShouldNotUpgrade() | ||
| configureShouldBlock() | ||
| val response = testee.shouldIntercept( | ||
| request = mockRequest, | ||
| currentUrl = "foo.com", | ||
| webView = webView, | ||
| webViewClientListener = null) | ||
|
|
||
| assertEquals(availableSurrogate.jsFunction.byteInputStream().read(), response!!.data.read()) | ||
| } | ||
|
|
||
| private fun assertRequestCanContinueToLoad(response: WebResourceResponse?) { | ||
| assertNull(response) | ||
| } | ||
|
|
||
| private fun configureShouldBlock() { | ||
| val blockTrackingEvent = TrackingEvent(blocked = true, | ||
| documentUrl = "", | ||
| trackerUrl = "", | ||
| trackerNetwork = null) | ||
| whenever(mockRequest.isForMainFrame).thenReturn(false) | ||
| whenever(mockTrackerDetector.evaluate(any(), any(), any())).thenReturn(blockTrackingEvent) | ||
| } | ||
|
|
||
| private fun configureShouldUpgrade() { | ||
| whenever(mockHttpsUpgrader.shouldUpgrade(any())).thenReturn(true) | ||
| whenever(mockRequest.url).thenReturn(validUri()) | ||
| whenever(mockRequest.isForMainFrame).thenReturn(true) | ||
| } | ||
|
|
||
| private fun configureShouldNotUpgrade() { | ||
| whenever(mockHttpsUpgrader.shouldUpgrade(any())).thenReturn(false) | ||
| whenever(mockRequest.url).thenReturn(validUri()) | ||
| whenever(mockRequest.isForMainFrame).thenReturn(true) | ||
| } | ||
|
|
||
| private fun validUri() = Uri.parse("example.com") | ||
|
|
||
| private fun assertCancelledResponse(response: WebResourceResponse?) { | ||
| assertNotNull(response) | ||
| assertNull(response!!.data) | ||
| assertNull(response.mimeType) | ||
| assertNull(response.encoding) | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,7 @@ class HttpsUpgraderTest { | |
| @Before | ||
| fun before() { | ||
| mockDao = mock() | ||
| testee = HttpsUpgrader(mockDao) | ||
| testee = HttpsUpgraderImpl(mockDao) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The great implementation name debate. But nothing to change here, another one to discuss probably. |
||
| } | ||
|
|
||
| @Test | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We seem to be mixing and matching approaches. Nothing to do here, but we should all perhaps have a chat about what the standard approach should be (I prefer just creating them directly without annotations or using initMocks personally).