-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathfilesystem_test.go
50 lines (40 loc) · 970 Bytes
/
filesystem_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package httpfs
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-billy/v5/util"
)
// TestFileSystem tests the HTTP filesystem.
func TestFileSystem(t *testing.T) {
mfs := memfs.New()
err := mfs.MkdirAll("./stuff", 0755)
if err != nil {
t.Fatal(err.Error())
}
data := []byte("hello world!\n")
err = util.WriteFile(mfs, "./stuff/test.txt", data, 0755)
if err != nil {
t.Fatal(err.Error())
}
var hfs http.FileSystem = NewFileSystem(mfs, "/test")
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(hfs))
req := httptest.NewRequest("GET", "/test/stuff/test.txt", nil)
rw := httptest.NewRecorder()
mux.ServeHTTP(rw, req)
res := rw.Result()
if res.StatusCode != 200 {
t.Fatalf("status code: %d", res.StatusCode)
}
readData, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err.Error())
}
if !bytes.Equal(readData, data) {
t.Fail()
}
}