aboutsummaryrefslogtreecommitdiff
path: root/fs_test.go
diff options
context:
space:
mode:
authorGravatar Erik Dubbelboer <erik@dubbelboer.com> 2018-08-17 15:51:44 +0800
committerGravatar Erik Dubbelboer <erik@dubbelboer.com> 2018-08-17 15:51:44 +0800
commite3fd87a86605f83b08944d2c0fd8756a29479fd8 (patch)
treed42254b5acaec7b0c3ece71f531e3b7d34dd971a /fs_test.go
parentMerge pull request #303 from chebyrash/master (diff)
downloadfasthttp-e3fd87a86605f83b08944d2c0fd8756a29479fd8.tar.gz
fasthttp-e3fd87a86605f83b08944d2c0fd8756a29479fd8.tar.bz2
fasthttp-e3fd87a86605f83b08944d2c0fd8756a29479fd8.zip
Added PathNotFound handle func for handling 404.
See https://github.com/erikdubbelboer/fasthttp/pull/10
Diffstat (limited to 'fs_test.go')
-rw-r--r--fs_test.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/fs_test.go b/fs_test.go
index 9eda008..78b1e13 100644
--- a/fs_test.go
+++ b/fs_test.go
@@ -14,6 +14,14 @@ import (
"time"
)
+type TestLogger struct {
+ t *testing.T
+}
+
+func (t TestLogger) Printf(format string, args ...interface{}) {
+ t.t.Logf(format, args...)
+}
+
func TestNewVHostPathRewriter(t *testing.T) {
var ctx RequestCtx
var req Request
@@ -53,6 +61,43 @@ func TestNewVHostPathRewriterMaliciousHost(t *testing.T) {
}
}
+func testPathNotFound(t *testing.T, pathNotFoundFunc RequestHandler) {
+ var ctx RequestCtx
+ var req Request
+ req.SetRequestURI("http//some.url/file")
+ ctx.Init(&req, nil, TestLogger{t})
+
+ fs := &FS{
+ Root: "./",
+ PathNotFound: pathNotFoundFunc,
+ }
+ fs.NewRequestHandler()(&ctx)
+
+ if pathNotFoundFunc == nil {
+ // different to ...
+ if !bytes.Equal(ctx.Response.Body(),
+ []byte("Cannot open requested path")) {
+ t.Fatalf("response defers. Response: %q", ctx.Response.Body())
+ }
+ } else {
+ // Equals to ...
+ if bytes.Equal(ctx.Response.Body(),
+ []byte("Cannot open requested path")) {
+ t.Fatalf("respones defers. Response: %q", ctx.Response.Body())
+ }
+ }
+}
+
+func TestPathNotFound(t *testing.T) {
+ testPathNotFound(t, nil)
+}
+
+func TestPathNotFoundFunc(t *testing.T) {
+ testPathNotFound(t, func(ctx *RequestCtx) {
+ ctx.WriteString("Not found hehe")
+ })
+}
+
func TestServeFileHead(t *testing.T) {
var ctx RequestCtx
var req Request