RawGRPCServerResponsePart+Assertions.swift 1.7 KB

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