49 lines
989 B
Go
49 lines
989 B
Go
|
|
package htpack
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"io"
|
||
|
|
"net/http/httptest"
|
||
|
|
"os"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/google/go-cmp/cmp"
|
||
|
|
"src.lwithers.me.uk/go/htpack/packed"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestHandler(t *testing.T) {
|
||
|
|
mapped, err := packed.MapAndLoad("packed/testdata/helloworld.pack")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal("loading test packfile:", err)
|
||
|
|
}
|
||
|
|
defer mapped.Close()
|
||
|
|
|
||
|
|
orig, err := os.ReadFile("packed/testdata/helloworld.txt")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal("loading test textfile:", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
h := NewMapped("", mapped)
|
||
|
|
h.AddMapped("/foo", mapped)
|
||
|
|
|
||
|
|
testsv := httptest.NewServer(h)
|
||
|
|
defer testsv.Close()
|
||
|
|
cl := testsv.Client()
|
||
|
|
url := testsv.URL + "/helloworld.txt"
|
||
|
|
|
||
|
|
resp, err := cl.Get(url)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal("executing GET request:", err)
|
||
|
|
}
|
||
|
|
defer resp.Body.Close()
|
||
|
|
|
||
|
|
body, err := io.ReadAll(resp.Body)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal("reading response body:", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if !bytes.Equal(body, orig) {
|
||
|
|
t.Error("difference in response (- got, + expected):")
|
||
|
|
t.Error(cmp.Diff(string(body), string(orig)))
|
||
|
|
}
|
||
|
|
}
|