4
0

scraper_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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://example.org/") != "" {
  18. t.Error("A rule not defined should not return anything")
  19. }
  20. }
  21. func TestWhitelistedContentTypes(t *testing.T) {
  22. scenarios := map[string]bool{
  23. "text/html": true,
  24. "TeXt/hTmL": true,
  25. "application/xhtml+xml": true,
  26. "text/html; charset=utf-8": true,
  27. "application/xhtml+xml; charset=utf-8": true,
  28. "text/css": false,
  29. "application/javascript": false,
  30. "image/png": false,
  31. "application/pdf": false,
  32. }
  33. for inputValue, expectedResult := range scenarios {
  34. actualResult := isAllowedContentType(inputValue)
  35. if actualResult != expectedResult {
  36. t.Errorf(`Unexpected result for content type whitelist, got "%v" instead of "%v"`, actualResult, expectedResult)
  37. }
  38. }
  39. }
  40. func TestSelectorRules(t *testing.T) {
  41. var ruleTestCases = map[string]string{
  42. "img.html": "article > img",
  43. "iframe.html": "article > iframe",
  44. "p.html": "article > p",
  45. }
  46. for filename, rule := range ruleTestCases {
  47. html, err := os.ReadFile("testdata/" + filename)
  48. if err != nil {
  49. t.Fatalf(`Unable to read file %q: %v`, filename, err)
  50. }
  51. actualResult, err := findContentUsingCustomRules(bytes.NewReader(html), rule)
  52. if err != nil {
  53. t.Fatalf(`Scraping error for %q - %q: %v`, filename, rule, err)
  54. }
  55. expectedResult, err := os.ReadFile("testdata/" + filename + "-result")
  56. if err != nil {
  57. t.Fatalf(`Unable to read file %q: %v`, filename, err)
  58. }
  59. if actualResult != strings.TrimSpace(string(expectedResult)) {
  60. t.Errorf(`Unexpected result for %q, got "%s" instead of "%s"`, rule, actualResult, expectedResult)
  61. }
  62. }
  63. }