ServerTestExample.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. var provider: Echo_EchoProvider!
  26. override func setUp() {
  27. super.setUp()
  28. provider = EchoProvider()
  29. }
  30. override func tearDown() {
  31. provider = nil
  32. super.tearDown()
  33. }
  34. }
  35. extension ServerTestExample {
  36. func testUnary() {
  37. XCTAssertEqual(Echo_EchoResponse(text: "Swift echo get: "),
  38. try provider.get(request: Echo_EchoRequest(text: ""), session: Echo_EchoGetSessionTestStub()))
  39. XCTAssertEqual(Echo_EchoResponse(text: "Swift echo get: foo"),
  40. try provider.get(request: Echo_EchoRequest(text: "foo"), session: Echo_EchoGetSessionTestStub()))
  41. XCTAssertEqual(Echo_EchoResponse(text: "Swift echo get: foo bar"),
  42. try provider.get(request: Echo_EchoRequest(text: "foo bar"), session: Echo_EchoGetSessionTestStub()))
  43. }
  44. func testClientStreaming() {
  45. let session = Echo_EchoCollectSessionTestStub()
  46. session.inputs = ["foo", "bar", "baz"].map { Echo_EchoRequest(text: $0) }
  47. XCTAssertEqual(Echo_EchoResponse(text: "Swift echo collect: foo bar baz"), try provider.collect(session: session)!)
  48. }
  49. func testServerStreaming() {
  50. let session = Echo_EchoExpandSessionTestStub()
  51. XCTAssertEqual(.ok, try provider.expand(request: Echo_EchoRequest(text: "foo bar baz"), session: session)!.code)
  52. XCTAssertEqual(["foo", "bar", "baz"].enumerated()
  53. .map { Echo_EchoResponse(text: "Swift echo expand (\($0)): \($1)") },
  54. session.outputs)
  55. }
  56. func testBidirectionalStreaming() {
  57. let inputStrings = ["foo", "bar", "baz"]
  58. let session = Echo_EchoUpdateSessionTestStub()
  59. session.inputs = inputStrings.map { Echo_EchoRequest(text: $0) }
  60. XCTAssertEqual(.ok, try! provider.update(session: session)!.code)
  61. XCTAssertEqual(inputStrings.enumerated()
  62. .map { Echo_EchoResponse(text: "Swift echo update (\($0)): \($1)") },
  63. session.outputs)
  64. }
  65. }