forked from gramework/gramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_router_test.go
More file actions
97 lines (77 loc) · 2.22 KB
/
Copy pathapp_router_test.go
File metadata and controls
97 lines (77 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2017-present Kirill Danshin and Gramework contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
package gramework
import (
"io/ioutil"
"os"
"strings"
"testing"
"github.com/valyala/fasthttp"
)
func testProvideTempFile(t *testing.T, action func(file, dir string)) {
tmpFile, err := ioutil.TempFile(os.TempDir(), "servedirtmp_")
if err != nil {
t.Error("cannot create temporary file:", err)
}
defer os.Remove(tmpFile.Name())
// write some text into file
text := []byte("test_file_data")
if _, err = tmpFile.Write(text); err != nil {
t.Error("failed to write to temporary file:", err)
}
if err := tmpFile.Close(); err != nil {
t.Error(err)
}
tempFName := strings.Replace(tmpFile.Name(), os.TempDir(), "", -1)
action(tempFName, os.TempDir())
}
func TestAppServeDir(t *testing.T) {
var funcSD func(*Context)
app := New()
ctx := Context{RequestCtx: &fasthttp.RequestCtx{}}
testProvideTempFile(t, func(file, dir string) {
ctx.Request.SetRequestURI(file)
funcSD = app.ServeDir(dir)
funcSD(&ctx)
})
if ctx.Response.StatusCode() != fasthttp.StatusOK {
t.Errorf("failed on file read by directory server %d", ctx.Response.StatusCode())
}
}
func TestAppServeDirNoCache(t *testing.T) {
var funcSD func(*Context)
const (
hdrCacheControl = "no-cache, no-store, must-revalidate"
hdrPragma = "no-cache"
hdrExpires = "0"
)
app := New()
ctx := Context{RequestCtx: &fasthttp.RequestCtx{}}
testProvideTempFile(t, func(file, dir string) {
ctx.Request.SetRequestURI(file)
funcSD = app.ServeDirNoCache(dir)
funcSD(&ctx)
})
if ctx.Response.StatusCode() != fasthttp.StatusOK {
t.Errorf("failed on directory serve (no cache) %d", ctx.Response.StatusCode())
}
hdrList := []struct {
name string
exp string
}{
{"Cache-Control", hdrCacheControl},
{"Pragma", hdrPragma},
{"Expires", hdrExpires},
}
for _, hdr := range hdrList {
if act := string(ctx.Response.Header.Peek(hdr.name)); act != hdr.exp {
t.Errorf("wrong header '%s' check: %s, but %s expected", hdr.name, act, hdr.exp)
}
}
}