Skip to content

Commit

Permalink
Client content negotiation: test new features
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketraman committed Nov 7, 2024
1 parent aad0072 commit cf1cc78
Showing 1 changed file with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,58 @@ class ContentNegotiationTests {
}
}

@Test
fun addAcceptHeadersWithDefaultQValue() {
testWithEngine(MockEngine) {
val registeredTypesToSend = listOf(
ContentType("testing", "a"),
ContentType("testing", "b"),
ContentType("testing", "c")
)

setupWithContentNegotiation {
for (typeToSend in registeredTypesToSend) {
register(typeToSend, TestContentConverter())
defaultAcceptHeaderQValue = "0.8"
}
}

test { client ->
client.get("https://test.com/").apply {
val sentTypes = assertNotNull(call.request.headers.getAll(HttpHeaders.Accept))
.map { ContentType.parse(it) }

// Order NOT tested
for (typeToSend in registeredTypesToSend) {
assertContains(sentTypes, typeToSend.withParameter("q", "0.8"))
}
}
}
}
}

@Test
fun skipAddAcceptHeadersWithMatchingContentType() {
testWithEngine(MockEngine) {
setupWithContentNegotiation {
register(ContentType("testing", "a"), TestContentConverter())
}

test { client ->
client.get("https://test.com/") {
// our explicitly specified lower q-value should take precedence
accept(ContentType("testing", "a", listOf(HeaderValueParam("q", "0.5"))))
}.apply {
val sentTypes = assertNotNull(call.request.headers.getAll(HttpHeaders.Accept))
.map { ContentType.parse(it) }

assertContains(sentTypes, ContentType("testing", "a", listOf(HeaderValueParam("q", "0.5"))))
assertEquals(1, sentTypes.size)
}
}
}
}

@Test
fun testKeepsContentType() {
testWithEngine(MockEngine) {
Expand Down

0 comments on commit cf1cc78

Please sign in to comment.