-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem_test.go
168 lines (140 loc) · 5.11 KB
/
problem_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// SPDX-FileCopyrightText: 2018-2024 caixw
//
// SPDX-License-Identifier: MIT
package web
import (
"errors"
"io/fs"
"net/http"
"net/http/httptest"
"testing"
"github.com/issue9/assert/v4"
"github.com/issue9/mux/v9/header"
"github.com/issue9/mux/v9/types"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
var (
_ Responser = &Problem{}
_ error = &Problem{}
)
// 此函数放最前,内有依赖行数的测试,心意减少其行数的变化。
func TestContext_Error(t *testing.T) {
a := assert.New(t, false)
srv := newTestServer(a)
t.Run("id=empty", func(t *testing.T) {
a := assert.New(t, false)
srv.logBuf.Reset()
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/path", nil)
ctx := srv.NewContext(w, r, types.NewContext())
ctx.Error(errors.New("log1 log2"), "").Apply(ctx)
a.Contains(srv.logBuf.String(), "problem_test.go:38"). // NOTE: 此测试依赖上一行的行号
Contains(srv.logBuf.String(), "log1 log2").
Contains(srv.logBuf.String(), header.XRequestID). // 包含 x-request-id 值
Equal(w.Code, http.StatusInternalServerError)
// errs.HTTP
srv.logBuf.Reset()
w = httptest.NewRecorder()
r = httptest.NewRequest(http.MethodGet, "/path", nil)
ctx = srv.NewContext(w, r, types.NewContext())
ctx.Error(NewError(http.StatusBadRequest, errors.New("log1 log2")), "").Apply(ctx)
a.Contains(srv.logBuf.String(), "problem_test.go:50"). // NOTE: 此测试依赖上一行的行号
Contains(srv.logBuf.String(), "log1 log2").
Contains(srv.logBuf.String(), header.XRequestID). // 包含 x-request-id 值
Equal(w.Code, http.StatusBadRequest)
// fs.ErrPermission
srv.logBuf.Reset()
w = httptest.NewRecorder()
r = httptest.NewRequest(http.MethodGet, "/path", nil)
ctx = srv.NewContext(w, r, types.NewContext())
ctx.Error(fs.ErrPermission, "").Apply(ctx)
a.Equal(w.Code, http.StatusForbidden)
// fs.ErrNotExist
srv.logBuf.Reset()
w = httptest.NewRecorder()
r = httptest.NewRequest(http.MethodGet, "/path", nil)
ctx = srv.NewContext(w, r, types.NewContext())
ctx.Error(fs.ErrNotExist, "").Apply(ctx)
a.Equal(w.Code, http.StatusNotFound)
})
t.Run("id=41110", func(t *testing.T) {
a := assert.New(t, false)
srv.logBuf.Reset()
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/path", nil)
ctx := srv.NewContext(w, r, types.NewContext())
ctx.Error(errors.New("log1 log2"), "41110").Apply(ctx)
a.Contains(srv.logBuf.String(), "problem_test.go:81"). // NOTE: 此测试依赖上一行的行号
Contains(srv.logBuf.String(), "log1 log2").
Contains(srv.logBuf.String(), header.XRequestID). // 包含 x-request-id 值
Equal(w.Code, 411)
// errs.HTTP
srv.logBuf.Reset()
w = httptest.NewRecorder()
r = httptest.NewRequest(http.MethodGet, "/path", nil)
ctx = srv.NewContext(w, r, types.NewContext())
ctx.Error(NewError(http.StatusBadRequest, errors.New("log1 log2")), "41110").Apply(ctx)
a.Contains(srv.logBuf.String(), "problem_test.go:93"). // NOTE: 此测试依赖上一行的行号
Contains(srv.logBuf.String(), "log1 log2").
Contains(srv.logBuf.String(), header.XRequestID). // 包含 x-request-id 值
Equal(w.Code, 411)
})
}
func TestProblems_Add(t *testing.T) {
a := assert.New(t, false)
ps := newProblems("")
a.NotNil(ps)
l := len(ps.problems)
a.False(ps.Exists("40010")).
False(ps.Exists("40011")).
True(ps.Exists(ProblemNotFound))
ps.Add(400, []*LocaleProblem{
{ID: "40010", Title: Phrase("title"), Detail: Phrase("detail")},
{ID: "40011", Title: Phrase("title"), Detail: Phrase("detail")},
}...)
a.True(ps.Exists("40010")).
True(ps.Exists("40011")).
Equal(l+2, len(ps.problems))
a.PanicString(func() {
ps.Add(400, []*LocaleProblem{
{ID: "40010", Title: Phrase("title"), Detail: Phrase("detail")},
}...)
}, "存在相同值的 id 参数")
a.PanicString(func() {
ps.Add(99, []*LocaleProblem{
{ID: "40012", Title: Phrase("title"), Detail: Phrase("detail")},
}...)
}, "status 必须是一个有效的状态码")
a.PanicString(func() {
ps.Add(412, []*LocaleProblem{
{ID: "40013", Title: nil, Detail: nil},
}...)
}, "title 不能为空")
}
func TestProblems_initProblem(t *testing.T) {
a := assert.New(t, false)
p := message.NewPrinter(language.SimplifiedChinese)
ps := newProblems("")
a.NotNil(ps)
ps.Add(400, &LocaleProblem{ID: "40010", Title: Phrase("title"), Detail: Phrase("detail")})
pp := &Problem{}
ps.initProblem(pp, "40010", p)
a.Equal(pp.Type, "40010")
ps = newProblems("https://example.com/qa#")
a.NotNil(ps)
ps.Add(400, &LocaleProblem{ID: "40011", Title: Phrase("title"), Detail: Phrase("detail")})
pp = &Problem{}
ps.initProblem(pp, "40011", p)
a.Equal(pp.Type, "https://example.com/qa#40011").
Equal(ps.Prefix(), "https://example.com/qa#")
ps = newProblems(ProblemAboutBlank)
a.NotNil(ps)
ps.Add(400, &LocaleProblem{ID: "40012", Title: Phrase("title"), Detail: Phrase("detail")})
pp = &Problem{}
ps.initProblem(pp, "40012", p)
a.Equal(pp.Type, ProblemAboutBlank)
a.PanicString(func() {
ps.initProblem(pp, "not-exists", p)
}, "未找到有关 not-exists 的定义")
}