Browse Source

Add request compression check to interop's TestService (#1903)

Gustavo Cairo 1 year ago
parent
commit
7875e56cd6
1 changed files with 24 additions and 0 deletions
  1. 24 0
      Sources/InteroperabilityTests/TestService.swift

+ 24 - 0
Sources/InteroperabilityTests/TestService.swift

@@ -51,6 +51,18 @@ public struct TestService: Grpc_Testing_TestService.ServiceProtocol {
   public func unaryCall(
     request: ServerRequest.Single<Grpc_Testing_TestService.Method.UnaryCall.Input>
   ) async throws -> ServerResponse.Single<Grpc_Testing_TestService.Method.UnaryCall.Output> {
+    // We can't validate messages at the wire-encoding layer (i.e. where the compression byte is
+    // set), so we have to check via the encoding header. Note that it is possible for the header
+    // to be set and for the message to not be compressed.
+    let isRequestCompressed =
+      request.metadata["grpc-encoding"].filter({ $0 != "identity" }).count > 0
+    if request.message.expectCompressed.value, !isRequestCompressed {
+      throw RPCError(
+        code: .invalidArgument,
+        message: "Expected compressed request, but 'grpc-encoding' was missing"
+      )
+    }
+
     // If the request has a responseStatus set, the server should return that status.
     // If the code is an error code, the server will throw an error containing that code
     // and the message set in the responseStatus.
@@ -139,9 +151,21 @@ public struct TestService: Grpc_Testing_TestService.ServiceProtocol {
   ) async throws
     -> ServerResponse.Single<Grpc_Testing_TestService.Method.StreamingInputCall.Output>
   {
+    let isRequestCompressed =
+      request.metadata["grpc-encoding"].filter({ $0 != "identity" }).count > 0
     var aggregatedPayloadSize = 0
 
     for try await message in request.messages {
+      // We can't validate messages at the wire-encoding layer (i.e. where the compression byte is
+      // set), so we have to check via the encoding header. Note that it is possible for the header
+      // to be set and for the message to not be compressed.
+      if message.expectCompressed.value, !isRequestCompressed {
+        throw RPCError(
+          code: .invalidArgument,
+          message: "Expected compressed request, but 'grpc-encoding' was missing"
+        )
+      }
+
       aggregatedPayloadSize += message.payload.body.count
     }