scraper_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package scraper // import "miniflux.app/v2/internal/reader/scraper"
  4. import (
  5. "bytes"
  6. "os"
  7. "strings"
  8. "testing"
  9. )
  10. func TestGetPredefinedRules(t *testing.T) {
  11. if getPredefinedScraperRules("http://www.phoronix.com/") == "" {
  12. t.Error("Unable to find rule for phoronix.com")
  13. }
  14. if getPredefinedScraperRules("https://www.linux.com/") == "" {
  15. t.Error("Unable to find rule for linux.com")
  16. }
  17. if getPredefinedScraperRules("https://linux.com/") == "" {
  18. t.Error("Unable to find rule for linux.com")
  19. }
  20. if getPredefinedScraperRules("https://example.org/") != "" {
  21. t.Error("A rule not defined should not return anything")
  22. }
  23. }
  24. func TestWhitelistedContentTypes(t *testing.T) {
  25. scenarios := map[string]bool{
  26. "text/html": true,
  27. "TeXt/hTmL": true,
  28. "application/xhtml+xml": true,
  29. "text/html; charset=utf-8": true,
  30. "application/xhtml+xml; charset=utf-8": true,
  31. "text/css": false,
  32. "application/javascript": false,
  33. "image/png": false,
  34. "application/pdf": false,
  35. }
  36. for inputValue, expectedResult := range scenarios {
  37. actualResult := isAllowedContentType(inputValue)
  38. if actualResult != expectedResult {
  39. t.Errorf(`Unexpected result for content type whitelist, got "%v" instead of "%v"`, actualResult, expectedResult)
  40. }
  41. }
  42. }
  43. func TestSelectorRules(t *testing.T) {
  44. var ruleTestCases = map[string]string{
  45. "img.html": "article > img",
  46. "iframe.html": "article > iframe",
  47. "p.html": "article > p",
  48. }
  49. for filename, rule := range ruleTestCases {
  50. html, err := os.ReadFile("testdata/" + filename)
  51. if err != nil {
  52. t.Fatalf(`Unable to read file %q: %v`, filename, err)
  53. }
  54. _, actualResult, err := findContentUsingCustomRules(bytes.NewReader(html), rule)
  55. if err != nil {
  56. t.Fatalf(`Scraping error for %q - %q: %v`, filename, rule, err)
  57. }
  58. expectedResult, err := os.ReadFile("testdata/" + filename + "-result")
  59. if err != nil {
  60. t.Fatalf(`Unable to read file %q: %v`, filename, err)
  61. }
  62. if actualResult != strings.TrimSpace(string(expectedResult)) {
  63. t.Errorf(`Unexpected result for %q, got %q instead of %q`, rule, actualResult, expectedResult)
  64. }
  65. }
  66. }
  67. func TestParseBaseURLWithCustomRules(t *testing.T) {
  68. html := `<html><head><base href="https://example.com/"></head><body><img src="image.jpg"></body></html>`
  69. baseURL, _, err := findContentUsingCustomRules(strings.NewReader(html), "img")
  70. if err != nil {
  71. t.Fatalf(`Scraping error: %v`, err)
  72. }
  73. if baseURL != "https://example.com/" {
  74. t.Errorf(`Unexpected base URL, got %q instead of "https://example.com/"`, baseURL)
  75. }
  76. }
  77. func TestParseMultipleBaseURLWithCustomRules(t *testing.T) {
  78. html := `<html><head><base href="https://example.com/"><base href="https://example.org/"/></head><body><img src="image.jpg"></body></html>`
  79. baseURL, _, err := findContentUsingCustomRules(strings.NewReader(html), "img")
  80. if err != nil {
  81. t.Fatalf(`Scraping error: %v`, err)
  82. }
  83. if baseURL != "https://example.com/" {
  84. t.Errorf(`Unexpected base URL, got %q instead of "https://example.com/"`, baseURL)
  85. }
  86. }
  87. func TestParseRelativeBaseURLWithCustomRules(t *testing.T) {
  88. html := `<html><head><base href="/test"></head><body><img src="image.jpg"></body></html>`
  89. baseURL, _, err := findContentUsingCustomRules(strings.NewReader(html), "img")
  90. if err != nil {
  91. t.Fatalf(`Scraping error: %v`, err)
  92. }
  93. if baseURL != "" {
  94. t.Errorf(`Unexpected base URL, got %q`, baseURL)
  95. }
  96. }
  97. func TestParseEmptyBaseURLWithCustomRules(t *testing.T) {
  98. html := `<html><head><base href=" "></head><body><img src="image.jpg"></body></html>`
  99. baseURL, _, err := findContentUsingCustomRules(strings.NewReader(html), "img")
  100. if err != nil {
  101. t.Fatalf(`Scraping error: %v`, err)
  102. }
  103. if baseURL != "" {
  104. t.Errorf(`Unexpected base URL, got %q instead of ""`, baseURL)
  105. }
  106. }
  107. func TestParseMissingBaseURLWithCustomRules(t *testing.T) {
  108. html := `<html><head></head><body><img src="image.jpg"></body></html>`
  109. baseURL, _, err := findContentUsingCustomRules(strings.NewReader(html), "img")
  110. if err != nil {
  111. t.Fatalf(`Scraping error: %v`, err)
  112. }
  113. if baseURL != "" {
  114. t.Errorf(`Unexpected base URL, got %q instead of ""`, baseURL)
  115. }
  116. }