forked from gofiber/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n_test.go
250 lines (234 loc) · 5.47 KB
/
i18n_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package fiberi18n
import (
"context"
"io"
"net/http"
"sync"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
)
func newServer() *fiber.App {
app := fiber.New()
app.Use(New())
app.Get("/", func(ctx *fiber.Ctx) error {
return ctx.SendString(MustLocalize(ctx, "welcome"))
})
app.Get("/:name", func(ctx *fiber.Ctx) error {
return ctx.SendString(MustLocalize(ctx, &i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": ctx.Params("name"),
},
}))
})
return app
}
var i18nApp = newServer()
func makeRequest(lang language.Tag, name string, app *fiber.App) (*http.Response, error) {
path := "/" + name
req, _ := http.NewRequestWithContext(context.Background(), "GET", path, nil)
if lang != language.Und {
req.Header.Add("Accept-Language", lang.String())
}
req.Method = "GET"
req.RequestURI = path
resp, err := app.Test(req)
return resp, err
}
func TestI18nEN(t *testing.T) {
t.Parallel()
type args struct {
lang language.Tag
name string
}
tests := []struct {
name string
args args
want string
}{
{
name: "hello world",
args: args{
name: "",
lang: language.English,
},
want: "hello",
},
{
name: "hello alex",
args: args{
name: "alex",
lang: language.English,
},
want: "hello alex",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := makeRequest(tt.args.lang, tt.args.name, i18nApp)
utils.AssertEqual(t, err, nil)
body, err := io.ReadAll(got.Body)
utils.AssertEqual(t, err, nil)
utils.AssertEqual(t, tt.want, string(body))
})
}
}
func TestI18nZH(t *testing.T) {
type args struct {
lang language.Tag
name string
}
tests := []struct {
name string
args args
want string
}{
{
name: "hello world",
args: args{
name: "",
lang: language.Chinese,
},
want: "你好",
},
{
name: "hello alex",
args: args{
name: "alex",
lang: language.Chinese,
},
want: "你好 alex",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := makeRequest(tt.args.lang, tt.args.name, i18nApp)
utils.AssertEqual(t, err, nil)
body, err := io.ReadAll(got.Body)
utils.AssertEqual(t, err, nil)
utils.AssertEqual(t, tt.want, string(body))
})
}
}
func TestParallelI18n(t *testing.T) {
type args struct {
lang language.Tag
name string
}
tests := []struct {
name string
args args
want string
}{
{
name: "hello world",
args: args{
name: "",
lang: language.Chinese,
},
want: "你好",
},
{
name: "hello alex",
args: args{
name: "alex",
lang: language.Chinese,
},
want: "你好 alex",
},
{
name: "hello peter",
args: args{
name: "peter",
lang: language.English,
},
want: "hello peter",
},
}
t.Parallel()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := makeRequest(tt.args.lang, tt.args.name, i18nApp)
utils.AssertEqual(t, err, nil)
body, err := io.ReadAll(got.Body)
utils.AssertEqual(t, err, nil)
utils.AssertEqual(t, tt.want, string(body))
})
}
}
func TestLocalize(t *testing.T) {
t.Parallel()
app := fiber.New()
app.Use(New())
app.Get("/", func(ctx *fiber.Ctx) error {
localize, err := Localize(ctx, "welcome?")
utils.AssertEqual(t, "", localize)
return fiber.NewError(500, err.Error())
})
app.Get("/:name", func(ctx *fiber.Ctx) error {
name := ctx.Params("name")
localize, err := Localize(ctx, &i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": name,
},
})
utils.AssertEqual(t, nil, err)
return ctx.SendString(localize)
})
t.Run("test localize", func(t *testing.T) {
got, err := makeRequest(language.Chinese, "", app)
utils.AssertEqual(t, 500, got.StatusCode)
utils.AssertEqual(t, nil, err)
body, _ := io.ReadAll(got.Body)
utils.AssertEqual(t, `i18n.Localize error: message "welcome?" not found in language "zh"`, string(body))
got, err = makeRequest(language.English, "name", app)
utils.AssertEqual(t, 200, got.StatusCode)
utils.AssertEqual(t, nil, err)
body, _ = io.ReadAll(got.Body)
utils.AssertEqual(t, "hello name", string(body))
})
}
func Test_defaultLangHandler(t *testing.T) {
app := fiber.New()
app.Use(New())
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString(defaultLangHandler(nil, language.English.String()))
})
app.Get("/test", func(c *fiber.Ctx) error {
return c.SendString(defaultLangHandler(c, language.English.String()))
})
t.Parallel()
t.Run("test nil ctx", func(t *testing.T) {
var wg sync.WaitGroup
want := 100
wg.Add(want)
for i := 0; i < want; i++ {
go func() {
defer wg.Done()
got, err := makeRequest(language.English, "", app)
utils.AssertEqual(t, nil, err)
body, _ := io.ReadAll(got.Body)
utils.AssertEqual(t, "en", string(body))
}()
}
wg.Wait()
})
t.Run("test query and header", func(t *testing.T) {
got, err := makeRequest(language.Chinese, "test?lang=en", app)
utils.AssertEqual(t, nil, err)
body, _ := io.ReadAll(got.Body)
utils.AssertEqual(t, "en", string(body))
got, err = makeRequest(language.Chinese, "test", app)
utils.AssertEqual(t, nil, err)
body, _ = io.ReadAll(got.Body)
utils.AssertEqual(t, "zh", string(body))
got, err = makeRequest(language.Chinese, "test", app)
utils.AssertEqual(t, nil, err)
body, _ = io.ReadAll(got.Body)
utils.AssertEqual(t, "zh", string(body))
})
}