forked from jdkeke142/lingo-toml
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lingo_test.go
102 lines (93 loc) · 2.35 KB
/
lingo_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
package lingo
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestFallback(t *testing.T) {
l, err := New("en_US", "translations", nil)
if err != nil {
t.Error(err)
}
t1 := l.TranslationsForLocale("de_DE")
actual := t1.Value("only.english")
expected := "This is only in the English file"
if actual != expected {
t.Errorf("Expected %v, got %v", expected, actual)
t.Fail()
}
}
func TestLingo(t *testing.T) {
tests := []struct {
key string
expected string
}{
{"main.subtitle", "Knives that put cut in cutlery."},
{"home.title", "Welcome to CutleryPlus!"},
{"menu.products.self", "Products"},
{"menu.non.existant", "menu.non.existant"},
}
l, err := New("de_DE", "translations", nil)
if err != nil {
t.Error(err)
}
t1 := l.TranslationsForLocale("en_US")
for _, tc := range tests {
actual := t1.Value(tc.key)
if actual != tc.expected {
t.Errorf("Expected %v, got %v", tc.expected, actual)
t.Fail()
}
}
actual := t1.Value("error.404", "idnex.html")
expected := "Page idnex.html not found!"
if actual != expected {
t.Errorf("Expected %v, got %v", expected, actual)
t.Fail()
}
}
func TestLingoHttp(t *testing.T) {
l, err := New("en_US", "translations", nil)
if err != nil {
t.Error(err)
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expected := r.Header.Get("Expected-Results")
t1 := l.TranslationsForRequest(r)
actual := t1.Value("error.500")
if actual != expected {
t.Errorf("Expected %v, got %v", expected, actual)
t.Fail()
}
}))
defer srv.Close()
url, _ := url.Parse(srv.URL)
req1 := &http.Request{
Method: "GET",
Header: map[string][]string{
"Accept-Language": {"sr, en-gb;q=0.8, en;q=0.7"},
"Expected-Results": {"Greska sa nase strane, pokusajte ponovo."},
},
URL: url,
}
req2 := &http.Request{
Method: "GET",
Header: map[string][]string{
"Accept-Language": {"en-US, en-gb;q=0.8, en;q=0.7"},
"Expected-Results": {"Something is wrong on our side, please try again."},
},
URL: url,
}
req3 := &http.Request{
Method: "GET",
Header: map[string][]string{
"Accept-Language": {"de-at, en-gb;q=0.8, en;q=0.7"},
"Expected-Results": {"Stimmt etwas nicht auf unserer Seite ist, versuchen Sie es erneut."},
},
URL: url,
}
http.DefaultClient.Do(req1)
http.DefaultClient.Do(req2)
http.DefaultClient.Do(req3)
}