// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package api_test import ( "bytes" "fmt" "image" "image/color" "image/png" "net/http" "net/http/httptest" "strings" "testing" "time" miniflux "miniflux.app/v2/client" ) const ( fixtureFeedTitle = "Integration Test Feed" fixtureSubscriptionTitle = "Integration Test Subscription" fixtureEntryCount = 3 // fixtureSearchTerm appears only in the content of the second entry. fixtureSearchTerm = "zanzibar" // fixtureArticleText appears in every article page, but not in the feed. fixtureArticleText = "This paragraph only exists on the original article page." ) // feedFixture is a local website with a feed, an icon and article pages. // The Miniflux server under test fetches it instead of a remote site, // which requires FETCHER_ALLOW_PRIVATE_NETWORKS=1. type feedFixture struct { websiteURL string feedURL string } // newFeedFixture starts a fixture website that is shut down when the test ends. func newFeedFixture(t *testing.T) *feedFixture { t.Helper() icon := newFixtureIcon(t) mux := http.NewServeMux() server := httptest.NewServer(mux) t.Cleanup(server.Close) fixture := &feedFixture{ websiteURL: server.URL + "/", feedURL: server.URL + "/feed.xml", } mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") fmt.Fprintf(w, ` Integration Test Website

Integration Test Website

`, fixtureSubscriptionTitle) }) mux.HandleFunc("GET /feed.xml", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/rss+xml") fmt.Fprint(w, fixture.feedDocument()) }) mux.HandleFunc("GET /icon.png", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "image/png") w.Write(icon) }) mux.HandleFunc("GET /entries/{id}", func(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") w.Header().Set("Content-Type", "text/html; charset=utf-8") fmt.Fprintf(w, ` Article %[1]s

Article %[1]s

%[2]s It is long enough for the content extractor to keep it as the main content of the page.

A second paragraph gives the article some more text, so that it does not look like a navigation block.

`, id, fixtureArticleText) }) return fixture } // subscribe creates a feed for the fixture with the given client and returns its ID. func (f *feedFixture) subscribe(t *testing.T, client *miniflux.Client) int64 { t.Helper() feedID, err := client.CreateFeed(&miniflux.FeedCreationRequest{FeedURL: f.feedURL}) if err != nil { t.Fatal(err) } return feedID } // feedDocument returns an RSS feed with fixtureEntryCount entries published a day apart. // The second entry mentions fixtureSearchTerm and has an audio enclosure. func (f *feedFixture) feedDocument() string { var feed strings.Builder fmt.Fprintf(&feed, ` %s %s Feed served by the API integration tests`, fixtureFeedTitle, f.websiteURL) for i := 1; i <= fixtureEntryCount; i++ { content := fmt.Sprintf("Content of entry %d.", i) enclosure := "" if i == 2 { content += " It mentions " + fixtureSearchTerm + "." enclosure = fmt.Sprintf(``, f.websiteURL, i) } fmt.Fprintf(&feed, ` entry-%[1]d Entry %[1]d %[2]sentries/%[1]d %[3]s %[4]s %[5]s `, i, f.websiteURL, time.Date(2025, time.January, i, 10, 0, 0, 0, time.UTC).Format(time.RFC1123Z), content, enclosure) } feed.WriteString("\n\n\n") return feed.String() } func newFixtureIcon(t *testing.T) []byte { t.Helper() img := image.NewRGBA(image.Rect(0, 0, 16, 16)) for x := range 16 { for y := range 16 { img.Set(x, y, color.RGBA{R: 0x33, G: 0x66, B: 0x99, A: 0xff}) } } var buf bytes.Buffer if err := png.Encode(&buf, img); err != nil { t.Fatal(err) } return buf.Bytes() }