-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathembed_test.go
138 lines (131 loc) · 3.66 KB
/
embed_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
// MIT License
//
// Copyright (c) 2019 Gin-Gonic
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// This file may have been modified by CloudWeGo authors. All CloudWeGo
// Modifications are Copyright 2022 CloudWeGo Authors.
package i18n
import (
"context"
"embed"
"encoding/json"
"net/http"
"testing"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/config"
"github.com/cloudwego/hertz/pkg/common/ut"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/cloudwego/hertz/pkg/route"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
)
func newEmbedServer(middleware ...app.HandlerFunc) *route.Engine {
router := route.NewEngine(config.NewOptions([]config.Option{}))
router.Use(middleware...)
router.GET("/", func(c context.Context, ctx *app.RequestContext) {
ctx.String(200, MustGetMessage(c, "welcome"))
})
router.GET("/:name", func(c context.Context, ctx *app.RequestContext) {
ctx.String(http.StatusOK, MustGetMessage(c, &i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": ctx.Param("name"),
},
}))
})
return router
}
func request(lang language.Tag, name string) string {
path := "/" + name
w := ut.PerformRequest(
s,
consts.MethodGet, path, nil,
ut.Header{Key: "Accept-Language", Value: lang.String()},
)
response := w.Result()
return string(response.Body())
}
var (
//go:embed example/localizeJSON/*
fs embed.FS
s = newEmbedServer(Localize(WithBundle(&BundleCfg{
DefaultLanguage: language.English,
FormatBundleFile: "json",
AcceptLanguage: []language.Tag{language.English, language.Chinese},
RootPath: "./example/localizeJSON/",
UnmarshalFunc: json.Unmarshal,
// After commenting this line, use defaultLoader
// it will be loaded from the file
Loader: &EmbedLoader{fs},
})))
)
func TestEmbedLoader(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.English,
},
want: "hello",
},
{
name: "hello alex",
args: args{
name: "",
lang: language.Chinese,
},
want: "你好",
},
{
name: "hello alex",
args: args{
name: "alex",
lang: language.English,
},
want: "hello alex",
},
{
name: "hello alex german",
args: args{
name: "alex",
lang: language.Chinese,
},
want: "你好 alex",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := request(tt.args.lang, tt.args.name)
if got != tt.want {
t.Errorf("makeRequest() = %v, want %v", got, tt.want)
}
})
}
}