Add test for fsSmallFileReader.WriteTo and fix bug (#128)

The bug raises when io.Writer passed to fsSmallFileReader.WriteTo
doesn't support ReadFrom interface.
This commit is contained in:
Victor Gaydov
2016-07-12 10:42:39 +04:00
committed by Aliaksandr Valialkin
parent 006fac3cba
commit a0fe3404bf
2 changed files with 54 additions and 1 deletions
+1 -1
View File
@@ -593,7 +593,7 @@ func (r *fsSmallFileReader) WriteTo(w io.Writer) (int64, error) {
curPos := r.startPos
bufv := copyBufPool.Get()
buf := bufv.([]byte)
for err != nil {
for err == nil {
tailLen := r.endPos - curPos
if tailLen <= 0 {
break
+53
View File
@@ -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