1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
13 "github.com/revel/config"
14 "github.com/revel/revel/logger"
18 testDataPath string = "testdata/i18n"
19 testConfigPath string = "testdata/i18n/config"
20 testConfigName string = "test_app.conf"
23 func TestI18nLoadMessages(t *testing.T) {
24 loadMessages(testDataPath)
26 // Assert that we have the expected number of languages
27 if len(MessageLanguages()) != 2 {
28 t.Fatalf("Expected messages to contain no more or less than 2 languages, instead there are %d languages", len(MessageLanguages()))
32 func TestI18nMessage(t *testing.T) {
33 loadMessages(testDataPath)
36 // Assert that we can get a message and we get the expected return value
37 if message := Message("nl", "greeting"); message != "Hallo" {
38 t.Errorf("Message 'greeting' for locale 'nl' (%s) does not have the expected value", message)
40 if message := Message("en", "greeting"); message != "Hello" {
41 t.Errorf("Message 'greeting' for locale 'en' (%s) does not have the expected value", message)
43 if message := Message("en", "folded"); message != "Greeting is 'Hello'" {
44 t.Error("Unexpected unfolded message: ", message)
46 if message := Message("en", "arguments.string", "Vincent Hanna"); message != "My name is Vincent Hanna" {
47 t.Errorf("Message 'arguments.string' for locale 'en' (%s) does not have the expected value", message)
49 if message := Message("en", "arguments.hex", 1234567, 1234567); message != "The number 1234567 in hexadecimal notation would be 12d687" {
50 t.Errorf("Message 'arguments.hex' for locale 'en' (%s) does not have the expected value", message)
52 if message := Message("en", "folded.arguments", 12345); message != "Rob is 12345 years old" {
53 t.Errorf("Message 'folded.arguments' for locale 'en' (%s) does not have the expected value", message)
55 if message := Message("en-AU", "greeting"); message != "G'day" {
56 t.Errorf("Message 'greeting' for locale 'en-AU' (%s) does not have the expected value", message)
58 if message := Message("en-AU", "only_exists_in_default"); message != "Default" {
59 t.Errorf("Message 'only_exists_in_default' for locale 'en-AU' (%s) does not have the expected value", message)
62 // Assert that message merging works
63 if message := Message("en", "greeting2"); message != "Yo!" {
64 t.Errorf("Message 'greeting2' for locale 'en' (%s) does not have the expected value", message)
67 // Assert that we get the expected return value for a locale that doesn't exist
68 if message := Message("unknown locale", "message"); message != "??? message ???" {
69 t.Error("Locale 'unknown locale' is not supposed to exist")
71 // Assert that we get the expected return value for a message that doesn't exist
72 if message := Message("nl", "unknown message"); message != "??? unknown message ???" {
73 t.Error("Message 'unknown message' is not supposed to exist")
76 if message := Message("en", "arguments.string", "<img src=a onerror=alert(1) />"); message != "My name is <img src=a onerror=alert(1) />" {
77 t.Error("XSS protection for messages is broken:", message)
79 // Avoid escaping HTML
80 if message := Message("en", "arguments.string", template.HTML("<img src=a onerror=alert(1) />")); message != "My name is <img src=a onerror=alert(1) />" {
81 t.Error("Passing safe HTML to message is broken:", message)
85 func TestI18nMessageWithDefaultLocale(t *testing.T) {
86 loadMessages(testDataPath)
89 if message := Message("doesn't exist", "greeting"); message != "Hello" {
90 t.Errorf("Expected message '%s' for unknown locale to be default '%s' but was '%s'", "greeting", "Hello", message)
92 if message := Message("doesn't exist", "unknown message"); message != "??? unknown message ???" {
93 t.Error("Message 'unknown message' is not supposed to exist in the default language")
97 func TestHasLocaleCookie(t *testing.T) {
100 if found, value := hasLocaleCookie(buildRequestWithCookie("APP_LANG", "en").Request); !found {
101 t.Errorf("Expected %s cookie with value '%s' but found nothing or unexpected value '%s'", "APP_LANG", "en", value)
103 if found, value := hasLocaleCookie(buildRequestWithCookie("APP_LANG", "en-US").Request); !found {
104 t.Errorf("Expected %s cookie with value '%s' but found nothing or unexpected value '%s'", "APP_LANG", "en-US", value)
106 if found, _ := hasLocaleCookie(buildRequestWithCookie("DOESNT_EXIST", "en-US").Request); found {
107 t.Errorf("Expected %s cookie to not exist, but apparently it does", "DOESNT_EXIST")
111 func TestHasLocaleCookieWithInvalidConfig(t *testing.T) {
112 loadTestI18nConfigWithoutLanguageCookieOption(t)
113 if found, _ := hasLocaleCookie(buildRequestWithCookie("APP_LANG", "en-US").Request); found {
114 t.Errorf("Expected %s cookie to not exist because the configured name is missing", "APP_LANG")
116 if found, _ := hasLocaleCookie(buildRequestWithCookie("REVEL_LANG", "en-US").Request); !found {
117 t.Errorf("Expected %s cookie to exist", "REVEL_LANG")
121 func TestHasAcceptLanguageHeader(t *testing.T) {
122 if found, value := hasAcceptLanguageHeader(buildRequestWithAcceptLanguages("en-US").Request); !found && value != "en-US" {
123 t.Errorf("Expected to find Accept-Language header with value '%s', found '%s' instead", "en-US", value)
125 if found, value := hasAcceptLanguageHeader(buildRequestWithAcceptLanguages("en-GB", "en-US", "nl").Request); !found && value != "en-GB" {
126 t.Errorf("Expected to find Accept-Language header with value '%s', found '%s' instead", "en-GB", value)
130 func TestBeforeRequest(t *testing.T) {
131 loadTestI18nConfig(t)
133 c := buildEmptyRequest()
134 if I18nFilter(c, NilChain); c.Request.Locale != "" {
135 t.Errorf("Expected to find current language '%s' in controller, found '%s' instead", "", c.Request.Locale)
138 c = buildRequestWithCookie("APP_LANG", "en-US")
139 if I18nFilter(c, NilChain); c.Request.Locale != "en-US" {
140 t.Errorf("Expected to find current language '%s' in controller, found '%s' instead", "en-US", c.Request.Locale)
143 c = buildRequestWithAcceptLanguages("en-GB", "en-US")
144 if I18nFilter(c, NilChain); c.Request.Locale != "en-GB" {
145 t.Errorf("Expected to find current language '%s' in controller, found '%s' instead", "en-GB", c.Request.Locale)
149 func TestI18nMessageUnknownValueFormat(t *testing.T) {
150 loadMessages(testDataPath)
151 loadTestI18nConfigWithUnknowFormatOption(t)
153 // Assert that we can get a message and we get the expected return value
154 if message := Message("en", "greeting"); message != "Hello" {
155 t.Errorf("Message 'greeting' for locale 'en' (%s) does not have the expected value", message)
158 // Assert that we get the expected return value with original format
159 if message := Message("unknown locale", "message"); message != "*** message ***" {
160 t.Error("Locale 'unknown locale' is not supposed to exist")
162 if message := Message("nl", "unknown message"); message != "*** unknown message ***" {
163 t.Error("Message 'unknown message' is not supposed to exist")
167 func BenchmarkI18nLoadMessages(b *testing.B) {
168 excludeFromTimer(b, func() {
169 RevelLog.SetHandler(logger.FuncHandler(func(r *logger.Record) error {
174 for i := 0; i < b.N; i++ {
175 loadMessages(testDataPath)
179 func BenchmarkI18nMessage(b *testing.B) {
180 for i := 0; i < b.N; i++ {
181 Message("nl", "greeting")
185 func BenchmarkI18nMessageWithArguments(b *testing.B) {
186 excludeFromTimer(b, func() {
187 RevelLog.SetHandler(logger.FuncHandler(func(r *logger.Record) error {
193 for i := 0; i < b.N; i++ {
194 Message("en", "arguments.string", "Vincent Hanna")
198 func BenchmarkI18nMessageWithFoldingAndArguments(b *testing.B) {
199 excludeFromTimer(b, func() {
200 RevelLog.SetHandler(logger.FuncHandler(func(r *logger.Record) error {
206 for i := 0; i < b.N; i++ {
207 Message("en", "folded.arguments", 12345)
211 // Exclude whatever operations the given function performs from the benchmark timer.
212 func excludeFromTimer(b *testing.B, f func()) {
218 func loadTestI18nConfig(t *testing.T) {
219 ConfPaths = append(ConfPaths, testConfigPath)
220 testConfig, err := config.LoadContext(testConfigName, ConfPaths)
222 t.Fatalf("Unable to load test config '%s': %s", testConfigName, err.Error())
225 CookiePrefix = Config.StringDefault("cookie.prefix", "REVEL")
228 func loadTestI18nConfigWithoutLanguageCookieOption(t *testing.T) {
229 loadTestI18nConfig(t)
230 Config.Raw().RemoveOption("DEFAULT", "i18n.cookie")
233 func loadTestI18nConfigWithUnknowFormatOption(t *testing.T) {
234 loadTestI18nConfig(t)
235 Config.Raw().AddOption("DEFAULT", "i18n.unknown_format", "*** %s ***")
238 func buildRequestWithCookie(name, value string) *Controller {
239 httpRequest, _ := http.NewRequest("GET", "/", nil)
240 controller := NewTestController(nil, httpRequest)
242 httpRequest.AddCookie(&http.Cookie{
258 func buildRequestWithAcceptLanguages(acceptLanguages ...string) *Controller {
259 httpRequest, _ := http.NewRequest("GET", "/", nil)
260 controller := NewTestController(nil, httpRequest)
262 request := controller.Request
263 for _, acceptLanguage := range acceptLanguages {
264 request.AcceptLanguages = append(request.AcceptLanguages, AcceptLanguage{acceptLanguage, 1})
269 func buildEmptyRequest() *Controller {
270 httpRequest, _ := http.NewRequest("GET", "/", nil)
271 controller := NewTestController(nil, httpRequest)