2
0

GRPCTestingConvenienceMethods.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 EchoStatusRequest {
  57. static func withStatus(of status: Grpc_Testing_EchoStatus) -> Self {
  58. return Self.with { instance in
  59. instance.responseStatus = status
  60. }
  61. }
  62. }
  63. extension Grpc_Testing_SimpleRequest: EchoStatusRequest { }
  64. extension Grpc_Testing_StreamingOutputCallRequest: EchoStatusRequest { }
  65. // MARK: - Payload request
  66. protocol PayloadRequest: Message {
  67. var payload: Grpc_Testing_Payload { get set }
  68. }
  69. extension PayloadRequest {
  70. static func withPayload(of payload: Grpc_Testing_Payload) -> Self {
  71. return Self.with { instance in
  72. instance.payload = payload
  73. }
  74. }
  75. }
  76. extension Grpc_Testing_SimpleRequest: PayloadRequest { }
  77. extension Grpc_Testing_StreamingOutputCallRequest: PayloadRequest { }
  78. extension Grpc_Testing_StreamingInputCallRequest: PayloadRequest { }
  79. // MARK: - Echo metadata
  80. extension HTTPHeaders {
  81. /// See `ServerFeatures.echoMetadata`.
  82. var shouldEchoMetadata: Bool {
  83. return self.contains(name: "x-grpc-test-echo-initial") || self.contains(name: "x-grpc-test-echo-trailing-bin")
  84. }
  85. }