integration_fixture_test.go 4.4 KB

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