From d964c438a3495c4223c35fefc79eabbeff19d735 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Tue, 12 Jan 2021 18:41:27 +0100 Subject: [PATCH] add internal method to accept Any parameter on BaseRequest --- .../com/auth0/android/management/UsersAPIClient.kt | 9 +++++++-- .../java/com/auth0/android/request/DefaultClient.kt | 4 +++- .../java/com/auth0/android/request/RequestOptions.kt | 2 +- .../auth0/android/request/internal/BaseRequest.kt | 5 +++++ .../auth0/android/management/UsersAPIClientTest.java | 3 --- .../auth0/android/provider/WebAuthProviderTest.kt | 6 +++--- .../internal/BaseAuthenticationRequestTest.java | 12 ++++++------ .../android/request/internal/BaseRequestTest.java | 4 ++-- 8 files changed, 27 insertions(+), 18 deletions(-) diff --git a/auth0/src/main/java/com/auth0/android/management/UsersAPIClient.kt b/auth0/src/main/java/com/auth0/android/management/UsersAPIClient.kt index 67cb9c407..18989226b 100755 --- a/auth0/src/main/java/com/auth0/android/management/UsersAPIClient.kt +++ b/auth0/src/main/java/com/auth0/android/management/UsersAPIClient.kt @@ -28,6 +28,7 @@ import com.auth0.android.Auth0 import com.auth0.android.Auth0Exception import com.auth0.android.authentication.ParameterBuilder import com.auth0.android.request.* +import com.auth0.android.request.internal.BaseRequest import com.auth0.android.request.internal.GsonAdapter import com.auth0.android.request.internal.GsonAdapter.Companion.forListOf import com.auth0.android.request.internal.GsonAdapter.Companion.forMap @@ -186,8 +187,12 @@ public class UsersAPIClient @VisibleForTesting(otherwise = VisibleForTesting.PRI val userProfileAdapter: JsonAdapter = GsonAdapter( UserProfile::class.java, gson ) - return factory.patch(url.toString(), userProfileAdapter) - .addParameter(USER_METADATA_KEY, gson.toJson(userMetadata)) + val patch = factory.patch( + url.toString(), + userProfileAdapter + ) as BaseRequest + patch.addParameter(USER_METADATA_KEY, userMetadata) + return patch } /** diff --git a/auth0/src/main/java/com/auth0/android/request/DefaultClient.kt b/auth0/src/main/java/com/auth0/android/request/DefaultClient.kt index e2c83e58a..75ff85e31 100644 --- a/auth0/src/main/java/com/auth0/android/request/DefaultClient.kt +++ b/auth0/src/main/java/com/auth0/android/request/DefaultClient.kt @@ -40,7 +40,8 @@ public class DefaultClient() : NetworkingClient { when (options.method) { is HttpMethod.GET -> { // add parameters as query - options.parameters.map { urlBuilder.addQueryParameter(it.key, it.value) } + options.parameters.filterValues { it is String } + .map { urlBuilder.addQueryParameter(it.key, it.value as String) } requestBuilder.method(options.method.toString(), null) } else -> { @@ -66,6 +67,7 @@ public class DefaultClient() : NetworkingClient { val builder = OkHttpClient.Builder() // logging + //TODO: OFF by default! if (enableLogging) { val logger: Interceptor = HttpLoggingInterceptor() .setLevel(HttpLoggingInterceptor.Level.BODY) diff --git a/auth0/src/main/java/com/auth0/android/request/RequestOptions.kt b/auth0/src/main/java/com/auth0/android/request/RequestOptions.kt index dfd7726e6..f93efe678 100644 --- a/auth0/src/main/java/com/auth0/android/request/RequestOptions.kt +++ b/auth0/src/main/java/com/auth0/android/request/RequestOptions.kt @@ -4,6 +4,6 @@ package com.auth0.android.request * Holder for the information required to configure a request */ public class RequestOptions(public val method: HttpMethod) { - public val parameters: MutableMap = mutableMapOf() + public val parameters: MutableMap = mutableMapOf() public val headers: MutableMap = mutableMapOf() } \ No newline at end of file diff --git a/auth0/src/main/java/com/auth0/android/request/internal/BaseRequest.kt b/auth0/src/main/java/com/auth0/android/request/internal/BaseRequest.kt index 9d1dade12..b35232824 100755 --- a/auth0/src/main/java/com/auth0/android/request/internal/BaseRequest.kt +++ b/auth0/src/main/java/com/auth0/android/request/internal/BaseRequest.kt @@ -71,6 +71,11 @@ public open class BaseRequest internal constructor( return this } + internal fun addParameter(name: String, value: Any): Request { + options.parameters[name] = value + return this + } + /** * Runs asynchronously and executes the network request, without blocking the current thread. * The result is parsed into a value and posted in the callback's onSuccess method or a diff --git a/auth0/src/test/java/com/auth0/android/management/UsersAPIClientTest.java b/auth0/src/test/java/com/auth0/android/management/UsersAPIClientTest.java index d527c6771..9163a752a 100755 --- a/auth0/src/test/java/com/auth0/android/management/UsersAPIClientTest.java +++ b/auth0/src/test/java/com/auth0/android/management/UsersAPIClientTest.java @@ -51,7 +51,6 @@ import org.hamcrest.collection.IsMapWithSize; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; @@ -288,7 +287,6 @@ public void shouldUnlinkAccountSync() throws Exception { } @Test - @Ignore("PATCH method not supported by HttpUrlConnection") public void shouldUpdateUserMetadata() throws Exception { mockAPI.willReturnUserProfile(); @@ -316,7 +314,6 @@ public void shouldUpdateUserMetadata() throws Exception { } @Test - @Ignore("PATCH method not supported by HttpUrlConnection") public void shouldUpdateUserMetadataSync() throws Exception { mockAPI.willReturnUserProfile(); diff --git a/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt b/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt index c4412e613..975844f65 100644 --- a/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt +++ b/auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt @@ -1078,15 +1078,15 @@ public class WebAuthProviderTest { ) ) ) - MatcherAssert.assertThat>( + MatcherAssert.assertThat>( codeOptionsCaptor.firstValue.parameters, IsMapContaining.hasEntry("code", "1234") ) - MatcherAssert.assertThat>( + MatcherAssert.assertThat>( codeOptionsCaptor.firstValue.parameters, IsMapContaining.hasEntry("grant_type", "authorization_code") ) - MatcherAssert.assertThat>( + MatcherAssert.assertThat>( codeOptionsCaptor.firstValue.parameters, IsMapContaining.hasKey("code_verifier") ) diff --git a/auth0/src/test/java/com/auth0/android/request/internal/BaseAuthenticationRequestTest.java b/auth0/src/test/java/com/auth0/android/request/internal/BaseAuthenticationRequestTest.java index 3a00cf022..5133a6f2f 100644 --- a/auth0/src/test/java/com/auth0/android/request/internal/BaseAuthenticationRequestTest.java +++ b/auth0/src/test/java/com/auth0/android/request/internal/BaseAuthenticationRequestTest.java @@ -74,7 +74,7 @@ public void shouldSetGrantType() throws Exception { .execute(); verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); + Map values = optionsCaptor.getValue().getParameters(); assertThat(values, aMapWithSize(1)); assertThat(values, hasEntry("grant_type", "grantType")); } @@ -88,7 +88,7 @@ public void shouldSetConnection() throws Exception { .execute(); verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); + Map values = optionsCaptor.getValue().getParameters(); assertThat(values, aMapWithSize(1)); assertThat(values, hasEntry("connection", "my-connection")); } @@ -102,7 +102,7 @@ public void shouldSetRealm() throws Exception { .execute(); verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); + Map values = optionsCaptor.getValue().getParameters(); assertThat(values, aMapWithSize(1)); assertThat(values, hasEntry("realm", "my-realm")); } @@ -116,7 +116,7 @@ public void shouldSetScope() throws Exception { .execute(); verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); + Map values = optionsCaptor.getValue().getParameters(); assertThat(values, aMapWithSize(1)); assertThat(values, hasEntry("scope", "email profile")); } @@ -130,7 +130,7 @@ public void shouldSetAudience() throws Exception { .execute(); verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); + Map values = optionsCaptor.getValue().getParameters(); assertThat(values, aMapWithSize(1)); assertThat(values, hasEntry("audience", "my-api")); } @@ -148,7 +148,7 @@ public void shouldAddAuthenticationParameters() throws Exception { .execute(); verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); + Map values = optionsCaptor.getValue().getParameters(); assertThat(values, aMapWithSize(2)); assertThat(values, hasEntry("extra", "value")); assertThat(values, hasEntry("123", "890")); diff --git a/auth0/src/test/java/com/auth0/android/request/internal/BaseRequestTest.java b/auth0/src/test/java/com/auth0/android/request/internal/BaseRequestTest.java index 70a62bae5..8e82afde9 100755 --- a/auth0/src/test/java/com/auth0/android/request/internal/BaseRequestTest.java +++ b/auth0/src/test/java/com/auth0/android/request/internal/BaseRequestTest.java @@ -131,7 +131,7 @@ public void shouldAddParameter() throws Exception { baseRequest.execute(); verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); + Map values = optionsCaptor.getValue().getParameters(); assertThat(values, aMapWithSize(1)); assertThat(values, hasEntry("A", "1")); } @@ -148,7 +148,7 @@ public void shouldAddParameters() throws Exception { baseRequest.execute(); verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); + Map values = optionsCaptor.getValue().getParameters(); assertThat(values, aMapWithSize(2)); assertThat(values, hasEntry("A", "1")); assertThat(values, hasEntry("B", "2"));