GRPCTestingConvenienceMethods.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 SwiftProtobuf
  18. import NIOHTTP1
  19. // MARK: - Payload creation
  20. extension Grpc_Testing_Payload {
  21. static func bytes<T>(of body: inout T) -> Grpc_Testing_Payload {
  22. return Grpc_Testing_Payload.with { payload in
  23. payload.body = Data(bytes: &body, count: MemoryLayout.size(ofValue: body))
  24. }
  25. }
  26. static func zeros(count: Int) -> Grpc_Testing_Payload {
  27. return Grpc_Testing_Payload.with { payload in
  28. payload.body = Data(repeating: 0, count: count)
  29. }
  30. }
  31. }
  32. // MARK: - Echo status creation
  33. extension Grpc_Testing_EchoStatus {
  34. init(code: Int32, message: String) {
  35. self.code = code
  36. self.message = message
  37. }
  38. }
  39. // MARK: - Response Parameter creation
  40. extension Grpc_Testing_ResponseParameters {
  41. static func size(_ size: Int) -> Grpc_Testing_ResponseParameters {
  42. return Grpc_Testing_ResponseParameters.with { parameters in
  43. parameters.size = numericCast(size)
  44. }
  45. }
  46. }
  47. // MARK: - Echo status
  48. protocol EchoStatusRequest: Message {
  49. var responseStatus: Grpc_Testing_EchoStatus { get set }
  50. }
  51. extension EchoStatusRequest {
  52. var shouldEchoStatus: Bool {
  53. return self.responseStatus != Grpc_Testing_EchoStatus()
  54. }
  55. }
  56. extension Grpc_Testing_SimpleRequest: EchoStatusRequest { }
  57. extension Grpc_Testing_StreamingOutputCallRequest: EchoStatusRequest { }
  58. // MARK: - Echo metadata
  59. extension HTTPHeaders {
  60. /// See `ServerFeatures.echoMetadata`.
  61. var shouldEchoMetadata: Bool {
  62. return self.contains(name: "x-grpc-test-echo-initial") || self.contains(name: "x-grpc-test-echo-trailing-bin")
  63. }
  64. }