scraper_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "%s" instead of "%s"`, rule, actualResult, expectedResult)
  64. }
  65. }
  66. }