aboutsummaryrefslogtreecommitdiff
path: root/misc/i18n/lingo_test.go
blob: ca1f559c7eb96217b490925fe7754aba02895925 (plain)
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
package i18n

import (
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"
)

func TestLingo(t *testing.T) {
	l := New("de_DE", "translations")
	t1 := l.TranslationsForLocale("en_US")
	r1 := t1.Value("main.subtitle")
	r1Exp := "Knives that put cut in cutlery."
	if r1 != r1Exp {
		t.Errorf("Expected \""+r1Exp+"\", got %s", r1)
		t.Fail()
	}
	r2 := t1.Value("home.title")
	r2Exp := "Welcome to CutleryPlus!"
	if r2 != r2Exp {
		t.Errorf("Expected \""+r2Exp+"\", got %s", r2)
		t.Fail()
	}
	r3 := t1.Value("menu.products.self")
	r3Exp := "Products"
	if r3 != r3Exp {
		t.Errorf("Expected \""+r3Exp+"\", got %s", r3)
		t.Fail()
	}
	r4 := t1.Value("menu.non.existant")
	r4Exp := "non.existant"
	if r4 != r4Exp {
		t.Errorf("Expected \""+r4Exp+"\", got %s", r4)
		t.Fail()
	}
	r5 := t1.Value("error.404", "idnex.html")
	r5Exp := "Page idnex.html not found!"
	if r5 != r5Exp {
		t.Errorf("Expected \""+r5Exp+"\", got \"%s\"", r5)
		t.Fail()
	}
}

func TestLingoHttp(t *testing.T) {
	l := New("en_US", "translations")
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		expected := r.Header.Get("Expected-Results")
		t1 := l.TranslationsForRequest(r)
		r1 := t1.Value("error.500")
		if r1 != expected {
			t.Errorf("Expected \""+expected+"\", got %s", r1)
			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)
}