GRPCTestingConvenienceMethods.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 struct Foundation.Data
  17. import GRPCInteroperabilityTestModels
  18. import NIOHPACK
  19. import SwiftProtobuf
  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: - Bool value
  34. extension Grpc_Testing_BoolValue: ExpressibleByBooleanLiteral {
  35. public typealias BooleanLiteralType = Bool
  36. public init(booleanLiteral value: Bool) {
  37. self = .with {
  38. $0.value = value
  39. }
  40. }
  41. }
  42. // MARK: - Echo status creation
  43. extension Grpc_Testing_EchoStatus {
  44. init(code: Int32, message: String) {
  45. self = .with {
  46. $0.code = code
  47. $0.message = message
  48. }
  49. }
  50. }
  51. // MARK: - Response Parameter creation
  52. extension Grpc_Testing_ResponseParameters {
  53. static func size(_ size: Int) -> Grpc_Testing_ResponseParameters {
  54. return Grpc_Testing_ResponseParameters.with { parameters in
  55. parameters.size = numericCast(size)
  56. }
  57. }
  58. }
  59. // MARK: - Echo status
  60. protocol EchoStatusRequest: Message {
  61. var responseStatus: Grpc_Testing_EchoStatus { get set }
  62. }
  63. extension EchoStatusRequest {
  64. var shouldEchoStatus: Bool {
  65. return self.responseStatus != Grpc_Testing_EchoStatus()
  66. }
  67. }
  68. extension EchoStatusRequest {
  69. static func withStatus(of status: Grpc_Testing_EchoStatus) -> Self {
  70. return Self.with { instance in
  71. instance.responseStatus = status
  72. }
  73. }
  74. }
  75. extension Grpc_Testing_SimpleRequest: EchoStatusRequest {}
  76. extension Grpc_Testing_StreamingOutputCallRequest: EchoStatusRequest {}
  77. // MARK: - Payload request
  78. protocol PayloadRequest: Message {
  79. var payload: Grpc_Testing_Payload { get set }
  80. }
  81. extension PayloadRequest {
  82. static func withPayload(of payload: Grpc_Testing_Payload) -> Self {
  83. return Self.with { instance in
  84. instance.payload = payload
  85. }
  86. }
  87. }
  88. extension Grpc_Testing_SimpleRequest: PayloadRequest {}
  89. extension Grpc_Testing_StreamingOutputCallRequest: PayloadRequest {}
  90. extension Grpc_Testing_StreamingInputCallRequest: PayloadRequest {}
  91. // MARK: - Echo metadata
  92. extension HPACKHeaders {
  93. /// See `ServerFeatures.echoMetadata`.
  94. var shouldEchoMetadata: Bool {
  95. return self.contains(name: "x-grpc-test-echo-initial") || self
  96. .contains(name: "x-grpc-test-echo-trailing-bin")
  97. }
  98. }