encoding_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 *",
  24. acceptEncoding: "*;q=0",
  25. want: "identity",
  26. },
  27. {
  28. name: "gzip",
  29. acceptEncoding: "gzip",
  30. want: "gzip",
  31. },
  32. {
  33. name: "gzip and br",
  34. acceptEncoding: "gzip,br",
  35. want: "br",
  36. },
  37. {
  38. name: "br and gzip",
  39. acceptEncoding: "br,gzip,deflate",
  40. want: "br",
  41. },
  42. {
  43. name: "unsupported encoding",
  44. acceptEncoding: "unknown",
  45. want: "identity",
  46. },
  47. {
  48. name: "empty encoding",
  49. acceptEncoding: ",",
  50. want: "identity",
  51. },
  52. {
  53. name: "multiple encodings and q=0",
  54. acceptEncoding: "gzip;q=0,br;q=0",
  55. want: "identity",
  56. },
  57. {
  58. // We want br here but weights are not supported.
  59. name: "multiple encodings and q values",
  60. acceptEncoding: "gzip;q=0.5,br;q=0.8",
  61. want: "br",
  62. },
  63. {
  64. name: "multiple encodings and wildcard",
  65. acceptEncoding: "*;q=0,gzip,br",
  66. want: "br",
  67. },
  68. {
  69. name: "multiple encodings and wildcard and q=0",
  70. acceptEncoding: "*;q=0,gzip,br;q=0",
  71. want: "gzip",
  72. },
  73. {
  74. // We want br here but weights are not supported.
  75. name: "multiple encodings and wildcard and q values",
  76. acceptEncoding: "*;q=0.5,gzip;q=0.8,br",
  77. want: "br",
  78. },
  79. {
  80. name: "multiple encodings and wildcard and q values and q=0",
  81. acceptEncoding: "*;q=0.5,gzip;q=0.8,br;q=0",
  82. want: "gzip",
  83. },
  84. {
  85. name: "invalid q value",
  86. acceptEncoding: "gzip;q=abc,deflate",
  87. want: "deflate",
  88. },
  89. {
  90. name: "wrong spaces placing around q value",
  91. acceptEncoding: "gzip;q= 0.5, deflate;q=0.8",
  92. want: "deflate",
  93. },
  94. {
  95. name: "correct spaces placing around q value",
  96. acceptEncoding: "gzip ; q=0.5, deflate;q=0.8",
  97. want: "gzip",
  98. },
  99. {
  100. name: "default for chrome / safari as of 2026",
  101. acceptEncoding: "gzip, deflate, br, zstd",
  102. want: "br",
  103. },
  104. {
  105. // Server preference order wins over the client's listed order.
  106. name: "server preference over client order",
  107. acceptEncoding: "deflate, gzip",
  108. want: "gzip",
  109. },
  110. {
  111. // q=0 excludes a specific supported encoding; next server pick wins.
  112. name: "q=0 excludes specific supported encoding",
  113. acceptEncoding: "br;q=0, gzip",
  114. want: "gzip",
  115. },
  116. {
  117. // A bare wildcard is not treated as opting into compression.
  118. name: "bare wildcard",
  119. acceptEncoding: "*",
  120. want: "identity",
  121. },
  122. {
  123. name: "identity only",
  124. acceptEncoding: "identity",
  125. want: "identity",
  126. },
  127. {
  128. name: "whitespace-only and empty segments",
  129. acceptEncoding: " , gzip , ",
  130. want: "gzip",
  131. },
  132. }
  133. for _, test := range tests {
  134. t.Run(test.name, func(t *testing.T) {
  135. t.Parallel()
  136. // Instantiate parser for each test to make sure it doesn't return cached values.
  137. parser := AcceptEncoding(acceptable...)
  138. got := parser.Parse(test.acceptEncoding)
  139. if got != test.want {
  140. t.Errorf("Parse(%q) = %q, want %q", test.acceptEncoding, got, test.want)
  141. }
  142. })
  143. }
  144. }
  145. func BenchmarkAcceptEncoding(b *testing.B) {
  146. encoding := "identity;q=0,gzip,whatever"
  147. expected := "gzip"
  148. parser := AcceptEncoding("br", "gzip", "deflate")
  149. for b.Loop() {
  150. got := parser.Parse(encoding)
  151. if got != expected {
  152. b.Errorf("Parse(%q) = %q, want %q", encoding, got, expected)
  153. }
  154. }
  155. }