aboutsummaryrefslogtreecommitdiff
path: root/fs_test.go
diff options
context:
space:
mode:
authorGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-12-09 18:52:23 +0200
committerGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-12-09 18:52:23 +0200
commitebf9968b68ee45cd8af53f3b2c7d141cc8f29cc6 (patch)
tree5aae67a81adc2a51f77c7cc5b6e23afc8300463e /fs_test.go
parentAdded support for If-Modified-Since to SendFile and FS (diff)
downloadfasthttp-ebf9968b68ee45cd8af53f3b2c7d141cc8f29cc6.tar.gz
fasthttp-ebf9968b68ee45cd8af53f3b2c7d141cc8f29cc6.tar.bz2
fasthttp-ebf9968b68ee45cd8af53f3b2c7d141cc8f29cc6.zip
Renamed fshandler to fs
Diffstat (limited to 'fs_test.go')
-rw-r--r--fs_test.go166
1 files changed, 166 insertions, 0 deletions
diff --git a/fs_test.go b/fs_test.go
new file mode 100644
index 0000000..c757e35
--- /dev/null
+++ b/fs_test.go
@@ -0,0 +1,166 @@
+package fasthttp
+
+import (
+ "bytes"
+ "io/ioutil"
+ "os"
+ "sort"
+ "testing"
+ "time"
+)
+
+func TestFSHandlerSingleThread(t *testing.T) {
+ requestHandler := FSHandler(".", 0)
+
+ f, err := os.Open(".")
+ if err != nil {
+ t.Fatalf("cannot open cwd: %s", err)
+ }
+
+ filenames, err := f.Readdirnames(0)
+ f.Close()
+ if err != nil {
+ t.Fatalf("cannot read dirnames in cwd: %s", err)
+ }
+ sort.Sort(sort.StringSlice(filenames))
+
+ for i := 0; i < 3; i++ {
+ fsHandlerTest(t, requestHandler, filenames)
+ }
+}
+
+func TestFSHandlerConcurrent(t *testing.T) {
+ requestHandler := FSHandler(".", 0)
+
+ f, err := os.Open(".")
+ if err != nil {
+ t.Fatalf("cannot open cwd: %s", err)
+ }
+
+ filenames, err := f.Readdirnames(0)
+ f.Close()
+ if err != nil {
+ t.Fatalf("cannot read dirnames in cwd: %s", err)
+ }
+ sort.Sort(sort.StringSlice(filenames))
+
+ concurrency := 10
+ ch := make(chan struct{}, concurrency)
+ for j := 0; j < concurrency; j++ {
+ go func() {
+ for i := 0; i < 3; i++ {
+ fsHandlerTest(t, requestHandler, filenames)
+ }
+ ch <- struct{}{}
+ }()
+ }
+
+ for j := 0; j < concurrency; j++ {
+ select {
+ case <-ch:
+ case <-time.After(time.Second):
+ t.Fatalf("timeout")
+ }
+ }
+}
+
+func fsHandlerTest(t *testing.T, requestHandler RequestHandler, filenames []string) {
+ var ctx RequestCtx
+ var req Request
+ ctx.Init(&req, nil, defaultLogger)
+ ctx.Request.Header.SetHost("foobar.com")
+
+ filesTested := 0
+ for _, name := range filenames {
+ f, err := os.Open(name)
+ if err != nil {
+ t.Fatalf("cannot open file %q: %s", name, err)
+ }
+ stat, err := f.Stat()
+ if err != nil {
+ t.Fatalf("cannot get file stat %q: %s", name, err)
+ }
+ if stat.IsDir() {
+ f.Close()
+ continue
+ }
+ data, err := ioutil.ReadAll(f)
+ f.Close()
+ if err != nil {
+ t.Fatalf("cannot read file contents %q: %s", name, err)
+ }
+
+ ctx.URI().Update(name)
+ requestHandler(&ctx)
+ if ctx.Response.bodyStream == nil {
+ t.Fatalf("response body stream must be non-empty")
+ }
+ body, err := ioutil.ReadAll(ctx.Response.bodyStream)
+ if err != nil {
+ t.Fatalf("error when reading response body stream: %s", err)
+ }
+ if !bytes.Equal(body, data) {
+ t.Fatalf("unexpected body returned: %q. Expecting %q", body, data)
+ }
+ filesTested++
+ if filesTested >= 10 {
+ break
+ }
+ }
+
+ // verify index page generation
+ ctx.URI().Update("/")
+ requestHandler(&ctx)
+ if ctx.Response.bodyStream == nil {
+ t.Fatalf("response body stream must be non-empty")
+ }
+ body, err := ioutil.ReadAll(ctx.Response.bodyStream)
+ if err != nil {
+ t.Fatalf("error when reading response body stream: %s", err)
+ }
+ if len(body) == 0 {
+ t.Fatalf("index page must be non-empty")
+ }
+}
+
+func TestStripPathSlashes(t *testing.T) {
+ testStripPathSlashes(t, "", 0, "")
+ testStripPathSlashes(t, "", 10, "")
+ testStripPathSlashes(t, "/", 0, "")
+ testStripPathSlashes(t, "/", 1, "")
+ testStripPathSlashes(t, "/", 10, "")
+ testStripPathSlashes(t, "/foo/bar/baz", 0, "/foo/bar/baz")
+ testStripPathSlashes(t, "/foo/bar/baz", 1, "/bar/baz")
+ testStripPathSlashes(t, "/foo/bar/baz", 2, "/baz")
+ testStripPathSlashes(t, "/foo/bar/baz", 3, "")
+ testStripPathSlashes(t, "/foo/bar/baz", 10, "")
+
+ // trailing slash
+ testStripPathSlashes(t, "/foo/bar/", 0, "/foo/bar")
+ testStripPathSlashes(t, "/foo/bar/", 1, "/bar")
+ testStripPathSlashes(t, "/foo/bar/", 2, "")
+ testStripPathSlashes(t, "/foo/bar/", 3, "")
+}
+
+func testStripPathSlashes(t *testing.T, path string, stripSlashes int, expectedPath string) {
+ s := stripLeadingSlashes([]byte(path), stripSlashes)
+ s = stripTrailingSlashes(s)
+ if string(s) != expectedPath {
+ t.Fatalf("unexpected path after stripping %q with stripSlashes=%d: %q. Expecting %q", path, stripSlashes, s, expectedPath)
+ }
+}
+
+func TestFileExtension(t *testing.T) {
+ testFileExtension(t, "foo.bar", ".bar")
+ testFileExtension(t, "foobar", "")
+ testFileExtension(t, "foo.bar.baz", ".baz")
+ testFileExtension(t, "", "")
+ testFileExtension(t, "/a/b/c.d/efg.jpg", ".jpg")
+}
+
+func testFileExtension(t *testing.T, path, expectedExt string) {
+ ext := fileExtension(path)
+ if ext != expectedExt {
+ t.Fatalf("unexpected file extension for file %q: %q. Expecting %q", path, ext, expectedExt)
+ }
+}