GRPCTestingConvenienceMethods.swift 3.1 KB

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