writer_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package headerwriter
  2. import (
  3. "fmt"
  4. "math"
  5. "net/http"
  6. "net/http/httptest"
  7. "strconv"
  8. "testing"
  9. "time"
  10. "github.com/imgproxy/imgproxy/v3/httpheaders"
  11. "github.com/stretchr/testify/suite"
  12. )
  13. type HeaderWriterSuite struct {
  14. suite.Suite
  15. }
  16. type writerTestCase struct {
  17. name string
  18. url string
  19. req http.Header
  20. res http.Header
  21. config Config
  22. fn func(*Writer)
  23. }
  24. func (s *HeaderWriterSuite) TestHeaderCases() {
  25. expires := time.Date(2030, 8, 1, 0, 0, 0, 0, time.UTC)
  26. expiresSeconds := strconv.Itoa(int(time.Until(expires).Seconds()))
  27. shortExpires := time.Now().Add(10 * time.Second)
  28. shortExpiresSeconds := strconv.Itoa(int(time.Until(shortExpires).Seconds()))
  29. tt := []writerTestCase{
  30. {
  31. name: "MinimalHeaders",
  32. req: http.Header{},
  33. res: http.Header{
  34. httpheaders.CacheControl: []string{"no-cache"},
  35. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  36. },
  37. config: Config{
  38. SetCanonicalHeader: false,
  39. DefaultTTL: 0,
  40. CacheControlPassthrough: false,
  41. LastModifiedEnabled: false,
  42. EnableClientHints: false,
  43. SetVaryAccept: false,
  44. },
  45. },
  46. {
  47. name: "PassthroughCacheControl",
  48. req: http.Header{
  49. httpheaders.CacheControl: []string{"no-cache, no-store, must-revalidate"},
  50. },
  51. res: http.Header{
  52. httpheaders.CacheControl: []string{"no-cache, no-store, must-revalidate"},
  53. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  54. },
  55. config: Config{
  56. CacheControlPassthrough: true,
  57. DefaultTTL: 3600,
  58. },
  59. },
  60. {
  61. name: "PassthroughCacheControlExpires",
  62. req: http.Header{
  63. httpheaders.Expires: []string{expires.Format(http.TimeFormat)},
  64. },
  65. res: http.Header{
  66. httpheaders.CacheControl: []string{fmt.Sprintf("max-age=%s, public", expiresSeconds)},
  67. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  68. },
  69. config: Config{
  70. CacheControlPassthrough: true,
  71. DefaultTTL: 3600,
  72. },
  73. },
  74. {
  75. name: "PassthroughCacheControlExpiredInThePast",
  76. req: http.Header{
  77. httpheaders.Expires: []string{time.Now().Add(-1 * time.Hour).UTC().Format(http.TimeFormat)},
  78. },
  79. res: http.Header{
  80. httpheaders.CacheControl: []string{"max-age=3600, public"},
  81. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  82. },
  83. config: Config{
  84. CacheControlPassthrough: true,
  85. DefaultTTL: 3600,
  86. },
  87. },
  88. {
  89. name: "Canonical_ValidURL",
  90. req: http.Header{},
  91. url: "https://example.com/image.jpg",
  92. res: http.Header{
  93. httpheaders.Link: []string{"<https://example.com/image.jpg>; rel=\"canonical\""},
  94. httpheaders.CacheControl: []string{"max-age=3600, public"},
  95. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  96. },
  97. config: Config{
  98. SetCanonicalHeader: true,
  99. DefaultTTL: 3600,
  100. },
  101. fn: func(w *Writer) {
  102. w.SetCanonical()
  103. },
  104. },
  105. {
  106. name: "Canonical_InvalidURL",
  107. url: "ftp://example.com/image.jpg",
  108. req: http.Header{},
  109. res: http.Header{
  110. httpheaders.CacheControl: []string{"max-age=3600, public"},
  111. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  112. },
  113. config: Config{
  114. SetCanonicalHeader: true,
  115. DefaultTTL: 3600,
  116. },
  117. },
  118. {
  119. name: "WriteCanonical_Disabled",
  120. req: http.Header{},
  121. url: "https://example.com/image.jpg",
  122. res: http.Header{
  123. httpheaders.CacheControl: []string{"max-age=3600, public"},
  124. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  125. },
  126. config: Config{
  127. SetCanonicalHeader: false,
  128. DefaultTTL: 3600,
  129. },
  130. fn: func(w *Writer) {
  131. w.SetCanonical()
  132. },
  133. },
  134. {
  135. name: "LastModified",
  136. req: http.Header{
  137. httpheaders.LastModified: []string{expires.Format(http.TimeFormat)},
  138. },
  139. res: http.Header{
  140. httpheaders.LastModified: []string{expires.Format(http.TimeFormat)},
  141. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  142. httpheaders.CacheControl: []string{"max-age=3600, public"},
  143. },
  144. config: Config{
  145. LastModifiedEnabled: true,
  146. DefaultTTL: 3600,
  147. },
  148. fn: func(w *Writer) {
  149. w.SetLastModified()
  150. },
  151. },
  152. {
  153. name: "SetMaxAgeTTL",
  154. req: http.Header{},
  155. res: http.Header{
  156. httpheaders.CacheControl: []string{"max-age=1, public"},
  157. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  158. },
  159. config: Config{
  160. DefaultTTL: 3600,
  161. FallbackImageTTL: 1,
  162. },
  163. fn: func(w *Writer) {
  164. w.SetIsFallbackImage()
  165. },
  166. },
  167. {
  168. name: "SetMaxAgeExpires",
  169. req: http.Header{},
  170. res: http.Header{
  171. httpheaders.CacheControl: []string{fmt.Sprintf("max-age=%s, public", expiresSeconds)},
  172. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  173. },
  174. config: Config{
  175. DefaultTTL: math.MaxInt32,
  176. },
  177. fn: func(w *Writer) {
  178. w.SetExpires(&expires)
  179. },
  180. },
  181. {
  182. name: "SetMaxAgeTTLOutlivesExpires",
  183. req: http.Header{},
  184. res: http.Header{
  185. httpheaders.CacheControl: []string{fmt.Sprintf("max-age=%s, public", shortExpiresSeconds)},
  186. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  187. },
  188. config: Config{
  189. DefaultTTL: math.MaxInt32,
  190. FallbackImageTTL: 600,
  191. },
  192. fn: func(w *Writer) {
  193. w.SetIsFallbackImage()
  194. w.SetExpires(&shortExpires)
  195. },
  196. },
  197. {
  198. name: "SetVaryHeader",
  199. req: http.Header{},
  200. res: http.Header{
  201. httpheaders.Vary: []string{"Accept, Sec-CH-DPR, DPR, Sec-CH-Width, Width"},
  202. httpheaders.CacheControl: []string{"no-cache"},
  203. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  204. },
  205. config: Config{
  206. EnableClientHints: true,
  207. SetVaryAccept: true,
  208. },
  209. fn: func(w *Writer) {
  210. w.SetVary()
  211. },
  212. },
  213. {
  214. name: "PassthroughHeaders",
  215. req: http.Header{
  216. "X-Test": []string{"foo", "bar"},
  217. },
  218. res: http.Header{
  219. "X-Test": []string{"foo", "bar"},
  220. httpheaders.CacheControl: []string{"no-cache"},
  221. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  222. },
  223. config: Config{},
  224. fn: func(w *Writer) {
  225. w.Passthrough([]string{"X-Test"})
  226. },
  227. },
  228. {
  229. name: "CopyFromHeaders",
  230. req: http.Header{},
  231. res: http.Header{
  232. "X-From": []string{"baz"},
  233. httpheaders.CacheControl: []string{"no-cache"},
  234. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  235. },
  236. config: Config{},
  237. fn: func(w *Writer) {
  238. h := http.Header{}
  239. h.Set("X-From", "baz")
  240. w.CopyFrom(h, []string{"X-From"})
  241. },
  242. },
  243. {
  244. name: "WriteContentLength",
  245. req: http.Header{},
  246. res: http.Header{
  247. httpheaders.ContentLength: []string{"123"},
  248. httpheaders.CacheControl: []string{"no-cache"},
  249. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  250. },
  251. config: Config{},
  252. fn: func(w *Writer) {
  253. w.SetContentLength(123)
  254. },
  255. },
  256. {
  257. name: "WriteContentType",
  258. req: http.Header{},
  259. res: http.Header{
  260. httpheaders.ContentType: []string{"image/png"},
  261. httpheaders.CacheControl: []string{"no-cache"},
  262. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  263. },
  264. config: Config{},
  265. fn: func(w *Writer) {
  266. w.SetContentType("image/png")
  267. },
  268. },
  269. {
  270. name: "SetMaxAgeFromExpiresNil",
  271. req: http.Header{},
  272. res: http.Header{
  273. httpheaders.CacheControl: []string{"max-age=3600, public"},
  274. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  275. },
  276. config: Config{
  277. DefaultTTL: 3600,
  278. },
  279. fn: func(w *Writer) {
  280. w.SetExpires(nil)
  281. },
  282. },
  283. {
  284. name: "WriteVaryAcceptOnly",
  285. req: http.Header{},
  286. res: http.Header{
  287. httpheaders.Vary: []string{"Accept"},
  288. httpheaders.CacheControl: []string{"no-cache"},
  289. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  290. },
  291. config: Config{
  292. SetVaryAccept: true,
  293. },
  294. fn: func(w *Writer) {
  295. w.SetVary()
  296. },
  297. },
  298. {
  299. name: "WriteVaryClientHintsOnly",
  300. req: http.Header{},
  301. res: http.Header{
  302. httpheaders.Vary: []string{"Sec-CH-DPR, DPR, Sec-CH-Width, Width"},
  303. httpheaders.CacheControl: []string{"no-cache"},
  304. httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
  305. },
  306. config: Config{
  307. EnableClientHints: true,
  308. },
  309. fn: func(w *Writer) {
  310. w.SetVary()
  311. },
  312. },
  313. }
  314. for _, tc := range tt {
  315. s.Run(tc.name, func() {
  316. writer := New(&tc.config, tc.req, tc.url)
  317. if tc.fn != nil {
  318. tc.fn(writer)
  319. }
  320. r := httptest.NewRecorder()
  321. writer.Write(r)
  322. s.Require().Equal(tc.res, r.Header())
  323. })
  324. }
  325. }
  326. func TestHeaderWriter(t *testing.T) {
  327. suite.Run(t, new(HeaderWriterSuite))
  328. }