encoding_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package response
  4. import (
  5. "testing"
  6. )
  7. func TestAcceptEncoding(t *testing.T) {
  8. t.Parallel()
  9. acceptable := []string{
  10. "br", "gzip", "deflate",
  11. }
  12. tests := []struct {
  13. name string
  14. acceptEncoding string
  15. want string
  16. }{
  17. {
  18. name: "Empty input",
  19. acceptEncoding: "",
  20. want: "identity",
  21. },
  22. {
  23. name: "q=0 and identity",
  24. acceptEncoding: "identity;q=0",
  25. want: "",
  26. },
  27. {
  28. name: "q=0 and *",
  29. acceptEncoding: "*;q=0",
  30. want: "",
  31. },
  32. {
  33. name: "gzip",
  34. acceptEncoding: "gzip",
  35. want: "gzip",
  36. },
  37. {
  38. name: "gzip and br",
  39. acceptEncoding: "gzip,br",
  40. want: "gzip",
  41. },
  42. {
  43. name: "br and gzip",
  44. acceptEncoding: "br,gzip,deflate",
  45. want: "br",
  46. },
  47. {
  48. name: "unsupported encoding",
  49. acceptEncoding: "unknown",
  50. want: "identity",
  51. },
  52. {
  53. name: "empty encoding",
  54. acceptEncoding: ",",
  55. want: "identity",
  56. },
  57. {
  58. name: "multiple encodings and q=0",
  59. acceptEncoding: "gzip;q=0,br;q=0",
  60. want: "identity",
  61. },
  62. {
  63. // We want br here but weights are not supported.
  64. name: "multiple encodings and q values",
  65. acceptEncoding: "gzip;q=0.5,br;q=0.8",
  66. want: "gzip",
  67. },
  68. {
  69. name: "multiple encodings and wildcard",
  70. acceptEncoding: "*;q=0,gzip,br",
  71. want: "gzip",
  72. },
  73. {
  74. name: "multiple encodings and wildcard and q=0",
  75. acceptEncoding: "*;q=0,gzip,br;q=0",
  76. want: "gzip",
  77. },
  78. {
  79. // We want br here but weights are not supported.
  80. name: "multiple encodings and wildcard and q values",
  81. acceptEncoding: "*;q=0.5,gzip;q=0.8,br",
  82. want: "gzip",
  83. },
  84. {
  85. name: "multiple encodings and wildcard and q values and q=0",
  86. acceptEncoding: "*;q=0.5,gzip;q=0.8,br;q=0",
  87. want: "gzip",
  88. },
  89. {
  90. name: "invalid q value",
  91. acceptEncoding: "gzip;q=abc,deflate",
  92. want: "deflate",
  93. },
  94. {
  95. name: "wrong spaces placing around q value",
  96. acceptEncoding: "gzip;q= 0.5, deflate;q=0.8",
  97. want: "deflate",
  98. },
  99. {
  100. name: "correct spaces placing around q value",
  101. acceptEncoding: "gzip ; q=0.5, deflate;q=0.8",
  102. want: "gzip",
  103. },
  104. }
  105. for _, test := range tests {
  106. t.Run(test.name, func(t *testing.T) {
  107. t.Parallel()
  108. // Instantiate parser for each test to make sure it doesn't return cached values.
  109. parser := AcceptEncoding(acceptable...)
  110. got := parser.Parse(test.acceptEncoding)
  111. if got != test.want {
  112. t.Errorf("Parse(%q) = %q, want %q", test.acceptEncoding, got, test.want)
  113. }
  114. })
  115. }
  116. }
  117. func BenchmarkAcceptEncoding(b *testing.B) {
  118. encoding := "identity;q=0,gzip,whatever"
  119. expected := "gzip"
  120. parser := AcceptEncoding("br", "gzip", "deflate")
  121. for b.Loop() {
  122. got := parser.Parse(encoding)
  123. if got != expected {
  124. b.Errorf("Parse(%q) = %q, want %q", encoding, got, expected)
  125. }
  126. }
  127. }