You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
997 B
60 lines
997 B
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"os"
|
|
"testing/fstest"
|
|
)
|
|
|
|
var fs = http.FileServer(http.Dir("./testdata"))
|
|
|
|
var srv = httptest.NewServer(fs)
|
|
|
|
func TestDownload(t *testing.T) {
|
|
var (
|
|
b bytes.Buffer
|
|
c = make(chan request)
|
|
)
|
|
|
|
url := srv.URL + "/somefile"
|
|
t.Logf("file URL: %s", url)
|
|
|
|
go func() { c <- request{&b, url}; close(c) }()
|
|
Download(c)
|
|
|
|
want := "somefile inner data"
|
|
if got := b.String(); got != want {
|
|
t.Errorf("file buffer received unexpected value\nGot:\n%s\nWant:\n%s", got, want)
|
|
}
|
|
}
|
|
|
|
func TestRead(t *testing.T) {
|
|
input := `boston
|
|
http://localhost/someboston.jpg
|
|
http://localhost/anotherboston.mp4
|
|
|
|
orlando
|
|
http://localhost/someorlando.jpg
|
|
`
|
|
dir := t.TempDir()
|
|
t.Logf("root directory: %s", dir)
|
|
|
|
c := Read(dir, strings.NewReader(input))
|
|
for range c {
|
|
} // drain the channel
|
|
|
|
err := fstest.TestFS(
|
|
os.DirFS(dir),
|
|
"boston0.jpg",
|
|
"boston1.mp4",
|
|
"orlando0.jpg",
|
|
)
|
|
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|