MessageEncodingHeaderValidatorTests.swift 3.2 KB

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