referer_override_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package rewrite // import "miniflux.app/v2/internal/reader/rewrite"
  4. import (
  5. "testing"
  6. )
  7. func TestGetRefererForURL(t *testing.T) {
  8. testCases := []struct {
  9. name string
  10. url string
  11. expected string
  12. }{
  13. {
  14. name: "Weibo Image URL",
  15. url: "https://wx1.sinaimg.cn/large/example.jpg",
  16. expected: "https://weibo.com",
  17. },
  18. {
  19. name: "Pixiv Image URL",
  20. url: "https://i.pximg.net/img-master/example.jpg",
  21. expected: "https://www.pixiv.net",
  22. },
  23. {
  24. name: "SSPai CDN URL",
  25. url: "https://cdnfile.sspai.com/example.png",
  26. expected: "https://sspai.com",
  27. },
  28. {
  29. name: "Instagram CDN URL",
  30. url: "https://scontent-sjc3-1.cdninstagram.com/example.jpg",
  31. expected: "https://www.instagram.com",
  32. },
  33. {
  34. name: "Weibo Video URL",
  35. url: "https://f.video.weibocdn.com/example.mp4",
  36. expected: "https://weibo.com",
  37. },
  38. {
  39. name: "HelloGithub Image URL",
  40. url: "https://img.hellogithub.com/example.png",
  41. expected: "https://hellogithub.com",
  42. },
  43. {
  44. name: "Park Blogs",
  45. url: "https://www.parkablogs.com/sites/default/files/2025/image.jpg",
  46. expected: "https://www.parkablogs.com",
  47. },
  48. {
  49. name: "Non-matching URL",
  50. url: "https://example.com/image.jpg",
  51. expected: "",
  52. },
  53. {
  54. name: "Exact hostname match",
  55. url: "https://appinn.com/some/path",
  56. expected: "https://appinn.com",
  57. },
  58. {
  59. name: "Only hostnames starting with a dot should match as suffix",
  60. url: "https://fake-appinn.com/some/path",
  61. expected: "",
  62. },
  63. {
  64. name: "Subdomain match with suffix",
  65. url: "https://sub.moyu.im/image.png",
  66. expected: "https://i.jandan.net",
  67. },
  68. }
  69. for _, tc := range testCases {
  70. t.Run(tc.name, func(t *testing.T) {
  71. result := GetRefererForURL(tc.url)
  72. if result != tc.expected {
  73. t.Errorf("GetRefererForURL(%s): expected %s, got %s",
  74. tc.url, tc.expected, result)
  75. }
  76. })
  77. }
  78. }