[components] Closes https://github.com/mozilla-mobile/android-components/issues/2025: concept-fetch: Add API to control if cookies should be sent
This commit is contained in:
@@ -26,6 +26,7 @@ dependencies {
|
||||
implementation Dependencies.kotlin_coroutines
|
||||
|
||||
implementation Dependencies.thirdparty_okhttp
|
||||
implementation Dependencies.thirdparty_okhttp_urlconnection
|
||||
|
||||
implementation project(':concept-fetch')
|
||||
|
||||
|
||||
@@ -9,8 +9,12 @@ import mozilla.components.concept.fetch.Headers
|
||||
import mozilla.components.concept.fetch.MutableHeaders
|
||||
import mozilla.components.concept.fetch.Request
|
||||
import mozilla.components.concept.fetch.Response
|
||||
import mozilla.components.lib.fetch.okhttp.OkHttpClient.Companion.getOrCreateCookieManager
|
||||
import okhttp3.JavaNetCookieJar
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.RequestBody
|
||||
import java.net.CookieHandler
|
||||
import java.net.CookieManager
|
||||
|
||||
typealias RequestBuilder = okhttp3.Request.Builder
|
||||
|
||||
@@ -32,12 +36,23 @@ class OkHttpClient(
|
||||
|
||||
return actualResponse.toResponse()
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun getOrCreateCookieManager(): CookieManager {
|
||||
if (CookieHandler.getDefault() == null) {
|
||||
CookieHandler.setDefault(CookieManager())
|
||||
}
|
||||
return CookieHandler.getDefault() as CookieManager
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun okhttp3.OkHttpClient.rebuildFor(request: Request): okhttp3.OkHttpClient {
|
||||
@Suppress("ComplexCondition")
|
||||
if (request.connectTimeout != null ||
|
||||
request.readTimeout != null ||
|
||||
request.redirect != Request.Redirect.FOLLOW
|
||||
request.redirect != Request.Redirect.FOLLOW ||
|
||||
request.cookiePolicy != Request.CookiePolicy.OMIT
|
||||
) {
|
||||
val clientBuilder = newBuilder()
|
||||
|
||||
@@ -48,6 +63,10 @@ private fun okhttp3.OkHttpClient.rebuildFor(request: Request): okhttp3.OkHttpCli
|
||||
clientBuilder.followRedirects(false)
|
||||
}
|
||||
|
||||
if (request.cookiePolicy == Request.CookiePolicy.INCLUDE) {
|
||||
clientBuilder.cookieJar(JavaNetCookieJar(getOrCreateCookieManager()))
|
||||
}
|
||||
|
||||
return clientBuilder.build()
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
package mozilla.components.lib.fetch.okhttp
|
||||
|
||||
import mozilla.components.concept.fetch.Client
|
||||
import org.junit.Assert
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class OkHttpFetchTestCases : mozilla.components.tooling.fetch.tests.FetchTestCases() {
|
||||
@@ -16,6 +16,6 @@ class OkHttpFetchTestCases : mozilla.components.tooling.fetch.tests.FetchTestCas
|
||||
@Test
|
||||
fun `Client instance`() {
|
||||
// We need at least one test case defined here so that this is recognized as test class.
|
||||
Assert.assertTrue(createNewClient() is OkHttpClient)
|
||||
assertTrue(createNewClient() is OkHttpClient)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user