Skip to content

Commit cca7ccc

Browse files
committed
update clientOptions and add relevant tests
1 parent 8f43c5d commit cca7ccc

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎src/main/java/com/auth0/client/mgmt/core/ClientOptions.java‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public final class ClientOptions {
2727

2828
private final int maxRetries;
2929

30+
private final Telemetry telemetry;
31+
3032
private ClientOptions(
3133
Environment environment,
3234
Map<String, String> headers,
@@ -49,6 +51,7 @@ private ClientOptions(
4951
this.httpClient = httpClient;
5052
this.timeout = timeout;
5153
this.maxRetries = maxRetries;
54+
this.telemetry = telemetry;
5255
}
5356

5457
public Environment environment() {
@@ -94,6 +97,32 @@ public int maxRetries() {
9497
return this.maxRetries;
9598
}
9699

100+
/**
101+
* The telemetry explicitly configured on this instance, or {@code null} if it was left to
102+
* default to {@code auth0-java}. Package-private so {@link Builder#from(ClientOptions)} can
103+
* carry it over; not part of the public API.
104+
*/
105+
Telemetry telemetry() {
106+
return this.telemetry;
107+
}
108+
109+
/**
110+
* The static headers configured on this instance, including the resolved {@code Auth0-Client}
111+
* header. Package-private so {@link Builder#from(ClientOptions)} can carry them over; not part
112+
* of the public API.
113+
*/
114+
Map<String, String> headers() {
115+
return this.headers;
116+
}
117+
118+
/**
119+
* The dynamic header suppliers configured on this instance. Package-private so
120+
* {@link Builder#from(ClientOptions)} can carry them over; not part of the public API.
121+
*/
122+
Map<String, Supplier<String>> headerSuppliers() {
123+
return this.headerSuppliers;
124+
}
125+
97126
public static Builder builder() {
98127
return new Builder();
99128
}
@@ -230,13 +259,22 @@ public ClientOptions build() {
230259
}
231260

232261
/**
233-
* Create a new Builder initialized with values from an existing ClientOptions
262+
* Create a new Builder initialized with values from an existing ClientOptions.
263+
*
264+
* <p>Interceptors and logging are not copied explicitly: they are already baked into the
265+
* {@link OkHttpClient} carried over here, and {@link #build()} preserves them via
266+
* {@code newBuilder()}. Copying them would attach them a second time. The same applies to
267+
* the retry interceptor, hence {@code maxRetries} is carried over for reporting only.
234268
*/
235269
public static Builder from(ClientOptions clientOptions) {
236270
Builder builder = new Builder();
237271
builder.environment = clientOptions.environment();
238272
builder.timeout = Optional.of(clientOptions.timeout(null));
239273
builder.httpClient = clientOptions.httpClient();
274+
builder.maxRetries = clientOptions.maxRetries();
275+
builder.telemetry = clientOptions.telemetry();
276+
builder.headers.putAll(clientOptions.headers());
277+
builder.headerSuppliers.putAll(clientOptions.headerSuppliers());
240278
return builder;
241279
}
242280
}

‎src/test/java/com/auth0/client/mgmt/ManagementApiTelemetryTest.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ public void shouldNestAuth0JavaInTelemetryEnv() throws Exception {
6262
assertThat(telemetry.get("env").get("auth0-java").asText(), is("auth0-java-9.9.9"));
6363
}
6464

65+
@Test
66+
public void shouldPreserveTelemetryAndHeadersWhenCopyingOptions() throws Exception {
67+
ClientOptions original = ClientOptions.builder()
68+
.environment(com.auth0.client.mgmt.core.Environment.DEFAULT)
69+
.telemetry(new Telemetry("my-wrapper-sdk", "1.2.3"))
70+
.addHeader("Authorization", "Bearer test-token")
71+
.addHeader("X-Dynamic", () -> "dynamic-value")
72+
.maxRetries(5)
73+
.build();
74+
75+
ClientOptions copy = ClientOptions.Builder.from(original).build();
76+
77+
Map<String, String> headers = copy.headers(null);
78+
JsonNode telemetry =
79+
ObjectMapperProvider.getMapper().readTree(Base64.getUrlDecoder().decode(headers.get("Auth0-Client")));
80+
assertThat(telemetry.get("name").asText(), is("my-wrapper-sdk"));
81+
assertThat(telemetry.get("version").asText(), is("1.2.3"));
82+
assertThat(headers.get("Authorization"), is("Bearer test-token"));
83+
assertThat(headers.get("X-Dynamic"), is("dynamic-value"));
84+
assertThat(copy.maxRetries(), is(5));
85+
}
86+
6587
@Test
6688
public void shouldReturnBuilderFromWithTelemetry() {
6789
ManagementApiBuilder builder = ManagementApi.builder();

0 commit comments

Comments
 (0)