ServerTestExample.swift 3.2 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. 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. XCTAssertEqual(Echo_EchoResponse(text: "Swift echo collect: foo bar baz"), try provider.collect(session: session)!)
  56. }
  57. func testServerStreaming() {
  58. let session = Echo_EchoExpandSessionTestStub()
  59. XCTAssertEqual(.ok, try provider.expand(request: Echo_EchoRequest(text: "foo bar baz"), session: session)!.code)
  60. XCTAssertEqual(["foo", "bar", "baz"].enumerated()
  61. .map { Echo_EchoResponse(text: "Swift echo expand (\($0)): \($1)") },
  62. session.outputs)
  63. }
  64. func testBidirectionalStreaming() {
  65. let inputStrings = ["foo", "bar", "baz"]
  66. let session = Echo_EchoUpdateSessionTestStub()
  67. session.inputs = inputStrings.map { Echo_EchoRequest(text: $0) }
  68. XCTAssertEqual(.ok, try! provider.update(session: session)!.code)
  69. XCTAssertEqual(inputStrings.enumerated()
  70. .map { Echo_EchoResponse(text: "Swift echo update (\($0)): \($1)") },
  71. session.outputs)
  72. }
  73. }