Handler: add NewMapped, AddMapped
The Handler now supports serving multiple packfiles, possibly with different prefixes. Precedence is given by order in which packfiles are registered.
This commit is contained in:
parent
5bece326fb
commit
5b8873a4f5
3 changed files with 149 additions and 36 deletions
49
handler_test.go
Normal file
49
handler_test.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
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)))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue