Skip to content

Commit e6f26f9

Browse files
fix(client): allow updating header/query affecting fields in toBuilder()
1 parent f3f3cda commit e6f26f9

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

cas-parser-java-core/src/main/kotlin/com/cas_parser/api/core/ClientOptions.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,14 @@ private constructor(
405405
headers.put("X-Stainless-Runtime", "JRE")
406406
headers.put("X-Stainless-Runtime-Version", getJavaVersion())
407407
headers.put("X-Stainless-Kotlin-Version", KotlinVersion.CURRENT.toString())
408+
// We replace after all the default headers to allow end-users to overwrite them.
409+
headers.replaceAll(this.headers.build())
410+
queryParams.replaceAll(this.queryParams.build())
408411
apiKey.let {
409412
if (!it.isEmpty()) {
410-
headers.put("x-api-key", it)
413+
headers.replace("x-api-key", it)
411414
}
412415
}
413-
headers.replaceAll(this.headers.build())
414-
queryParams.replaceAll(this.queryParams.build())
415416

416417
return ClientOptions(
417418
httpClient,

cas-parser-java-core/src/test/kotlin/com/cas_parser/api/core/ClientOptionsTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ internal class ClientOptionsTest {
1616

1717
private val httpClient = mock<HttpClient>()
1818

19+
@Test
20+
fun putHeader_canOverwriteDefaultHeader() {
21+
val clientOptions =
22+
ClientOptions.builder()
23+
.httpClient(httpClient)
24+
.putHeader("User-Agent", "My User Agent")
25+
.apiKey("My API Key")
26+
.build()
27+
28+
assertThat(clientOptions.headers.values("User-Agent")).containsExactly("My User Agent")
29+
}
30+
31+
@Test
32+
fun toBuilder_apiKeyAuthCanBeUpdated() {
33+
var clientOptions =
34+
ClientOptions.builder().httpClient(httpClient).apiKey("My API Key").build()
35+
36+
clientOptions = clientOptions.toBuilder().apiKey("another My API Key").build()
37+
38+
assertThat(clientOptions.headers.values("x-api-key")).containsExactly("another My API Key")
39+
}
40+
1941
@Test
2042
fun toBuilder_whenOriginalClientOptionsGarbageCollected_doesNotCloseOriginalClient() {
2143
var clientOptions =

0 commit comments

Comments
 (0)