Skip to content

Commit 4821d5d

Browse files
committed
Add Accept header tests
1 parent bb7e16a commit 4821d5d

File tree

2 files changed

+120
-0
lines changed
  • codegen
    • smithy-aws-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/aws/protocols
    • smithy-kotlin-codegen-testutils/src/main/kotlin/software/amazon/smithy/kotlin/codegen/test

2 files changed

+120
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package software.amazon.smithy.kotlin.codegen.aws.protocols
6+
7+
import software.amazon.smithy.kotlin.codegen.test.*
8+
import kotlin.test.Test
9+
10+
class RpcV2CborTest {
11+
val model = """
12+
${"$"}version: "2"
13+
14+
namespace com.test
15+
16+
use smithy.protocols#rpcv2Cbor
17+
use aws.api#service
18+
19+
@rpcv2Cbor
20+
@service(sdkId: "CborExample")
21+
service CborExample {
22+
version: "1.0.0",
23+
operations: [GetFoo, GetFooStreaming]
24+
}
25+
26+
@http(method: "POST", uri: "/foo")
27+
operation GetFoo {}
28+
29+
@http(method: "POST", uri: "/foo-streaming")
30+
operation GetFooStreaming {
31+
input := {}
32+
output := {
33+
events: FooEvents
34+
}
35+
}
36+
37+
// Model taken from https://smithy.io/2.0/spec/streaming.html#event-streams
38+
@streaming
39+
union FooEvents {
40+
up: Movement
41+
down: Movement
42+
left: Movement
43+
right: Movement
44+
throttlingError: ThrottlingError
45+
}
46+
47+
structure Movement {
48+
velocity: Float
49+
}
50+
51+
@error("client")
52+
@retryable(throttling: true)
53+
structure ThrottlingError {}
54+
""".toSmithyModel()
55+
56+
@Test
57+
fun testStandardAcceptHeader() {
58+
val ctx = model.newTestContext("CborExample")
59+
60+
val generator = RpcV2Cbor()
61+
generator.generateProtocolClient(ctx.generationCtx)
62+
63+
ctx.generationCtx.delegator.finalize()
64+
ctx.generationCtx.delegator.flushWriters()
65+
66+
val actual = ctx.manifest.expectFileString("/src/main/kotlin/com/test/DefaultTestClient.kt")
67+
println(actual)
68+
val getFooMethod = actual.lines(" override suspend fun getFoo(input: GetFooRequest): GetFooResponse {", " }")
69+
70+
val expectedHeaderMutation = """
71+
op.install(
72+
MutateHeaders().apply {
73+
append("Accept", "application/cbor")
74+
}
75+
)
76+
""".replaceIndent(" ")
77+
getFooMethod.shouldContainOnlyOnceWithDiff(expectedHeaderMutation)
78+
}
79+
80+
@Test
81+
fun testEventStreamAcceptHeader() {
82+
val ctx = model.newTestContext("CborExample")
83+
84+
val generator = RpcV2Cbor()
85+
generator.generateProtocolClient(ctx.generationCtx)
86+
87+
ctx.generationCtx.delegator.finalize()
88+
ctx.generationCtx.delegator.flushWriters()
89+
90+
val actual = ctx.manifest.expectFileString("/src/main/kotlin/com/test/DefaultTestClient.kt")
91+
println(actual)
92+
val getFooMethod = actual.lines(" override suspend fun <T> getFooStreaming(input: GetFooStreamingRequest, block: suspend (GetFooStreamingResponse) -> T): T {", " }")
93+
94+
val expectedHeaderMutation = """
95+
op.install(
96+
MutateHeaders().apply {
97+
append("Accept", "application/vnd.amazon.eventstream")
98+
}
99+
)
100+
""".replaceIndent(" ")
101+
getFooMethod.shouldContainOnlyOnceWithDiff(expectedHeaderMutation)
102+
}
103+
}

codegen/smithy-kotlin-codegen-testutils/src/main/kotlin/software/amazon/smithy/kotlin/codegen/test/CodegenTestUtils.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import software.amazon.smithy.model.traits.TimestampFormatTrait
2828
import software.amazon.smithy.model.traits.Trait
2929
import software.amazon.smithy.protocol.traits.Rpcv2CborTrait
3030
import software.amazon.smithy.utils.StringUtils
31+
import kotlin.test.assertNotEquals
3132

3233
// This file houses test classes and functions relating to the code generator (protocols, serializers, etc)
3334
// Items contained here should be relatively high-level, utilizing all members of codegen classes, Smithy, and
@@ -274,3 +275,19 @@ fun KotlinCodegenPlugin.Companion.createSymbolProvider(
274275
* create a new [KotlinWriter] using the test context package name
275276
*/
276277
fun TestContext.newWriter(): KotlinWriter = KotlinWriter(generationCtx.settings.pkg.name)
278+
279+
/**
280+
* Get all the lines between [fromLine] and [toLine]
281+
*/
282+
fun String.lines(fromLine: String, toLine: String): String {
283+
val allLines = lines()
284+
285+
val fromIdx = allLines.indexOf(fromLine)
286+
assertNotEquals(-1, fromIdx, """Could not find from line "$fromLine" in all lines""")
287+
288+
val toIdxOffset = allLines.drop(fromIdx + 1).indexOf(toLine)
289+
assertNotEquals(-1, toIdxOffset, """Could not find to line "$toLine" in all lines""")
290+
291+
val toIdx = toIdxOffset + fromIdx + 1
292+
return allLines.subList(fromIdx, toIdx + 1).joinToString("\n")
293+
}

0 commit comments

Comments
 (0)