RawGRPCServerResponsePart+Assertions.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright 2019, 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 Foundation
  17. import GRPC
  18. import NIOHTTP1
  19. import XCTest
  20. extension RawGRPCServerResponsePart {
  21. /// Asserts that this value represents the headers case.
  22. ///
  23. /// - Parameter validate: A block to further validate the headers.
  24. func assertHeaders(validate: ((HTTPHeaders) -> Void)? = nil) {
  25. guard case .headers(let headers) = self else {
  26. XCTFail("Expected .headers but got \(self)")
  27. return
  28. }
  29. validate?(headers)
  30. }
  31. /// Asserts that this value represents the message case.
  32. ///
  33. /// - Parameter validate: A block to further validate the message.
  34. func assertMessage(validate: ((Data) -> Void)? = nil) {
  35. guard case .message(let message) = self else {
  36. XCTFail("Expected .message but got \(self)")
  37. return
  38. }
  39. validate?(message)
  40. }
  41. /// Asserts that this value represents the status case.
  42. ///
  43. /// - Parameter validate: A block to further validate the status.
  44. func assertStatus(validate: ((GRPCStatus) -> Void)? = nil) {
  45. guard case let .statusAndTrailers(status, _) = self else {
  46. XCTFail("Expected .status but got \(self)")
  47. return
  48. }
  49. validate?(status)
  50. }
  51. }