GRPCTestingConvenienceMethods.swift 3.0 KB

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