|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Square, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package okhttp3 |
| 17 | + |
| 18 | +import assertk.assertThat |
| 19 | +import assertk.assertions.isEqualTo |
| 20 | +import assertk.assertions.isNull |
| 21 | +import okhttp3.HttpUrl.Companion.toHttpUrl |
| 22 | +import org.junit.jupiter.api.Test |
| 23 | +import org.junit.jupiter.api.extension.RegisterExtension |
| 24 | + |
| 25 | +class CallTagsTest { |
| 26 | + @JvmField @RegisterExtension |
| 27 | + val clientTestRule = OkHttpClientTestRule() |
| 28 | + |
| 29 | + private var client = clientTestRule.newClient() |
| 30 | + |
| 31 | + @Test |
| 32 | + fun tagsSeededFromRequest() { |
| 33 | + val request = |
| 34 | + Request |
| 35 | + .Builder() |
| 36 | + .url("https://square.com/".toHttpUrl()) |
| 37 | + .tag(Integer::class, 5 as Integer) |
| 38 | + .tag(String::class, "hello") |
| 39 | + .build() |
| 40 | + val call = client.newCall(request) |
| 41 | + |
| 42 | + // Check the Kotlin-focused APIs. |
| 43 | + assertThat(call.tag(String::class)).isEqualTo("hello") |
| 44 | + assertThat(call.tag(Integer::class)).isEqualTo(5) |
| 45 | + assertThat(call.tag(Boolean::class)).isNull() |
| 46 | + assertThat(call.tag(Any::class)).isNull() |
| 47 | + |
| 48 | + // Check the Java APIs too. |
| 49 | + assertThat(call.tag(String::class.java)).isEqualTo("hello") |
| 50 | + assertThat(call.tag(Integer::class.java)).isEqualTo(5) |
| 51 | + assertThat(call.tag(Boolean::class.java)).isNull() |
| 52 | + assertThat(call.tag(Any::class.java)).isNull() |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun tagsCanBeComputed() { |
| 57 | + val request = |
| 58 | + Request |
| 59 | + .Builder() |
| 60 | + .url("https://square.com") |
| 61 | + .build() |
| 62 | + val call = client.newCall(request) |
| 63 | + |
| 64 | + // Check the Kotlin-focused APIs. |
| 65 | + assertThat(call.tag(String::class) { "a" }).isEqualTo("a") |
| 66 | + assertThat(call.tag(String::class) { "b" }).isEqualTo("a") |
| 67 | + assertThat(call.tag(String::class)).isEqualTo("a") |
| 68 | + |
| 69 | + // Check the Java-focused APIs. |
| 70 | + assertThat(call.tag(Integer::class) { 1 as Integer }).isEqualTo(1) |
| 71 | + assertThat(call.tag(Integer::class) { 2 as Integer }).isEqualTo(1) |
| 72 | + assertThat(call.tag(Integer::class)).isEqualTo(1) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun computedTagsAreNotRetainedInClone() { |
| 77 | + val request = |
| 78 | + Request |
| 79 | + .Builder() |
| 80 | + .url("https://square.com") |
| 81 | + .build() |
| 82 | + val callA = client.newCall(request) |
| 83 | + assertThat(callA.tag(String::class) { "a" }).isEqualTo("a") |
| 84 | + assertThat(callA.tag(String::class) { "b" }).isEqualTo("a") |
| 85 | + |
| 86 | + val callB = callA.clone() |
| 87 | + assertThat(callB.tag(String::class) { "c" }).isEqualTo("c") |
| 88 | + assertThat(callB.tag(String::class) { "d" }).isEqualTo("c") |
| 89 | + } |
| 90 | +} |
0 commit comments