-
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
2
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
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
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
| 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 androidNetworkingOptions | ||
| // 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 androidNetworkingOptions 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) | ||
| ) | ||
|
|
||
| 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 androidNetworkingOptions are sent on every request`() { | ||
| server.enqueue(MockResponse().setBody("{}")) | ||
|
|
||
| val client = A0Auth0Module.buildNetworkingClient( | ||
| JavaOnlyMap.of("defaultHeaders", JavaOnlyMap.of("X-Custom-Header", "custom-value")) | ||
| ) | ||
|
|
||
| client.load(server.url("/").toString(), RequestOptions(HttpMethod.GET)) | ||
|
|
||
| val recordedRequest = server.takeRequest() | ||
| assertTrue(recordedRequest.getHeader("X-Custom-Header") == "custom-value") | ||
| } | ||
| } |
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
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.
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: auth0/react-native-auth0
Length of output: 22137
🏁 Script executed:
Repository: auth0/react-native-auth0
Length of output: 22137
🏁 Script executed:
Repository: auth0/react-native-auth0
Length of output: 2448
🌐 Web query:
Auth0.Android 4.0.1 Auth0.getInstance networkingClient singleton source💡 Result:
In the Auth0.Android SDK (including version 4.0.1), the Auth0.getInstance method acts as a factory that returns a singleton instance of the Auth0 class for a given configuration [1]. Regarding your query about the networkingClient: 1. Singleton Behavior: The Auth0 class is managed as a singleton per configuration [1]. When you call Auth0.getInstance, it checks if an instance with the specified client ID and domain already exists; if so, it returns that existing instance [1]. 2. NetworkingClient Property: The Auth0 instance contains a networkingClient property (type NetworkingClient) which is used for all HTTP requests made by the library's various API clients (e.g., AuthenticationAPIClient, UsersAPIClient) [2][3][4]. 3. Customization: By default, this property is initialized with a DefaultClient [3]. You can replace it with a custom implementation by setting the networkingClient property directly on your Auth0 singleton instance [2][3]. Example of configuring the networking client: val account = Auth0.getInstance("{YOUR_CLIENT_ID}", "{YOUR_DOMAIN}") account.networkingClient = CustomNetworkingClient // Replace with your implementation Note that because the Auth0 instance is a singleton, changing the networkingClient on that instance will affect all subsequent API operations that rely on that specific Auth0 account instance [4].
Citations:
🏁 Script executed:
Repository: auth0/react-native-auth0
Length of output: 6617
Reset the networking client when options are omitted.
Auth0.getInstance(clientId, domain)reuses the same instance. A later initialization withoutandroidNetworkingOptionspreserves the previous customnetworkingClient. AssignDefaultClient()when the options are absent, and add a same-configuration re-initialization test.🤖 Prompt for AI Agents
Source: MCP tools