ChannelArgumentTests.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2018, 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. #if SWIFT_PACKAGE
  17. import CgRPC
  18. #endif
  19. import Foundation
  20. @testable import SwiftGRPC
  21. import XCTest
  22. fileprivate class ChannelArgumentTestProvider: Echo_EchoProvider {
  23. func get(request: Echo_EchoRequest, session: Echo_EchoGetSession) throws -> Echo_EchoResponse {
  24. // We simply return the user agent we received, which can then be inspected by the test code.
  25. return Echo_EchoResponse(text: (session as! ServerSessionBase).handler.requestMetadata["user-agent"]!)
  26. }
  27. func expand(request: Echo_EchoRequest, session: Echo_EchoExpandSession) throws -> ServerStatus? {
  28. fatalError("not implemented")
  29. }
  30. func collect(session: Echo_EchoCollectSession) throws -> Echo_EchoResponse? {
  31. fatalError("not implemented")
  32. }
  33. func update(session: Echo_EchoUpdateSession) throws -> ServerStatus? {
  34. fatalError("not implemented")
  35. }
  36. }
  37. class ChannelArgumentTests: BasicEchoTestCase {
  38. static var allTests: [(String, (ChannelArgumentTests) -> () throws -> Void)] {
  39. return [
  40. ("testArgumentKey", testArgumentKey),
  41. ("testStringArgument", testStringArgument),
  42. ("testIntegerArgument", testIntegerArgument),
  43. ("testBoolArgument", testBoolArgument),
  44. ("testTimeIntervalArgument", testTimeIntervalArgument),
  45. ]
  46. }
  47. fileprivate func makeClient(_ arguments: [Channel.Argument]) -> Echo_EchoServiceClient {
  48. let client = Echo_EchoServiceClient(address: address, secure: false, arguments: arguments)
  49. client.timeout = defaultTimeout
  50. return client
  51. }
  52. override func makeProvider() -> Echo_EchoProvider { return ChannelArgumentTestProvider() }
  53. }
  54. extension ChannelArgumentTests {
  55. func testArgumentKey() {
  56. let argument = Channel.Argument.defaultAuthority("default")
  57. XCTAssertEqual(String(cString: argument.toCArg().wrapped.key), "grpc.default_authority")
  58. }
  59. func testStringArgument() {
  60. let argument = Channel.Argument.primaryUserAgent("Primary/0.1")
  61. XCTAssertEqual(String(cString: argument.toCArg().wrapped.value.string), "Primary/0.1")
  62. }
  63. func testIntegerArgument() {
  64. let argument = Channel.Argument.http2MaxPingsWithoutData(5)
  65. XCTAssertEqual(argument.toCArg().wrapped.value.integer, 5)
  66. }
  67. func testBoolArgument() {
  68. let argument = Channel.Argument.keepAlivePermitWithoutCalls(true)
  69. XCTAssertEqual(argument.toCArg().wrapped.value.integer, 1)
  70. }
  71. func testTimeIntervalArgument() {
  72. let argument = Channel.Argument.keepAliveTime(2.5)
  73. XCTAssertEqual(argument.toCArg().wrapped.value.integer, 2500) // in ms
  74. }
  75. }
  76. extension ChannelArgumentTests {
  77. func testPracticalUse() {
  78. let client = makeClient([.primaryUserAgent("FOO"), .secondaryUserAgent("BAR")])
  79. let responseText = try! client.get(Echo_EchoRequest(text: "")).text
  80. XCTAssertTrue(responseText.hasPrefix("FOO "), "user agent \(responseText) should begin with 'FOO '")
  81. XCTAssertTrue(responseText.hasSuffix(" BAR"), "user agent \(responseText) should end with ' BAR'")
  82. }
  83. }