forked from gofiber/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembed_test.go
99 lines (92 loc) · 1.98 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
package fiberi18n
import (
"context"
"embed"
"encoding/json"
"io"
"net/http"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
)
//go:embed example/localizeJSON/*
var fs embed.FS
func newEmbedServer() *fiber.App {
app := fiber.New()
app.Use(New(&Config{
Loader: &EmbedLoader{fs},
UnmarshalFunc: json.Unmarshal,
RootPath: "./example/localizeJSON/",
FormatBundleFile: "json",
}))
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 embedApp = newEmbedServer()
func request(lang language.Tag, name string) (*http.Response, error) {
path := "/" + name
req, _ := http.NewRequestWithContext(context.Background(), "GET", path, nil)
req.Header.Add("Accept-Language", lang.String())
req.Method = "GET"
req.RequestURI = path
resp, err := embedApp.Test(req)
return resp, err
}
func TestEmbedLoader_LoadMessage(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: "",
lang: language.Chinese,
},
want: "你好",
},
{
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 := request(tt.args.lang, tt.args.name)
utils.AssertEqual(t, err, nil)
body, err := io.ReadAll(got.Body)
utils.AssertEqual(t, err, nil)
utils.AssertEqual(t, tt.want, string(body))
})
}
}