Skip to content

Commit

Permalink
TestStream
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Sep 15, 2023
1 parent 1c843a2 commit d4c807d
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions http/fs/fs_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fs

import (
"bytes"
"context"
"fmt"
"io"
Expand All @@ -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
}
Expand Down

0 comments on commit d4c807d

Please sign in to comment.