Skip to content

Commit a060296

Browse files
Echo Metadata in TestService (#1824)
Motivation: We want to be able to test that the server receives metadata and can send both initial and trailing metadata. Modifications: - added extensions for server requests that extract the initial and trailing metadata that should be echoed - added the initial and trailing metadata n the server responses Result: The interop tests will test that the server sends metadata as initial and trailing.
1 parent ec5b396 commit a060296

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

Sources/InteroperabilityTests/TestService.swift

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ internal struct TestService: Grpc_Testing_TestService.ServiceProtocol {
3232
request: ServerRequest.Single<Grpc_Testing_TestService.Method.EmptyCall.Input>
3333
) async throws -> ServerResponse.Single<Grpc_Testing_TestService.Method.EmptyCall.Output> {
3434
let message = Grpc_Testing_TestService.Method.EmptyCall.Output()
35-
return ServerResponse.Single(message: message)
35+
let (initialMetadata, trailingMetadata) = request.metadata.makeInitialAndTrailingMetadata()
36+
return ServerResponse.Single(
37+
message: message,
38+
metadata: initialMetadata,
39+
trailingMetadata: trailingMetadata
40+
)
3641
}
3742

3843
/// Server implements `unaryCall` which immediately returns a `SimpleResponse` with a payload
@@ -72,7 +77,13 @@ internal struct TestService: Grpc_Testing_TestService.ServiceProtocol {
7277
}
7378
}
7479

75-
return ServerResponse.Single(message: responseMessage)
80+
let (initialMetadata, trailingMetadata) = request.metadata.makeInitialAndTrailingMetadata()
81+
82+
return ServerResponse.Single(
83+
message: responseMessage,
84+
metadata: initialMetadata,
85+
trailingMetadata: trailingMetadata
86+
)
7687
}
7788

7889
/// Server gets the default `SimpleRequest` proto as the request. The content of the request is
@@ -102,7 +113,8 @@ internal struct TestService: Grpc_Testing_TestService.ServiceProtocol {
102113
) async throws
103114
-> ServerResponse.Stream<Grpc_Testing_TestService.Method.StreamingOutputCall.Output>
104115
{
105-
return ServerResponse.Stream { writer in
116+
let (initialMetadata, trailingMetadata) = request.metadata.makeInitialAndTrailingMetadata()
117+
return ServerResponse.Stream(metadata: initialMetadata) { writer in
106118
for responseParameter in request.message.responseParameters {
107119
let response = Grpc_Testing_StreamingOutputCallResponse.with { response in
108120
response.payload = Grpc_Testing_Payload.with { payload in
@@ -113,7 +125,7 @@ internal struct TestService: Grpc_Testing_TestService.ServiceProtocol {
113125
// We convert the `intervalUs` value from microseconds to nanoseconds.
114126
try await Task.sleep(nanoseconds: UInt64(responseParameter.intervalUs) * 1000)
115127
}
116-
return [:]
128+
return trailingMetadata
117129
}
118130
}
119131

@@ -135,7 +147,12 @@ internal struct TestService: Grpc_Testing_TestService.ServiceProtocol {
135147
$0.aggregatedPayloadSize = Int32(aggregatedPayloadSize)
136148
}
137149

138-
return ServerResponse.Single(message: responseMessage)
150+
let (initialMetadata, trailingMetadata) = request.metadata.makeInitialAndTrailingMetadata()
151+
return ServerResponse.Single(
152+
message: responseMessage,
153+
metadata: initialMetadata,
154+
trailingMetadata: trailingMetadata
155+
)
139156
}
140157

141158
/// Server implements `fullDuplexCall` by replying, in order, with one
@@ -148,7 +165,8 @@ internal struct TestService: Grpc_Testing_TestService.ServiceProtocol {
148165
) async throws
149166
-> ServerResponse.Stream<Grpc_Testing_TestService.Method.FullDuplexCall.Output>
150167
{
151-
return ServerResponse.Stream { writer in
168+
let (initialMetadata, trailingMetadata) = request.metadata.makeInitialAndTrailingMetadata()
169+
return ServerResponse.Stream(metadata: initialMetadata) { writer in
152170
for try await message in request.messages {
153171
// If a request message has a responseStatus set, the server should return that status.
154172
// If the code is an error code, the server will throw an error containing that code
@@ -174,7 +192,7 @@ internal struct TestService: Grpc_Testing_TestService.ServiceProtocol {
174192
try await writer.write(response)
175193
}
176194
}
177-
return [:]
195+
return trailingMetadata
178196
}
179197
}
180198

@@ -189,3 +207,18 @@ internal struct TestService: Grpc_Testing_TestService.ServiceProtocol {
189207
throw RPCError(code: .unimplemented, message: "The RPC is not implemented.")
190208
}
191209
}
210+
211+
extension Metadata {
212+
fileprivate func makeInitialAndTrailingMetadata() -> (Metadata, Metadata) {
213+
var initialMetadata = Metadata()
214+
var trailingMetadata = Metadata()
215+
for value in self[stringValues: "x-grpc-test-echo-initial"] {
216+
initialMetadata.addString(value, forKey: "x-grpc-test-echo-initial")
217+
}
218+
for value in self[binaryValues: "x-grpc-test-echo-trailing-bin"] {
219+
trailingMetadata.addBinary(value, forKey: "x-grpc-test-echo-trailing-bin")
220+
}
221+
222+
return (initialMetadata, trailingMetadata)
223+
}
224+
}

0 commit comments

Comments
 (0)