-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_test.go
124 lines (98 loc) · 2.79 KB
/
file_test.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package pgo2
import (
"bytes"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"github.com/pinguo/pgo2/iface"
"github.com/pinguo/pgo2/logs"
)
func TestNewFile(t *testing.T) {
var obj interface{}
obj = NewFile(nil)
if _, ok := obj.(iface.IPlugin); ok == false {
t.FailNow()
}
}
func TestFile_SetExcludeExtensions(t *testing.T) {
file := NewFile(nil)
var Extensions []interface{}
Extensions = []interface{}{".html", ".htm"}
file.SetExcludeExtensions(Extensions)
if len(file.excludeExtensions) != len(Extensions) {
t.FailNow()
}
}
func TestFile_HandleRequest(t *testing.T) {
App(true).Log().SetTarget(logs.TargetConsole, &mockTarget{})
App().publicPath, _ = filepath.Abs("./test/data")
SetAlias("@public", App().publicPath)
method := "GET"
body := bytes.NewReader([]byte(""))
t.Run("pathEmpty", func(t *testing.T) {
r := httptest.NewRequest(method, "/view", body)
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(false,true, r, w)
file := NewFile(nil)
file.HandleRequest(context)
if w.Body.String() != "" {
t.Fatal(`w.Body.String() !=""`)
}
if w.Result().Header.Get("Content-Type") != "" {
t.Fatal(`w.Result().Header.Get("Content-Type")!=""`)
}
})
t.Run("excludeExtensions", func(t *testing.T) {
r := httptest.NewRequest(method, "/view.html", body)
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(false,true, r, w)
file := NewFile(nil)
file.SetExcludeExtensions([]interface{}{".html"})
file.HandleRequest(context)
if w.Body.String() != "" {
t.Fatal(`w.Body.String() !=""`)
}
if w.Result().Header.Get("Content-Type") != "" {
t.Fatal(`w.Result().Header.Get("Content-Type")!=""`)
}
})
t.Run("methodErr", func(t *testing.T) {
r := httptest.NewRequest("POST", "/view.html", body)
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(false,true, r, w)
file := NewFile(nil)
file.HandleRequest(context)
if w.Code != 405 {
t.Fatal(`w.Code != 405`)
}
})
t.Run("pathNotExist", func(t *testing.T) {
r := httptest.NewRequest(method, "/viewNotExists.html", body)
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(false,true, r, w)
file := NewFile(nil)
file.HandleRequest(context)
if w.Code != 404 {
t.Fatal(` w.Code !=404`)
}
})
t.Run("ServeContent", func(t *testing.T) {
r := httptest.NewRequest(method, "/view.html", body)
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(false,true, r, w)
file := NewFile(nil)
file.HandleRequest(context)
if strings.Index(w.Body.String(), "test view") < 0 {
t.Fatal("strings.Index(w.Body.String(), \"test view\") < 0")
}
if w.Result().Header.Get("Content-Type") != "text/html; charset=utf-8" {
t.Fatal(`w.Result().Header.Get("Content-Type")!="text/html; charset=utf-8"`)
}
})
}