HeaderNormalizationTests.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import EchoImplementation
  17. import EchoModel
  18. @testable import GRPC
  19. import NIO
  20. import NIOHPACK
  21. import NIOHTTP1
  22. import XCTest
  23. class EchoMetadataValidator: Echo_EchoProvider {
  24. let interceptors: Echo_EchoServerInterceptorFactoryProtocol? = nil
  25. private func assertCustomMetadataIsLowercased(
  26. _ headers: HPACKHeaders,
  27. line: UInt = #line
  28. ) {
  29. // Header lookup is case-insensitive so we need to pull out the values we know the client sent
  30. // as custom-metadata and then compare a new set of headers.
  31. let customMetadata = HPACKHeaders(headers.filter { _, value, _ in
  32. value == "client"
  33. }.map {
  34. ($0.name, $0.value)
  35. })
  36. XCTAssertEqual(customMetadata, ["client": "client"], line: line)
  37. }
  38. func get(
  39. request: Echo_EchoRequest,
  40. context: StatusOnlyCallContext
  41. ) -> EventLoopFuture<Echo_EchoResponse> {
  42. self.assertCustomMetadataIsLowercased(context.headers)
  43. context.trailers.add(name: "SERVER", value: "server")
  44. return context.eventLoop.makeSucceededFuture(.with { $0.text = request.text })
  45. }
  46. func expand(
  47. request: Echo_EchoRequest,
  48. context: StreamingResponseCallContext<Echo_EchoResponse>
  49. ) -> EventLoopFuture<GRPCStatus> {
  50. self.assertCustomMetadataIsLowercased(context.headers)
  51. context.trailers.add(name: "SERVER", value: "server")
  52. return context.eventLoop.makeSucceededFuture(.ok)
  53. }
  54. func collect(
  55. context: UnaryResponseCallContext<Echo_EchoResponse>
  56. ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  57. self.assertCustomMetadataIsLowercased(context.headers)
  58. context.trailers.add(name: "SERVER", value: "server")
  59. return context.eventLoop.makeSucceededFuture({ event in
  60. switch event {
  61. case .message:
  62. ()
  63. case .end:
  64. context.responsePromise.succeed(.with { $0.text = "foo" })
  65. }
  66. })
  67. }
  68. func update(
  69. context: StreamingResponseCallContext<Echo_EchoResponse>
  70. ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  71. self.assertCustomMetadataIsLowercased(context.headers)
  72. context.trailers.add(name: "SERVER", value: "server")
  73. return context.eventLoop.makeSucceededFuture({ event in
  74. switch event {
  75. case .message:
  76. ()
  77. case .end:
  78. context.statusPromise.succeed(.ok)
  79. }
  80. })
  81. }
  82. }
  83. class HeaderNormalizationTests: GRPCTestCase {
  84. var group: EventLoopGroup!
  85. var server: Server!
  86. var channel: GRPCChannel!
  87. var client: Echo_EchoClient!
  88. override func setUp() {
  89. super.setUp()
  90. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  91. self.server = try! Server.insecure(group: self.group)
  92. .withServiceProviders([EchoMetadataValidator()])
  93. .bind(host: "localhost", port: 0)
  94. .wait()
  95. self.channel = ClientConnection.insecure(group: self.group)
  96. .connect(host: "localhost", port: self.server.channel.localAddress!.port!)
  97. self.client = Echo_EchoClient(channel: self.channel)
  98. }
  99. override func tearDown() {
  100. XCTAssertNoThrow(try self.channel.close().wait())
  101. XCTAssertNoThrow(try self.server.close().wait())
  102. XCTAssertNoThrow(try self.group.syncShutdownGracefully())
  103. super.tearDown()
  104. }
  105. private func assertCustomMetadataIsLowercased(
  106. _ headers: EventLoopFuture<HPACKHeaders>,
  107. expectation: XCTestExpectation,
  108. file: StaticString = #file,
  109. line: UInt = #line
  110. ) {
  111. // Header lookup is case-insensitive so we need to pull out the values we know the server sent
  112. // us as trailing-metadata and then compare a new set of headers.
  113. headers.map { trailers -> HPACKHeaders in
  114. let filtered = trailers.filter {
  115. $0.value == "server"
  116. }.map { name, value, _ in
  117. (name, value)
  118. }
  119. return HPACKHeaders(filtered)
  120. }.assertEqual(["server": "server"], fulfill: expectation, file: file, line: line)
  121. }
  122. func testHeadersAreNormalizedForUnary() throws {
  123. let trailingMetadata = self.expectation(description: "received trailing metadata")
  124. let options = CallOptions(customMetadata: ["CLIENT": "client"])
  125. let rpc = self.client.get(.with { $0.text = "foo" }, callOptions: options)
  126. self.assertCustomMetadataIsLowercased(rpc.trailingMetadata, expectation: trailingMetadata)
  127. self.wait(for: [trailingMetadata], timeout: 1.0)
  128. }
  129. func testHeadersAreNormalizedForClientStreaming() throws {
  130. let trailingMetadata = self.expectation(description: "received trailing metadata")
  131. let options = CallOptions(customMetadata: ["CLIENT": "client"])
  132. let rpc = self.client.collect(callOptions: options)
  133. rpc.sendEnd(promise: nil)
  134. self.assertCustomMetadataIsLowercased(rpc.trailingMetadata, expectation: trailingMetadata)
  135. self.wait(for: [trailingMetadata], timeout: 1.0)
  136. }
  137. func testHeadersAreNormalizedForServerStreaming() throws {
  138. let trailingMetadata = self.expectation(description: "received trailing metadata")
  139. let options = CallOptions(customMetadata: ["CLIENT": "client"])
  140. let rpc = self.client.expand(.with { $0.text = "foo" }, callOptions: options) {
  141. XCTFail("unexpected response: \($0)")
  142. }
  143. self.assertCustomMetadataIsLowercased(rpc.trailingMetadata, expectation: trailingMetadata)
  144. self.wait(for: [trailingMetadata], timeout: 1.0)
  145. }
  146. func testHeadersAreNormalizedForBidirectionalStreaming() throws {
  147. let trailingMetadata = self.expectation(description: "received trailing metadata")
  148. let options = CallOptions(customMetadata: ["CLIENT": "client"])
  149. let rpc = self.client.update(callOptions: options) {
  150. XCTFail("unexpected response: \($0)")
  151. }
  152. rpc.sendEnd(promise: nil)
  153. self.assertCustomMetadataIsLowercased(rpc.trailingMetadata, expectation: trailingMetadata)
  154. self.wait(for: [trailingMetadata], timeout: 1.0)
  155. }
  156. }