From 1c843a223996b57605deed1575f72fbc01d5e255 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 15 Sep 2023 08:46:37 +0800 Subject: [PATCH 1/2] testFile: Http, WithTracker, SequenceFile --- http/fs/fs_test.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/http/fs/fs_test.go b/http/fs/fs_test.go index bae8a34..4fc1138 100644 --- a/http/fs/fs_test.go +++ b/http/fs/fs_test.go @@ -17,6 +17,14 @@ import ( // ----------------------------------------------------------------------------------------- +type errCloser struct { + io.Reader +} + +func (p *errCloser) Close() error { + return fs.ErrPermission +} + type statCloser interface { io.Closer Stat() (fs.FileInfo, error) @@ -84,9 +92,17 @@ func TestHttpFile(t *testing.T) { w.WriteHeader(500) })) err500 := fmt.Errorf("http.Get %s error: status %d (%s)", "http://c.com/", 500, "") - testFile(t, Http("http://a.com", context.TODO()).With(mockClient, nil), &expectFile{name: "/", readdirErr: fs.ErrInvalid, readN: 1}) - testFile(t, Http("http://b.com").With(mockClient, nil), &expectFile{name: "/", openErr: &fs.PathError{Op: "http.Get", Path: "http://b.com/", Err: fs.ErrNotExist}}) - testFile(t, Http("http://c.com").With(mockClient, nil), &expectFile{name: "/", openErr: &fs.PathError{Op: "http.Get", Path: "http://c.com/", Err: err500}}) + aCom := Http("http://a.com", context.TODO()).With(mockClient, nil) + bCom := Http("http://b.com").With(mockClient, nil) + cCom := Http("http://c.com").With(mockClient, nil) + testFile(t, aCom, &expectFile{name: "/", readdirErr: fs.ErrInvalid, readN: 1}) + testFile(t, bCom, &expectFile{name: "/", openErr: &fs.PathError{Op: "http.Get", Path: "http://b.com/", Err: fs.ErrNotExist}}) + testFile(t, cCom, &expectFile{name: "/", openErr: &fs.PathError{Op: "http.Get", Path: "http://c.com/", Err: err500}}) + track := WithTracker(Root(), "http://a.com", ".txt").(*fsWithTracker) + track.httpfs = aCom + testFile(t, track, &expectFile{name: "/foo.txt", readdirErr: fs.ErrInvalid, readN: 1}) + testFile(t, WithTracker(bCom, aCom, ".txt"), &expectFile{name: "/bar.jpg", openErr: &fs.PathError{Op: "http.Get", Path: "http://b.com/bar.jpg", Err: fs.ErrNotExist}}) + testFile(t, SequenceFile("/foo/bar.txt", &errCloser{strings.NewReader("a")}), &expectFile{close: fs.ErrPermission, readdirErr: fs.ErrInvalid, readN: 1}) } var ( From d4c807d406f2c42e26578b936f22d9c0c3c59dc9 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 15 Sep 2023 10:48:02 +0800 Subject: [PATCH 2/2] TestStream --- http/fs/fs_test.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/http/fs/fs_test.go b/http/fs/fs_test.go index 4fc1138..6c669ad 100644 --- a/http/fs/fs_test.go +++ b/http/fs/fs_test.go @@ -1,6 +1,7 @@ package fs import ( + "bytes" "context" "fmt" "io" @@ -17,6 +18,73 @@ import ( // ----------------------------------------------------------------------------------------- +type iStream interface { + http.File + TryReader() *bytes.Reader + FullName() string + Size() int64 + ModTime() time.Time +} + +type expectStm struct { + br *bytes.Reader + fullName string + readN int + readErr error + size int64 + modt time.Time + unseek io.ReadCloser +} + +type sizeModt struct { + errCloser +} + +func (sizeModt) Size() int64 { + return 123 +} + +func (sizeModt) ModTime() time.Time { + return time.Time{} +} + +func testIS(t *testing.T, stm iStream, exp *expectStm) { + ts := ts.New(t) + ts.Case("TryReader", stm.TryReader()).Equal(exp.br) + ts.Case("FullName", stm.FullName()).Equal(exp.fullName) + ts.New("stm.Read").Init(stm.Read(make([]byte, 1))).Equal(exp.readN, exp.readErr) + ts.Case("stm.Size", stm.Size()).Equal(exp.size) + ts.Case("stm.ModTime", stm.ModTime()).Equal(exp.modt) + ts.Case("Unseekable", Unseekable(stm) != exp.unseek).Equal(true) +} + +func TestStream(t *testing.T) { + br := bytes.NewReader([]byte("a")) + file := &sizeModt{} + stm := &stream{br: br, file: file} + testIS(t, stm, &expectStm{br: br, readN: 1, size: 123}) + br.Seek(0, io.SeekStart) + ti := time.Now().Format(http.TimeFormat) + stm2 := &httpFile{br: br, file: file, resp: &http.Response{ContentLength: 123, Body: file, Header: http.Header{ + "Last-Modified": []string{ti}, + }}} + modt, err := http.ParseTime(ti) + if err != nil { + t.Fatal("http.ParseTime:", err) + } + testIS(t, stm2, &expectStm{br: br, readN: 1, size: 123, modt: modt}) + stm.br = nil + if f := Unseekable(stm); f != file { + t.Fatal("Unseekable:", f) + } + stm2.br = nil + if f := Unseekable(stm2); f != file { + t.Fatal("Unseekable:", f) + } +} + +// ----------------------------------------------------------------------------------------- + type errCloser struct { io.Reader }