fixture.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package servertest // import "miniflux.app/v2/internal/http/server/servertest"
  4. import (
  5. "bytes"
  6. "fmt"
  7. "image"
  8. "image/color"
  9. "image/png"
  10. "net/http"
  11. "net/http/httptest"
  12. "strings"
  13. "testing"
  14. "time"
  15. miniflux "miniflux.app/v2/client"
  16. )
  17. const (
  18. FeedTitle = "Integration Test Feed"
  19. SubscriptionTitle = "Integration Test Subscription"
  20. EntryCount = 3
  21. // SearchTerm appears only in the content of the second entry.
  22. SearchTerm = "zanzibar"
  23. // ArticleText appears in every article page, but not in the feed.
  24. ArticleText = "This paragraph only exists on the original article page."
  25. )
  26. // FeedFixture is a local website with a feed, an icon and article pages.
  27. // The server under test fetches it instead of a remote site.
  28. type FeedFixture struct {
  29. WebsiteURL string
  30. FeedURL string
  31. }
  32. // NewFeedFixture starts a fixture website that is shut down when the test ends.
  33. func NewFeedFixture(t testing.TB) *FeedFixture {
  34. t.Helper()
  35. icon := newFixtureIcon(t)
  36. mux := http.NewServeMux()
  37. server := httptest.NewServer(mux)
  38. t.Cleanup(server.Close)
  39. fixture := &FeedFixture{
  40. WebsiteURL: server.URL + "/",
  41. FeedURL: server.URL + "/feed.xml",
  42. }
  43. mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
  44. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  45. fmt.Fprintf(w, `<!DOCTYPE html>
  46. <html>
  47. <head>
  48. <title>Integration Test Website</title>
  49. <link rel="alternate" type="application/rss+xml" title=%q href="/feed.xml">
  50. <link rel="icon" type="image/png" href="/icon.png">
  51. </head>
  52. <body><h1>Integration Test Website</h1></body>
  53. </html>`, SubscriptionTitle)
  54. })
  55. mux.HandleFunc("GET /feed.xml", func(w http.ResponseWriter, r *http.Request) {
  56. w.Header().Set("Content-Type", "application/rss+xml")
  57. fmt.Fprint(w, fixture.feedDocument())
  58. })
  59. mux.HandleFunc("GET /icon.png", func(w http.ResponseWriter, r *http.Request) {
  60. w.Header().Set("Content-Type", "image/png")
  61. w.Write(icon)
  62. })
  63. mux.HandleFunc("GET /entries/{id}", func(w http.ResponseWriter, r *http.Request) {
  64. id := r.PathValue("id")
  65. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  66. fmt.Fprintf(w, `<!DOCTYPE html>
  67. <html>
  68. <head><title>Article %[1]s</title></head>
  69. <body>
  70. <article>
  71. <h1>Article %[1]s</h1>
  72. <p>%[2]s It is long enough for the content extractor to keep it as the main content of the page.</p>
  73. <p>A second paragraph gives the article some more text, so that it does not look like a navigation block.</p>
  74. </article>
  75. </body>
  76. </html>`, id, ArticleText)
  77. })
  78. return fixture
  79. }
  80. // Subscribe creates a feed for the fixture with the given client and returns its ID.
  81. func (f *FeedFixture) Subscribe(t testing.TB, client *miniflux.Client) int64 {
  82. t.Helper()
  83. feedID, err := client.CreateFeed(&miniflux.FeedCreationRequest{FeedURL: f.FeedURL})
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. return feedID
  88. }
  89. // feedDocument returns an RSS feed with EntryCount entries published a day apart.
  90. // The second entry mentions SearchTerm and has an audio enclosure.
  91. func (f *FeedFixture) feedDocument() string {
  92. var feed strings.Builder
  93. fmt.Fprintf(&feed, `<?xml version="1.0" encoding="UTF-8"?>
  94. <rss version="2.0">
  95. <channel>
  96. <title>%s</title>
  97. <link>%s</link>
  98. <description>Feed served by the integration tests</description>`, FeedTitle, f.WebsiteURL)
  99. for index := 1; index <= EntryCount; index++ {
  100. content := fmt.Sprintf("Content of entry %d.", index)
  101. enclosure := ""
  102. if index == 2 {
  103. content += " It mentions " + SearchTerm + "."
  104. enclosure = fmt.Sprintf(`<enclosure url="%smedia/episode-%d.mp3" length="1024" type="audio/mpeg"/>`, f.WebsiteURL, index)
  105. }
  106. fmt.Fprintf(&feed, `
  107. <item>
  108. <guid isPermaLink="false">entry-%[1]d</guid>
  109. <title>Entry %[1]d</title>
  110. <link>%[2]sentries/%[1]d</link>
  111. <pubDate>%[3]s</pubDate>
  112. <description>%[4]s</description>
  113. %[5]s
  114. </item>`, index, f.WebsiteURL, time.Date(2025, time.January, index, 10, 0, 0, 0, time.UTC).Format(time.RFC1123Z), content, enclosure)
  115. }
  116. feed.WriteString("\n</channel>\n</rss>\n")
  117. return feed.String()
  118. }
  119. func newFixtureIcon(t testing.TB) []byte {
  120. t.Helper()
  121. img := image.NewRGBA(image.Rect(0, 0, 16, 16))
  122. for column := range 16 {
  123. for row := range 16 {
  124. img.Set(column, row, color.RGBA{R: 0x33, G: 0x66, B: 0x99, A: 0xff})
  125. }
  126. }
  127. var buf bytes.Buffer
  128. if err := png.Encode(&buf, img); err != nil {
  129. t.Fatal(err)
  130. }
  131. return buf.Bytes()
  132. }