diff options
author | Aliaksandr Valialkin <valyala@gmail.com> | 2016-01-17 23:12:38 +0200 |
---|---|---|
committer | Aliaksandr Valialkin <valyala@gmail.com> | 2016-01-17 23:12:38 +0200 |
commit | 764a74e2ecd66ce8a34f8321dc40ebffabc58e0d (patch) | |
tree | 2775cee883a09789ac05aec09c61ae7a1f4630df /fs_test.go | |
parent | Properly update Request.URI() after Request.SetRequestURI() call (diff) | |
download | fasthttp-764a74e2ecd66ce8a34f8321dc40ebffabc58e0d.tar.gz fasthttp-764a74e2ecd66ce8a34f8321dc40ebffabc58e0d.tar.bz2 fasthttp-764a74e2ecd66ce8a34f8321dc40ebffabc58e0d.zip |
Added ServeFile and ServeFileUncompressed to be on par with net/http
Diffstat (limited to 'fs_test.go')
-rw-r--r-- | fs_test.go | 58 |
1 files changed, 58 insertions, 0 deletions
@@ -12,6 +12,64 @@ import ( "time" ) +func TestFSServeFileCompressed(t *testing.T) { + var ctx RequestCtx + var req Request + req.SetRequestURI("http://foobar.com/baz") + req.Header.Set("Accept-Encoding", "gzip") + ctx.Init(&req, nil, nil) + + ServeFile(&ctx, "fs.go") + + var resp Response + s := ctx.Response.String() + br := bufio.NewReader(bytes.NewBufferString(s)) + if err := resp.Read(br); err != nil { + t.Fatalf("unexpected error: %s", err) + } + + if string(resp.Header.Peek("Content-Encoding")) != "gzip" { + t.Fatalf("Unexpected 'Content-Encoding' %q. Expecting %q", resp.Header.Peek("Content-Encoding"), "gzip") + } + + body, err := resp.BodyGunzip() + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + expectedBody, err := getFileContents("/fs.go") + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + if !bytes.Equal(body, expectedBody) { + t.Fatalf("unexpected body %q. expecting %q", body, expectedBody) + } +} + +func TestFSServeFileUncompressed(t *testing.T) { + var ctx RequestCtx + var req Request + req.SetRequestURI("http://foobar.com/baz") + ctx.Init(&req, nil, nil) + + ServeFileUncompressed(&ctx, "fs.go") + + var resp Response + s := ctx.Response.String() + br := bufio.NewReader(bytes.NewBufferString(s)) + if err := resp.Read(br); err != nil { + t.Fatalf("unexpected error: %s", err) + } + + body := resp.Body() + expectedBody, err := getFileContents("/fs.go") + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + if !bytes.Equal(body, expectedBody) { + t.Fatalf("unexpected body %q. expecting %q", body, expectedBody) + } +} + func TestFSByteRangeConcurrent(t *testing.T) { fs := &FS{ Root: ".", |