Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ private constructor(

fun names(): Set<String> = map.keys

/**
* Returns all values associated with [name], or an empty list when the header is absent.
*
* Names returned by [names] always have at least one value.
*/
fun values(name: String): List<String> = map[name].orEmpty()

/**
* Returns the immutable, case-insensitive mapping of header names to their values.
*
* The returned map and its value lists are the same immutable representation used by this
* [Headers] instance; mutating the returned data is not supported.
*/
fun asMap(): Map<String, List<String>> = map

fun toBuilder(): Builder = Builder().putAll(map)

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.openai.core.http

import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Test

internal class HeadersMapViewTest {

@Test
fun asMapReturnsCaseInsensitiveImmutableHeaderMapping() {
val headers =
Headers.builder()
.put("X-Test", "one")
.put("x-test", "two")
.put("Other", "value")
.build()

val map = headers.asMap()

assertThat(map["X-TEST"]).containsExactly("one", "two")
assertThat(map["other"]).containsExactly("value")
assertThat(map).hasSize(2)
}

@Test
fun asMapDoesNotAllowMapMutation() {
val map = Headers.builder().put("X-Test", "value").build().asMap()

assertThatThrownBy {
@Suppress("UNCHECKED_CAST")
(map as MutableMap<String, List<String>>)["Other"] = listOf("value")
}
.isInstanceOf(UnsupportedOperationException::class.java)
}

@Test
fun asMapDoesNotAllowValueListMutation() {
val values = Headers.builder().put("X-Test", "value").build().asMap()["X-Test"]!!

assertThatThrownBy {
@Suppress("UNCHECKED_CAST")
(values as MutableList<String>).add("other")
}
.isInstanceOf(UnsupportedOperationException::class.java)
}
}