add coverage test

This commit is contained in:
Pierre Dubouilh
2021-12-22 14:29:46 +01:00
committed by Pierre Dubouilh
parent 5b55ab0a0a
commit 65a6087c60
4 changed files with 66 additions and 20 deletions
+31 -5
View File
@@ -30,11 +30,17 @@ func getRaw(t *testing.T, url string) []byte {
return body
}
func getZip(t *testing.T, dest string) []*zip.File {
func getZip(t *testing.T, needle string, dest string) (int, bool) {
b := getRaw(t, dest)
archive, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
unzipped, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
dieMaybe(t, err)
return archive.File
length := len(unzipped.File)
for _, file := range unzipped.File {
if file.Name == needle {
return length, true
}
}
return length, false
}
func get(t *testing.T, url string) string {
@@ -103,6 +109,13 @@ func doTestRegular(t *testing.T, url string, testExtra bool) {
fmt.Println("\r\n~~~~~~~~~~ test fetching default path")
fetchAndTestDefault(t, url)
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test fetching another page")
body0 = get(t, url+"/hols")
if !strings.Contains(body0, "glasgow.jpg") {
t.Fatal("fetching a subfolder failed")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test fetching an invalid path - redirected to root")
fetchAndTestDefault(t, url+"../../")
@@ -127,11 +140,20 @@ func doTestRegular(t *testing.T, url string, testExtra bool) {
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test zipping of folder 中文")
files := getZip(t, url+"zip?zipPath=%2F%E4%B8%AD%E6%96%87%2F&zipName=%E4%B8%AD%E6%96%87")
if len(files) != 1 || files[0].Name != "檔案.html" {
len, foundFile := getZip(t, "檔案.html", url+"zip?zipPath=%2F%E4%B8%AD%E6%96%87%2F&zipName=%E4%B8%AD%E6%96%87")
if len != 1 || !foundFile {
t.Fatal("invalid zip generated")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test zipping of folder with hidden file")
_, foundHidden := getZip(t, ".hidden-folder/some-file", url+"zip?zipPath=%2fhols%2f&zipName=hols")
if foundHidden && !testExtra {
t.Fatal("invalid zip generated - shouldnt contain hidden folder")
} else if !foundHidden && testExtra {
t.Fatal("invalid zip generated - should contain hidden folder")
}
// ~~~~~~~~~~~~~~~~~
fmt.Println("\r\n~~~~~~~~~~ test zip invalid path")
body0 = get(t, url+"zip?zipPath=%2Ftmp&zipName=subdir")
@@ -344,3 +366,7 @@ func TestRo(t *testing.T) {
fmt.Println("========== testing read only ============")
doTestReadonly(t, "http://127.0.0.1:8001/")
}
func TestRunMain(t *testing.T) {
main()
}