-
Notifications
You must be signed in to change notification settings - Fork 242
feat: allow configuring Auth0.Android's native networking client #1637
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
Open
NandanPrabhu
wants to merge
6
commits into
v6-development
Choose a base branch
from
feat/sdk-10614-android-networking-config
base: v6-development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
69df62a
feat: allow configuring Auth0.Android's native networking client
NandanPrabhu 1aaee83
fix: remove unnecessary unitTests.returnDefaultValues from android/bu…
NandanPrabhu 3d287e1
fix: gate enableLogging behind debug builds and stop leaking networki…
NandanPrabhu 6f81aa7
refactor: avoid the not-null assertion operator in initializeAuth0Wit…
NandanPrabhu b4c4e9f
refactor!: rename androidNetworkingOptions to networkingOptions
NandanPrabhu 83cb092
added android tests to github action workflow
NandanPrabhu 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
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
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
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
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
124 changes: 124 additions & 0 deletions
124
android/src/test/java/com/auth0/react/A0Auth0ModuleNetworkingOptionsTest.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,124 @@ | ||
| package com.auth0.react | ||
|
|
||
| import com.auth0.android.request.HttpMethod | ||
| import com.auth0.android.request.RequestOptions | ||
| import com.facebook.react.bridge.JavaOnlyMap | ||
| import okhttp3.mockwebserver.MockResponse | ||
| import okhttp3.mockwebserver.MockWebServer | ||
| import org.junit.After | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Assert.fail | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import java.io.IOException | ||
| import java.util.concurrent.TimeUnit | ||
| import kotlin.system.measureTimeMillis | ||
|
|
||
| // Proves that A0Auth0Module.buildNetworkingClient() genuinely threads networkingOptions | ||
| // into the DefaultClient it builds, rather than just compiling. Exercises the client against a | ||
| // real (local) server so the OkHttp timeout machinery actually runs. | ||
| class A0Auth0ModuleNetworkingOptionsTest { | ||
|
|
||
| private lateinit var server: MockWebServer | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| server = MockWebServer() | ||
| server.start() | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| server.shutdown() | ||
| } | ||
|
|
||
| @Test | ||
| fun `readTimeout from networkingOptions is applied to the built DefaultClient`() { | ||
| val configuredTimeoutSeconds = 1 | ||
| // Stall the response well past the configured timeout. | ||
| server.enqueue(MockResponse().setHeadersDelay(3, TimeUnit.SECONDS).setBody("{}")) | ||
|
|
||
| val client = A0Auth0Module.buildNetworkingClient( | ||
| JavaOnlyMap.of("readTimeout", configuredTimeoutSeconds), | ||
| isDebuggable = true | ||
| ) | ||
|
|
||
| var threw = false | ||
| val elapsedMillis = measureTimeMillis { | ||
| try { | ||
| client.load(server.url("/").toString(), RequestOptions(HttpMethod.GET)) | ||
| fail("Expected the configured read timeout to fire") | ||
| } catch (e: IOException) { | ||
| threw = true | ||
| } | ||
| } | ||
|
|
||
| assertTrue("Expected an IOException from the read timeout", threw) | ||
| // The server stalls for 3s; a working 1s readTimeout must fire well before that. | ||
| assertTrue( | ||
| "Expected the call to fail near the configured ${configuredTimeoutSeconds}s timeout, took ${elapsedMillis}ms", | ||
| elapsedMillis < TimeUnit.SECONDS.toMillis(2) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `defaultHeaders from networkingOptions are sent on every request`() { | ||
| server.enqueue(MockResponse().setBody("{}")) | ||
|
|
||
| val client = A0Auth0Module.buildNetworkingClient( | ||
| JavaOnlyMap.of("defaultHeaders", JavaOnlyMap.of("X-Custom-Header", "custom-value")), | ||
| isDebuggable = true | ||
| ) | ||
|
|
||
| client.load(server.url("/").toString(), RequestOptions(HttpMethod.GET)) | ||
|
|
||
| val recordedRequest = server.takeRequest() | ||
| assertTrue(recordedRequest.getHeader("X-Custom-Header") == "custom-value") | ||
| } | ||
|
|
||
| @Test | ||
| fun `enableLogging is ignored on a non-debuggable build even when requested`() { | ||
| server.enqueue(MockResponse().setBody("{}")) | ||
|
|
||
| // If the debuggable gate is ever removed, Auth0.Android attaches its logging | ||
| // interceptor and this request crashes ("Method ... not mocked") because | ||
| // android.util.Log isn't stubbed in this unit test environment - that crash is | ||
| // exactly the regression this test is meant to catch. | ||
| val client = A0Auth0Module.buildNetworkingClient( | ||
| JavaOnlyMap.of("enableLogging", true), | ||
| isDebuggable = false | ||
| ) | ||
|
|
||
| client.load(server.url("/").toString(), RequestOptions(HttpMethod.GET)) | ||
|
|
||
| val recordedRequest = server.takeRequest() | ||
| assertTrue(recordedRequest.method == "GET") | ||
| } | ||
|
|
||
| @Test | ||
| fun `re-initializing with the same configuration but no options restores the default client`() { | ||
| // First "initialization": custom options give a 1s readTimeout. | ||
| val customized = A0Auth0Module.resolveNetworkingClient( | ||
| JavaOnlyMap.of("readTimeout", 1), | ||
| isDebuggable = true | ||
| ) | ||
|
|
||
| // Re-initialization with the same clientId/domain but networkingOptions omitted | ||
| // must not carry the previous readTimeout forward - it should behave like a fresh | ||
| // DefaultClient() (10s default readTimeout). | ||
| val reset = A0Auth0Module.resolveNetworkingClient(null, isDebuggable = true) | ||
|
|
||
| server.enqueue(MockResponse().setHeadersDelay(3, TimeUnit.SECONDS).setBody("{}")) | ||
| var threw = false | ||
| try { | ||
| customized.load(server.url("/").toString(), RequestOptions(HttpMethod.GET)) | ||
| } catch (e: IOException) { | ||
| threw = true | ||
| } | ||
| assertTrue("Expected the 1s readTimeout to fire on the customized client", threw) | ||
|
|
||
| server.enqueue(MockResponse().setHeadersDelay(3, TimeUnit.SECONDS).setBody("{}")) | ||
| // Should comfortably survive the 3s delay under the restored 10s default readTimeout. | ||
| reset.load(server.url("/").toString(), RequestOptions(HttpMethod.GET)) | ||
| } | ||
| } |
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
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
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.