diff options
author | Erik Dubbelboer <erik@dubbelboer.com> | 2018-08-17 15:51:44 +0800 |
---|---|---|
committer | Erik Dubbelboer <erik@dubbelboer.com> | 2018-08-17 15:51:44 +0800 |
commit | e3fd87a86605f83b08944d2c0fd8756a29479fd8 (patch) | |
tree | d42254b5acaec7b0c3ece71f531e3b7d34dd971a /fs_test.go | |
parent | Merge pull request #303 from chebyrash/master (diff) | |
download | fasthttp-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.go | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -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 |