ChannelArgumentTests.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. fileprivate func makeClient(_ arguments: [Channel.Argument]) -> Echo_EchoServiceClient {
  39. let client = Echo_EchoServiceClient(address: address, secure: false, arguments: arguments)
  40. client.timeout = defaultTimeout
  41. return client
  42. }
  43. override func makeProvider() -> Echo_EchoProvider { return ChannelArgumentTestProvider() }
  44. }
  45. extension ChannelArgumentTests {
  46. func testArgumentKey() {
  47. let argument = Channel.Argument.defaultAuthority("default")
  48. XCTAssertEqual(String(cString: argument.toCArg().wrapped.key), "grpc.default_authority")
  49. }
  50. func testStringArgument() {
  51. let argument = Channel.Argument.primaryUserAgent("Primary/0.1")
  52. XCTAssertEqual(String(cString: argument.toCArg().wrapped.value.string), "Primary/0.1")
  53. }
  54. func testIntegerArgument() {
  55. let argument = Channel.Argument.http2MaxPingsWithoutData(5)
  56. XCTAssertEqual(argument.toCArg().wrapped.value.integer, 5)
  57. }
  58. func testBoolArgument() {
  59. let argument = Channel.Argument.keepAlivePermitWithoutCalls(true)
  60. XCTAssertEqual(argument.toCArg().wrapped.value.integer, 1)
  61. }
  62. func testTimeIntervalArgument() {
  63. let argument = Channel.Argument.keepAliveTime(2.5)
  64. XCTAssertEqual(argument.toCArg().wrapped.value.integer, 2500) // in ms
  65. }
  66. }
  67. extension ChannelArgumentTests {
  68. func testPracticalUse() {
  69. let client = makeClient([.primaryUserAgent("FOO"), .secondaryUserAgent("BAR")])
  70. let responseText = try! client.get(Echo_EchoRequest(text: "")).text
  71. XCTAssertTrue(responseText.hasPrefix("FOO "), "user agent \(responseText) should begin with 'FOO '")
  72. XCTAssertTrue(responseText.hasSuffix(" BAR"), "user agent \(responseText) should end with ' BAR'")
  73. }
  74. }