diff --git a/cache.go b/cache.go index a0baef9..8643d34 100644 --- a/cache.go +++ b/cache.go @@ -8,6 +8,8 @@ import ( type HTTPCache struct { } +// Based on http.MaxBytesReader + // MaxBytesReader is similar to io.LimitReader but is intended for // limiting the size of incoming request bodies. In contrast to // io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a @@ -16,8 +18,6 @@ type HTTPCache struct { // // MaxBytesReader prevents clients from accidentally or maliciously // sending a large request and wasting server resources. -// -// Based on http.MaxBytesReader func MaxBytesReader(r io.ReadCloser, n int64) io.ReadCloser { if n < 0 { // Treat negative limits as equivalent to 0. n = 0 diff --git a/mastodon_test.go b/mastodon_test.go new file mode 100644 index 0000000..c61d377 --- /dev/null +++ b/mastodon_test.go @@ -0,0 +1,48 @@ +package main + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" +) + +var mastoSourceTests = []struct { + filename string + title string + itemText string +}{ + { + filename: "/masto.html", + title: "magical on tiny.tilde.website", + itemText: "here's hoping it never melts", + }, +} + +func TestMastoSource(t *testing.T) { + server := httptest.NewServer(http.FileServer(http.Dir("testdata"))) + defer server.Close() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + for _, tt := range mastoSourceTests { + src := NewMastoSource(server.URL + tt.filename) + src.update(ctx) + if err := src.Error; err != nil { + t.Errorf("%v: Error = %v, expected nil", tt.filename, err) + continue + } + if src.Title != tt.title { + t.Errorf("%v: Title = %v expected %v", tt.filename, src.Title, tt.title) + } + items := src.GetItems() + if len(items) == 0 { + t.Errorf("%v: GetItems() = %v expected len > 0", tt.filename, items) + } else { + x := items[0] + if x.Text != tt.itemText { + t.Errorf("%v: item 0 Text = %q expected %q", tt.filename, x.Text, tt.itemText) + } + } + } +} diff --git a/testdata/masto.html b/testdata/masto.html new file mode 100644 index 0000000..868ffd8 --- /dev/null +++ b/testdata/masto.html @@ -0,0 +1,1091 @@ + + +
+ + + + + + + + + + +the ball is in your court y'all
https://www.intomore.com/health/91-lgbtq-folks-fully-vaccinated/
the speedrun for this game is going to be so sick
oh man they set the tension up in the xenomorph research facility perfectly though
★ hacker, musician, pokémon master ★