MessageEncodingHeaderValidatorTests.swift 3.3 KB

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