MessageEncodingHeaderValidatorTests.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2020, 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 XCTest
  17. @testable import GRPC
  18. class MessageEncodingHeaderValidatorTests: GRPCTestCase {
  19. func testSupportedAlgorithm() throws {
  20. let validator = MessageEncodingHeaderValidator(
  21. encoding: .enabled(
  22. .init(
  23. enabledAlgorithms: [.deflate, .gzip],
  24. decompressionLimit: .absolute(10)
  25. )
  26. )
  27. )
  28. let validation = validator.validate(requestEncoding: "gzip")
  29. switch validation {
  30. case .supported(.gzip, .absolute(10), acceptEncoding: []):
  31. () // Expected
  32. default:
  33. XCTFail("Expected .supported but was \(validation)")
  34. }
  35. }
  36. func testSupportedButNotAdvertisedAlgorithm() throws {
  37. let validator = MessageEncodingHeaderValidator(
  38. encoding: .enabled(.init(enabledAlgorithms: [.deflate], decompressionLimit: .absolute(10)))
  39. )
  40. let validation = validator.validate(requestEncoding: "gzip")
  41. switch validation {
  42. case .supported(.gzip, .absolute(10), acceptEncoding: ["deflate", "gzip"]):
  43. () // Expected
  44. default:
  45. XCTFail("Expected .supported but was \(validation)")
  46. }
  47. }
  48. func testSupportedButExplicitlyDisabled() throws {
  49. let validator = MessageEncodingHeaderValidator(encoding: .disabled)
  50. let validation = validator.validate(requestEncoding: "gzip")
  51. switch validation {
  52. case .unsupported(requestEncoding: "gzip", acceptEncoding: []):
  53. () // Expected
  54. default:
  55. XCTFail("Expected .unsupported but was \(validation)")
  56. }
  57. }
  58. func testUnsupportedButEnabled() throws {
  59. let validator = MessageEncodingHeaderValidator(
  60. encoding:
  61. .enabled(.init(enabledAlgorithms: [.gzip], decompressionLimit: .absolute(10)))
  62. )
  63. let validation = validator.validate(requestEncoding: "not-supported")
  64. switch validation {
  65. case .unsupported(requestEncoding: "not-supported", acceptEncoding: ["gzip"]):
  66. () // Expected
  67. default:
  68. XCTFail("Expected .unsupported but was \(validation)")
  69. }
  70. }
  71. func testNoCompressionWhenExplicitlyDisabled() throws {
  72. let validator = MessageEncodingHeaderValidator(encoding: .disabled)
  73. let validation = validator.validate(requestEncoding: nil)
  74. switch validation {
  75. case .noCompression:
  76. () // Expected
  77. default:
  78. XCTFail("Expected .noCompression but was \(validation)")
  79. }
  80. }
  81. func testNoCompressionWhenEnabled() throws {
  82. let validator = MessageEncodingHeaderValidator(
  83. encoding:
  84. .enabled(
  85. .init(
  86. enabledAlgorithms: CompressionAlgorithm.all,
  87. decompressionLimit: .absolute(10)
  88. )
  89. )
  90. )
  91. let validation = validator.validate(requestEncoding: nil)
  92. switch validation {
  93. case .noCompression:
  94. () // Expected
  95. default:
  96. XCTFail("Expected .noCompression but was \(validation)")
  97. }
  98. }
  99. }