ServerTestExample.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. import Dispatch
  17. import Foundation
  18. @testable import SwiftGRPC
  19. import XCTest
  20. // Sample test suite to demonstrate how one would test a `Provider` implementation.
  21. // These tests don't really test the logic of the SwiftGRPC library, but are meant
  22. // as an example of how one would go about testing their own client/server code that
  23. // relies on SwiftGRPC.
  24. class ServerTestExample: XCTestCase {
  25. static var allTests: [(String, (ServerTestExample) -> () throws -> Void)] {
  26. return [
  27. ("testUnary", testUnary),
  28. ("testClientStreaming", testClientStreaming),
  29. ("testServerStreaming", testServerStreaming),
  30. ("testBidirectionalStreaming", testBidirectionalStreaming)
  31. ]
  32. }
  33. var provider: Echo_EchoProvider!
  34. override func setUp() {
  35. super.setUp()
  36. provider = EchoProvider()
  37. }
  38. override func tearDown() {
  39. provider = nil
  40. super.tearDown()
  41. }
  42. }
  43. extension ServerTestExample {
  44. func testUnary() {
  45. XCTAssertEqual(Echo_EchoResponse(text: "Swift echo get: "),
  46. try provider.get(request: Echo_EchoRequest(text: ""), session: Echo_EchoGetSessionTestStub()))
  47. XCTAssertEqual(Echo_EchoResponse(text: "Swift echo get: foo"),
  48. try provider.get(request: Echo_EchoRequest(text: "foo"), session: Echo_EchoGetSessionTestStub()))
  49. XCTAssertEqual(Echo_EchoResponse(text: "Swift echo get: foo bar"),
  50. try provider.get(request: Echo_EchoRequest(text: "foo bar"), session: Echo_EchoGetSessionTestStub()))
  51. }
  52. func testClientStreaming() {
  53. let session = Echo_EchoCollectSessionTestStub()
  54. session.inputs = ["foo", "bar", "baz"].map { Echo_EchoRequest(text: $0) }
  55. XCTAssertNoThrow(try provider.collect(session: session))
  56. XCTAssertEqual(.ok, session.status!.code)
  57. XCTAssertEqual(Echo_EchoResponse(text: "Swift echo collect: foo bar baz"),
  58. session.output)
  59. }
  60. func testServerStreaming() {
  61. let session = Echo_EchoExpandSessionTestStub()
  62. XCTAssertNoThrow(try provider.expand(request: Echo_EchoRequest(text: "foo bar baz"), session: session))
  63. XCTAssertEqual(.ok, session.status!.code)
  64. XCTAssertEqual(["foo", "bar", "baz"].enumerated()
  65. .map { Echo_EchoResponse(text: "Swift echo expand (\($0)): \($1)") },
  66. session.outputs)
  67. }
  68. func testBidirectionalStreaming() {
  69. let inputStrings = ["foo", "bar", "baz"]
  70. let session = Echo_EchoUpdateSessionTestStub()
  71. session.inputs = inputStrings.map { Echo_EchoRequest(text: $0) }
  72. XCTAssertNoThrow(try provider.update(session: session))
  73. XCTAssertEqual(.ok, session.status!.code)
  74. XCTAssertEqual(inputStrings.enumerated()
  75. .map { Echo_EchoResponse(text: "Swift echo update (\($0)): \($1)") },
  76. session.outputs)
  77. }
  78. }