feed_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package model // import "miniflux.app/model"
  5. import (
  6. "fmt"
  7. "os"
  8. "testing"
  9. "time"
  10. "miniflux.app/config"
  11. "miniflux.app/http/client"
  12. )
  13. func TestFeedWithResponse(t *testing.T) {
  14. response := &client.Response{ETag: "Some etag", LastModified: "Some date", EffectiveURL: "Some URL"}
  15. feed := &Feed{}
  16. feed.WithClientResponse(response)
  17. if feed.EtagHeader != "Some etag" {
  18. t.Fatal(`The ETag header should be set`)
  19. }
  20. if feed.LastModifiedHeader != "Some date" {
  21. t.Fatal(`The LastModified header should be set`)
  22. }
  23. if feed.FeedURL != "Some URL" {
  24. t.Fatal(`The Feed URL should be set`)
  25. }
  26. }
  27. func TestFeedCategorySetter(t *testing.T) {
  28. feed := &Feed{}
  29. feed.WithCategoryID(int64(123))
  30. if feed.Category == nil {
  31. t.Fatal(`The category field should not be null`)
  32. }
  33. if feed.Category.ID != int64(123) {
  34. t.Error(`The category ID must be set`)
  35. }
  36. }
  37. func TestFeedBrowsingParams(t *testing.T) {
  38. feed := &Feed{}
  39. feed.WithBrowsingParameters(true, "Custom User Agent", "Username", "Secret", "Some Rule", "Another Rule")
  40. if !feed.Crawler {
  41. t.Error(`The crawler must be activated`)
  42. }
  43. if feed.UserAgent != "Custom User Agent" {
  44. t.Error(`The user agent must be set`)
  45. }
  46. if feed.Username != "Username" {
  47. t.Error(`The username must be set`)
  48. }
  49. if feed.Password != "Secret" {
  50. t.Error(`The password must be set`)
  51. }
  52. if feed.ScraperRules != "Some Rule" {
  53. t.Errorf(`The scraper rules must be set`)
  54. }
  55. if feed.RewriteRules != "Another Rule" {
  56. t.Errorf(`The rewrite rules must be set`)
  57. }
  58. }
  59. func TestFeedErrorCounter(t *testing.T) {
  60. feed := &Feed{}
  61. feed.WithError("Some Error")
  62. if feed.ParsingErrorMsg != "Some Error" {
  63. t.Error(`The error message must be set`)
  64. }
  65. if feed.ParsingErrorCount != 1 {
  66. t.Error(`The error counter must be set to 1`)
  67. }
  68. feed.ResetErrorCounter()
  69. if feed.ParsingErrorMsg != "" {
  70. t.Error(`The error message must be removed`)
  71. }
  72. if feed.ParsingErrorCount != 0 {
  73. t.Error(`The error counter must be set to 0`)
  74. }
  75. }
  76. func TestFeedCheckedNow(t *testing.T) {
  77. feed := &Feed{}
  78. feed.FeedURL = "https://example.org/feed"
  79. feed.CheckedNow()
  80. if feed.SiteURL != feed.FeedURL {
  81. t.Error(`The site URL must not be empty`)
  82. }
  83. if feed.CheckedAt.IsZero() {
  84. t.Error(`The checked date must be set`)
  85. }
  86. }
  87. func TestFeedScheduleNextCheckDefault(t *testing.T) {
  88. var err error
  89. parser := config.NewParser()
  90. config.Opts, err = parser.ParseEnvironmentVariables()
  91. if err != nil {
  92. t.Fatalf(`Parsing failure: %v`, err)
  93. }
  94. feed := &Feed{}
  95. weeklyCount := 10
  96. feed.ScheduleNextCheck(weeklyCount)
  97. if feed.NextCheckAt.IsZero() {
  98. t.Error(`The next_check_at must be set`)
  99. }
  100. }
  101. func TestFeedScheduleNextCheckEntryCountBasedMaxInterval(t *testing.T) {
  102. maxInterval := 5
  103. minInterval := 1
  104. os.Clearenv()
  105. os.Setenv("POLLING_SCHEDULER", "entry_frequency")
  106. os.Setenv("SCHEDULER_ENTRY_FREQUENCY_MAX_INTERVAL", fmt.Sprintf("%d", maxInterval))
  107. os.Setenv("SCHEDULER_ENTRY_FREQUENCY_MIN_INTERVAL", fmt.Sprintf("%d", minInterval))
  108. var err error
  109. parser := config.NewParser()
  110. config.Opts, err = parser.ParseEnvironmentVariables()
  111. if err != nil {
  112. t.Fatalf(`Parsing failure: %v`, err)
  113. }
  114. feed := &Feed{}
  115. weeklyCount := maxInterval * 100
  116. feed.ScheduleNextCheck(weeklyCount)
  117. if feed.NextCheckAt.IsZero() {
  118. t.Error(`The next_check_at must be set`)
  119. }
  120. if feed.NextCheckAt.After(time.Now().Add(time.Minute * time.Duration(maxInterval))) {
  121. t.Error(`The next_check_at should not be after the now + max interval`)
  122. }
  123. }
  124. func TestFeedScheduleNextCheckEntryCountBasedMinInterval(t *testing.T) {
  125. maxInterval := 500
  126. minInterval := 100
  127. os.Clearenv()
  128. os.Setenv("POLLING_SCHEDULER", "entry_frequency")
  129. os.Setenv("SCHEDULER_ENTRY_FREQUENCY_MAX_INTERVAL", fmt.Sprintf("%d", maxInterval))
  130. os.Setenv("SCHEDULER_ENTRY_FREQUENCY_MIN_INTERVAL", fmt.Sprintf("%d", minInterval))
  131. var err error
  132. parser := config.NewParser()
  133. config.Opts, err = parser.ParseEnvironmentVariables()
  134. if err != nil {
  135. t.Fatalf(`Parsing failure: %v`, err)
  136. }
  137. feed := &Feed{}
  138. weeklyCount := minInterval / 2
  139. feed.ScheduleNextCheck(weeklyCount)
  140. if feed.NextCheckAt.IsZero() {
  141. t.Error(`The next_check_at must be set`)
  142. }
  143. if feed.NextCheckAt.Before(time.Now().Add(time.Minute * time.Duration(minInterval))) {
  144. t.Error(`The next_check_at should not be before the now + min interval`)
  145. }
  146. }