Added a simple integration test

This commit is contained in:
James Mills 2016-09-14 16:42:30 +10:00
parent a4095fac05
commit fa09d1002b
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6

View File

@ -3,6 +3,8 @@ package gopher_test
import (
"fmt"
"log"
"os"
"testing"
"github.com/prologic/go-gopher"
)
@ -28,3 +30,46 @@ func Example_fileserver() {
gopher.Handle("/", gopher.FileServer(gopher.Dir("/tmp")))
log.Fatal(gopher.ListenAndServe("localhost:7000", nil))
}
func TestGet(t *testing.T) {
res, err := gopher.Get("gopher://localhost:7000/1hello")
if err != nil {
t.Fatal(err)
}
b, err := res.Dir.ToText()
if err != nil {
t.Fatal(err)
}
t.Logf("res: %s", string(b))
if len(res.Dir) == 0 {
t.Fatal("expected items but none found")
}
i := res.Dir[0]
if i.Type != gopher.INFO {
log.Fatalf("expected INFO item %s found", i.Type)
}
if i.Description != "Hello World!" {
log.Fatal("expected \"Hello World!\" as description")
}
}
func TestMain(m *testing.M) {
log.Print("Setup...")
gopher.HandleFunc("/hello", hello)
go func() {
log.Fatal(gopher.ListenAndServe("localhost:7000", nil))
}()
retcode := m.Run()
log.Printf(" Return: %q", retcode)
log.Print("Teardown...")
os.Exit(retcode)
}