Skip to content

Commit 17936b0

Browse files
committed
Adding extension function to extract eTag
1 parent 3c39b3d commit 17936b0

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2021 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.app.trackerdetection.api
18+
19+
import okhttp3.Headers
20+
import org.junit.Assert.*
21+
import org.junit.Test
22+
23+
class TrackerDataDownloaderKtTest {
24+
25+
@Test
26+
fun whenExtractETagAndContainsPrefixAndQuotesThenReturnETag() {
27+
val headers = Headers.headersOf("eTag", "W/\"123456789\"")
28+
assertEquals(ETAG, headers.extractETag())
29+
}
30+
31+
@Test
32+
fun whenExtractETagAndContainsQuotesThenReturnETag() {
33+
val headers = Headers.headersOf("eTag", "\"123456789\"")
34+
assertEquals(ETAG, headers.extractETag())
35+
}
36+
37+
@Test
38+
fun whenExtractETagAndDoesNotContainsQuotesAndPrefixThenReturnETag() {
39+
val headers = Headers.headersOf("eTag", "123456789")
40+
assertEquals(ETAG, headers.extractETag())
41+
}
42+
43+
companion object {
44+
const val ETAG = "123456789"
45+
}
46+
}

app/src/main/java/com/duckduckgo/app/trackerdetection/api/TrackerDataDownloader.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import com.duckduckgo.app.global.store.BinaryDataStore
2323
import com.duckduckgo.app.trackerdetection.Client.ClientName.*
2424
import com.duckduckgo.app.trackerdetection.TrackerDataLoader
2525
import com.duckduckgo.app.trackerdetection.db.TdsMetadataDao
26-
import com.duckduckgo.app.trackerdetection.db.TdsTrackerDao
2726
import com.duckduckgo.app.trackerdetection.db.TemporaryTrackingWhitelistDao
2827
import com.duckduckgo.app.trackerdetection.model.TemporaryTrackingWhitelistedDomain
2928
import io.reactivex.Completable
29+
import okhttp3.Headers
3030
import timber.log.Timber
3131
import java.io.IOException
3232
import javax.inject.Inject
@@ -54,10 +54,10 @@ class TrackerDataDownloader @Inject constructor(
5454
}
5555

5656
val body = response.body()!!
57-
val eTag = response.headers()["eTag"]?.removeSurrounding("W/\"", "\"").orEmpty() // removes weak eTag validator
57+
val eTag = response.headers().extractETag()
58+
Timber.d("Updating tds data from server")
5859
val oldEtag = metadataDao.eTag()
5960
if (eTag != oldEtag) {
60-
Timber.d("Updating tds data from server")
6161
appDatabase.runInTransaction {
6262
trackerDataLoader.persistTds(eTag, body)
6363
trackerDataLoader.loadTrackers()
@@ -107,3 +107,7 @@ class TrackerDataDownloader @Inject constructor(
107107
}
108108
}
109109
}
110+
111+
fun Headers.extractETag(): String {
112+
return this["eTag"]?.removePrefix("W/")?.removeSurrounding("\"", "\"").orEmpty() // removes weak eTag validator
113+
}

0 commit comments

Comments
 (0)