-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject_test.go
More file actions
90 lines (70 loc) · 2.05 KB
/
inject_test.go
File metadata and controls
90 lines (70 loc) · 2.05 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
package ginx
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/swaggo/swag"
"github.com/xiaoshicae/ginx/options"
"github.com/xiaoshicae/xone"
)
func TestInjectSwaggerInfoNil(t *testing.T) {
gin.SetMode(gin.TestMode)
engine := gin.New()
// 测试 nil swaggerInfo 不 panic
injectSwaggerInfo(nil, engine)
// 测试 nil engine 不 panic
injectSwaggerInfo(&swag.Spec{}, nil)
}
func TestInjectSwaggerInfoWithSpec(t *testing.T) {
gin.SetMode(gin.TestMode)
engine := gin.New()
spec := &swag.Spec{
InfoInstanceName: "test",
SwaggerTemplate: "{}",
}
injectSwaggerInfo(spec, engine)
// 验证 swagger 路由被注册
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/swagger/index.html", nil)
engine.ServeHTTP(w, req)
// 应该返回 200 或 301 重定向
if w.Code != http.StatusOK && w.Code != http.StatusMovedPermanently && w.Code != http.StatusNotFound {
t.Logf("swagger route response code: %d", w.Code)
}
}
func TestInjectSwaggerInfoWithUrlPrefix(t *testing.T) {
gin.SetMode(gin.TestMode)
engine := gin.New()
spec := &swag.Spec{
InfoInstanceName: "test",
SwaggerTemplate: "{}",
}
injectSwaggerInfo(spec, engine, options.WithSwaggerUrlPrefix("/api/v1"))
// 验证带前缀的 swagger 路由被注册
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/swagger/index.html", nil)
engine.ServeHTTP(w, req)
// 应该有响应(不是 404)
t.Logf("prefixed swagger route response code: %d", w.Code)
}
func TestInjectPrintBanner(t *testing.T) {
gin.SetMode(gin.TestMode)
engine := gin.New()
// 测试不 panic
injectPrintBanner(engine)
// 验证函数被注入到 FuncMap
if engine.FuncMap[xone.PrintBannerFuncKey] == nil {
t.Error("PrintBanner should be injected into FuncMap")
}
}
func TestSwaggerInfoFuncMapKey(t *testing.T) {
if xone.SwaggerInfoFuncKey == "" {
t.Error("SwaggerInfoFuncKey should not be empty")
}
}
func TestPrintBannerFuncMapKey(t *testing.T) {
if xone.PrintBannerFuncKey == "" {
t.Error("PrintBannerFuncKey should not be empty")
}
}