add mastodon test
parent
ddb93ee852
commit
5b76339e40
4
cache.go
4
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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue