110 lines
2.9 KiB
Go
110 lines
2.9 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
|
"src.lwithers.me.uk/go/htpack/cmd/htpacker/packer"
|
||
|
)
|
||
|
|
||
|
func TestParseGlobs(t *testing.T) {
|
||
|
ctGlobs, err := parseGlobs([]string{
|
||
|
"*.foo:text/html",
|
||
|
"*.bar:text/plain",
|
||
|
"baz/qux/*.js:application/javascript",
|
||
|
"/abs/file:image/png",
|
||
|
})
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
check := func(pos int, pattern, contentType string, pathComponents int) {
|
||
|
if pos >= len(ctGlobs) {
|
||
|
t.Errorf("entry %d not present", pos)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if pattern != ctGlobs[pos].pattern {
|
||
|
t.Errorf("entry %d: expected pattern %q but got %q",
|
||
|
pos, pattern, ctGlobs[pos].pattern)
|
||
|
}
|
||
|
|
||
|
if contentType != ctGlobs[pos].contentType {
|
||
|
t.Errorf("entry %d: expected content type %q but got %q",
|
||
|
pos, contentType, ctGlobs[pos].contentType)
|
||
|
}
|
||
|
|
||
|
if pathComponents != ctGlobs[pos].pathComponents {
|
||
|
t.Errorf("entry %d: expected num. path components %d but got %d",
|
||
|
pos, pathComponents, ctGlobs[pos].pathComponents)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
check(0, "*.foo", "text/html", 1)
|
||
|
check(1, "*.bar", "text/plain", 1)
|
||
|
check(2, "baz/qux/*.js", "application/javascript", 3)
|
||
|
check(3, "abs/file", "image/png", -1)
|
||
|
}
|
||
|
|
||
|
func TestParseGlobsErrSep(t *testing.T) {
|
||
|
const badValue = "hello/dave.js" // missing ":" separator
|
||
|
_, err := parseGlobs([]string{badValue})
|
||
|
switch err := err.(type) {
|
||
|
case *parseGlobError:
|
||
|
if err.Value != badValue {
|
||
|
t.Errorf("expected value %q but got %q", badValue, err.Value)
|
||
|
}
|
||
|
case nil:
|
||
|
t.Fatal("expected error")
|
||
|
default:
|
||
|
t.Errorf("unexpected error type %T (value %v)", err, err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestParseGlobsErrPattern(t *testing.T) {
|
||
|
const badValue = "[-z]:foo/bar" // malformed character class
|
||
|
_, err := parseGlobs([]string{badValue})
|
||
|
switch err := err.(type) {
|
||
|
case *parseGlobError:
|
||
|
if err.Value != badValue {
|
||
|
t.Errorf("expected value %q but got %q", badValue, err.Value)
|
||
|
}
|
||
|
case nil:
|
||
|
t.Fatal("expected error")
|
||
|
default:
|
||
|
t.Errorf("unexpected error type %T (value %v)", err, err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestApplyContentTypes(t *testing.T) {
|
||
|
// XXX: we program our _expectation_ of content-type into the Filename field
|
||
|
ftp := packer.FilesToPack{
|
||
|
"foo.txt": packer.FileToPack{Filename: "text/plain"},
|
||
|
"baz/foo.txt": packer.FileToPack{Filename: "text/plain"},
|
||
|
|
||
|
"baz/qux.png": packer.FileToPack{Filename: "image/png"},
|
||
|
"foo/qux.png": packer.FileToPack{},
|
||
|
"foo/baz/qux.png": packer.FileToPack{Filename: "image/png"},
|
||
|
|
||
|
"bar.jpeg": packer.FileToPack{},
|
||
|
"foo/baz/bar.jpeg": packer.FileToPack{},
|
||
|
"baz/bar.jpeg": packer.FileToPack{Filename: "image/jpeg"},
|
||
|
}
|
||
|
|
||
|
ctGlobs, err := parseGlobs([]string{
|
||
|
"*.txt:text/plain", // should match anywhere
|
||
|
"baz/qux.png:image/png", // won't match /foo/qux.png
|
||
|
"/baz/bar.jpeg:image/jpeg", // exact prefix match
|
||
|
})
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
ctGlobs.ApplyContentTypes(ftp)
|
||
|
for k, v := range ftp {
|
||
|
if v.Filename != v.ContentType {
|
||
|
t.Errorf("filename %q: expected content type %q but got %q",
|
||
|
k, v.Filename, v.ContentType)
|
||
|
}
|
||
|
}
|
||
|
}
|