proxy_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ui // import "miniflux.app/v2/internal/ui"
  4. import (
  5. "io"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "miniflux.app/v2/internal/http/response"
  10. "miniflux.app/v2/internal/mediaproxy"
  11. )
  12. func TestIsInlineMediaType(t *testing.T) {
  13. scenarios := []struct {
  14. contentType string
  15. expected bool
  16. }{
  17. {"image/png", true},
  18. {"image/svg+xml", true},
  19. {"IMAGE/JPEG; charset=utf-8", true},
  20. {"audio/mpeg", true},
  21. {"video/mp4", true},
  22. {"text/html", false},
  23. {"text/html; charset=utf-8", false},
  24. {"application/xhtml+xml", false},
  25. {"application/octet-stream", false},
  26. {"text/plain", false},
  27. {"", false},
  28. {";;", false},
  29. {"image", false},
  30. }
  31. for _, scenario := range scenarios {
  32. if result := isInlineMediaType(scenario.contentType); result != scenario.expected {
  33. t.Errorf(`Unexpected result for %q, got %v instead of %v`, scenario.contentType, result, scenario.expected)
  34. }
  35. }
  36. }
  37. func TestMediaProxyContentTypeHandling(t *testing.T) {
  38. scenarios := []struct {
  39. name string
  40. upstreamPath string
  41. upstreamContentType string
  42. expectedContentType string
  43. expectedContentDisposition string
  44. }{
  45. {"png image", "/image.png", "image/png", "image/png", "inline; filename=image.png"},
  46. {"svg image with parameters", "/image.svg", "IMAGE/SVG+XML; charset=utf-8", "IMAGE/SVG+XML; charset=utf-8", "inline; filename=image.svg"},
  47. {"audio", "/episode.mp3", "audio/mpeg", "audio/mpeg", "inline; filename=episode.mp3"},
  48. {"video", "/clip.mp4", "video/mp4", "video/mp4", "inline; filename=clip.mp4"},
  49. {"image without filename", "/", "image/png", "image/png", ""},
  50. {"html page", "/page.html", "text/html; charset=utf-8", "application/octet-stream", "attachment; filename=page.html"},
  51. {"xhtml page", "/page.xhtml", "application/xhtml+xml", "application/octet-stream", "attachment; filename=page.xhtml"},
  52. {"html page without filename", "/", "text/html", "application/octet-stream", "attachment"},
  53. {"missing content type", "/unknown", "", "application/octet-stream", "attachment; filename=unknown"},
  54. {"malformed content type", "/broken", ";;", "application/octet-stream", "attachment; filename=broken"},
  55. }
  56. const upstreamBody = "<html><body>upstream body</body></html>"
  57. for _, scenario := range scenarios {
  58. t.Run(scenario.name, func(t *testing.T) {
  59. configureTestOptions(t, map[string]string{
  60. "MEDIA_PROXY_PRIVATE_KEY": "test-private-key",
  61. "FETCHER_ALLOW_PRIVATE_NETWORKS": "1",
  62. })
  63. upstreamServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  64. if scenario.upstreamContentType == "" {
  65. // Setting the header to nil prevents net/http from sniffing a Content-Type.
  66. w.Header()["Content-Type"] = nil
  67. } else {
  68. w.Header().Set("Content-Type", scenario.upstreamContentType)
  69. }
  70. io.WriteString(w, upstreamBody)
  71. }))
  72. t.Cleanup(upstreamServer.Close)
  73. mux := http.NewServeMux()
  74. mux.HandleFunc("GET /proxy/{encodedDigest}/{encodedURL}", (&handler{}).mediaProxy)
  75. proxyPath := mediaproxy.ProxifyRelativeURL(upstreamServer.URL + scenario.upstreamPath)
  76. recorder := httptest.NewRecorder()
  77. mux.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, proxyPath, nil))
  78. if recorder.Code != http.StatusOK {
  79. t.Fatalf(`Unexpected status code, got %d instead of %d`, recorder.Code, http.StatusOK)
  80. }
  81. if contentType := recorder.Header().Get("Content-Type"); contentType != scenario.expectedContentType {
  82. t.Errorf(`Unexpected Content-Type, got %q instead of %q`, contentType, scenario.expectedContentType)
  83. }
  84. if contentDisposition := recorder.Header().Get("Content-Disposition"); contentDisposition != scenario.expectedContentDisposition {
  85. t.Errorf(`Unexpected Content-Disposition, got %q instead of %q`, contentDisposition, scenario.expectedContentDisposition)
  86. }
  87. if csp := recorder.Header().Get("Content-Security-Policy"); csp != response.ContentSecurityPolicyForUntrustedContent {
  88. t.Errorf(`Unexpected Content-Security-Policy, got %q`, csp)
  89. }
  90. if body := recorder.Body.String(); body != upstreamBody {
  91. t.Errorf(`Unexpected body, got %q instead of %q`, body, upstreamBody)
  92. }
  93. })
  94. }
  95. }