-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapper_test.go
More file actions
100 lines (88 loc) · 2.39 KB
/
apper_test.go
File metadata and controls
100 lines (88 loc) · 2.39 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
98
99
100
package goweber
import (
"fmt"
"net/http"
"testing"
"errors"
)
func gMiddleware(r *http.Request) error {
fmt.Println("啓用全局中間件")
return nil
}
func rMiddleware1(r *http.Request) error {
fmt.Println("啓用路由中間件1")
return nil
}
func rMiddleware2(r *http.Request) error {
fmt.Println("啓用路由中間件2")
return errors.New("{'code': 500, 'message': '中間件2認證失敗'}")
}
func TestApp(t *testing.T) {
app := New()
defer app.Close()
// 基礎功能
app.Get("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w,"%s", "Hello World")
})
// 中間件測試
app.Use(gMiddleware)
app.Get("/middleware", func(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("中間件測試成功"))
fmt.Fprintf(w,"%s", "中間件測試成功")
}, rMiddleware1, rMiddleware2)
// 緩存處理,64MB
cache:=NewCacher(64)
app.Get("/cache", func(w http.ResponseWriter, r *http.Request) {
if cache != nil {
if cache.IsCache(w,r) {
fmt.Println("緩存中")
return
}
}
fmt.Fprintf(w,"%s", "緩存響應")
if cache!=nil{
cache.SetCache(r,1,"緩存響應,使用緩存")
}
})
// 处理jwt
jwter:=NewJwter()
app.Get("/jwt/get", func(w http.ResponseWriter, r *http.Request) {
jwter.Key="F6987445"
token, err := jwter.Encode()
if err != nil {
// w.Write([]byte(err.Error()))
fmt.Fprintf(w,"%s", err.Error())
} else {
// w.Write([]byte(token))
fmt.Fprintf(w,"%s", token)
}
})
app.Get("/jwt/check",func(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("token")
err:=jwter.Validate(token)
if err != nil {
// w.Write([]byte("jwt驗證失敗,err:"+err.Error()))
fmt.Fprintf(w,"jwt驗證失敗,err:%s",err.Error())
return
}
// w.Write([]byte("jwt驗證成功,key:"+jwter.Key))
fmt.Fprintf(w,"jwt驗證成功,key:%s",jwter.Key)
})
// * 测试文件上传
uploader := NewFileUploader(10<<20,nil,"./files")
app.Post("/upload", func(w http.ResponseWriter, r *http.Request) {
uploader.FieldName = "s_file"
uploader.FieldNames = "s_files"
uploader.Keyword = "F6987445"
// * 处理文件上传
savePaths,err:=uploader.HandleUpload(r)
if err != nil {
// w.Write([]byte("文件上传失败,err:"+err.Error()))
fmt.Fprintf(w,"文件上传失败,err:%s",err.Error())
return
}
fmt.Fprintf(w, "文件上传成功,保存路径: %v", savePaths)
})
fmt.Println(app)
app.Run()
}