GRPCTestingConvenienceMethods.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. import GRPCInteroperabilityTestModels
  20. // MARK: - Payload creation
  21. extension Grpc_Testing_Payload {
  22. static func bytes<T>(of body: inout T) -> Grpc_Testing_Payload {
  23. return Grpc_Testing_Payload.with { payload in
  24. payload.body = Data(bytes: &body, count: MemoryLayout.size(ofValue: body))
  25. }
  26. }
  27. static func zeros(count: Int) -> Grpc_Testing_Payload {
  28. return Grpc_Testing_Payload.with { payload in
  29. payload.body = Data(repeating: 0, count: count)
  30. }
  31. }
  32. }
  33. // MARK: - Echo status creation
  34. extension Grpc_Testing_EchoStatus {
  35. init(code: Int32, message: String) {
  36. self = .with {
  37. $0.code = code
  38. $0.message = message
  39. }
  40. }
  41. }
  42. // MARK: - Response Parameter creation
  43. extension Grpc_Testing_ResponseParameters {
  44. static func size(_ size: Int) -> Grpc_Testing_ResponseParameters {
  45. return Grpc_Testing_ResponseParameters.with { parameters in
  46. parameters.size = numericCast(size)
  47. }
  48. }
  49. }
  50. // MARK: - Echo status
  51. protocol EchoStatusRequest: Message {
  52. var responseStatus: Grpc_Testing_EchoStatus { get set }
  53. }
  54. extension EchoStatusRequest {
  55. var shouldEchoStatus: Bool {
  56. return self.responseStatus != Grpc_Testing_EchoStatus()
  57. }
  58. }
  59. extension EchoStatusRequest {
  60. static func withStatus(of status: Grpc_Testing_EchoStatus) -> Self {
  61. return Self.with { instance in
  62. instance.responseStatus = status
  63. }
  64. }
  65. }
  66. extension Grpc_Testing_SimpleRequest: EchoStatusRequest { }
  67. extension Grpc_Testing_StreamingOutputCallRequest: EchoStatusRequest { }
  68. // MARK: - Payload request
  69. protocol PayloadRequest: Message {
  70. var payload: Grpc_Testing_Payload { get set }
  71. }
  72. extension PayloadRequest {
  73. static func withPayload(of payload: Grpc_Testing_Payload) -> Self {
  74. return Self.with { instance in
  75. instance.payload = payload
  76. }
  77. }
  78. }
  79. extension Grpc_Testing_SimpleRequest: PayloadRequest { }
  80. extension Grpc_Testing_StreamingOutputCallRequest: PayloadRequest { }
  81. extension Grpc_Testing_StreamingInputCallRequest: PayloadRequest { }
  82. // MARK: - Echo metadata
  83. extension HTTPHeaders {
  84. /// See `ServerFeatures.echoMetadata`.
  85. var shouldEchoMetadata: Bool {
  86. return self.contains(name: "x-grpc-test-echo-initial") || self.contains(name: "x-grpc-test-echo-trailing-bin")
  87. }
  88. }