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+ }
0 commit comments