diff options
author | Victor Gaydov <victor@enise.org> | 2016-07-12 10:42:39 +0400 |
---|---|---|
committer | Aliaksandr Valialkin <valyala@gmail.com> | 2016-07-12 09:42:39 +0300 |
commit | a0fe3404bf91136124e8416ddabd03998a89e826 (patch) | |
tree | 112975c95c5e1ef3cdf08092c42949bb3a19ddfc /fs_test.go | |
parent | Implement VisitUserValues - https://github.com/valyala/fasthttp/issues/126 (#... (diff) | |
download | fasthttp-a0fe3404bf91136124e8416ddabd03998a89e826.tar.gz fasthttp-a0fe3404bf91136124e8416ddabd03998a89e826.tar.bz2 fasthttp-a0fe3404bf91136124e8416ddabd03998a89e826.zip |
Add test for fsSmallFileReader.WriteTo and fix bug (#128)
The bug raises when io.Writer passed to fsSmallFileReader.WriteTo
doesn't support ReadFrom interface.
Diffstat (limited to 'fs_test.go')
-rw-r--r-- | fs_test.go | 53 |
1 files changed, 53 insertions, 0 deletions
@@ -4,9 +4,11 @@ import ( "bufio" "bytes" "fmt" + "io" "io/ioutil" "math/rand" "os" + "path" "sort" "testing" "time" @@ -88,6 +90,57 @@ func TestServeFileHead(t *testing.T) { } } +func TestServeFileSmallNoReadFrom(t *testing.T) { + teststr := "hello, world!" + + tempdir, err := ioutil.TempDir("", "httpexpect") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tempdir) + + if err := ioutil.WriteFile( + path.Join(tempdir, "hello"), []byte(teststr), 0666); err != nil { + t.Fatal(err) + } + + var ctx RequestCtx + var req Request + req.SetRequestURI("http://foobar.com/baz") + ctx.Init(&req, nil, nil) + + ServeFile(&ctx, path.Join(tempdir, "hello")) + + reader, ok := ctx.Response.bodyStream.(*fsSmallFileReader) + if !ok { + t.Fatal("expected fsSmallFileReader") + } + + buf := bytes.NewBuffer(nil) + + n, err := reader.WriteTo(pureWriter{buf}) + if err != nil { + t.Fatal(err) + } + + if n != int64(len(teststr)) { + t.Fatalf("expected %d bytes, got %d bytes", len(teststr), n) + } + + body := string(buf.Bytes()) + if body != teststr { + t.Fatalf("expected '%s'", teststr) + } +} + +type pureWriter struct { + w io.Writer +} + +func (pw pureWriter) Write(p []byte) (nn int, err error) { + return pw.w.Write(p) +} + func TestServeFileCompressed(t *testing.T) { var ctx RequestCtx var req Request |