|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package middleware |
| 21 | + |
| 22 | +import ( |
| 23 | + "net/http" |
| 24 | + "net/http/httptest" |
| 25 | + "strings" |
| 26 | + "testing" |
| 27 | + |
| 28 | + "github.com/apache/answer/internal/base/constant" |
| 29 | + "github.com/apache/answer/internal/base/translator" |
| 30 | + "github.com/gin-gonic/gin" |
| 31 | + "github.com/segmentfault/pacman/i18n" |
| 32 | + "github.com/stretchr/testify/assert" |
| 33 | +) |
| 34 | + |
| 35 | +func resolveAcceptLanguage(t *testing.T, header string) i18n.Language { |
| 36 | + t.Helper() |
| 37 | + gin.SetMode(gin.TestMode) |
| 38 | + |
| 39 | + var resolved i18n.Language |
| 40 | + router := gin.New() |
| 41 | + router.Use(ExtractAndSetAcceptLanguage) |
| 42 | + router.GET("/", func(ctx *gin.Context) { |
| 43 | + value, _ := ctx.Get(constant.AcceptLanguageFlag) |
| 44 | + resolved, _ = value.(i18n.Language) |
| 45 | + ctx.Status(http.StatusNoContent) |
| 46 | + }) |
| 47 | + |
| 48 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 49 | + if header != "" { |
| 50 | + req.Header.Set(constant.AcceptLanguageFlag, header) |
| 51 | + } |
| 52 | + router.ServeHTTP(httptest.NewRecorder(), req) |
| 53 | + return resolved |
| 54 | +} |
| 55 | + |
| 56 | +func TestExtractAndSetAcceptLanguage(t *testing.T) { |
| 57 | + original := translator.LanguageOptions |
| 58 | + translator.SetLanguageOptions([]*translator.LangOption{ |
| 59 | + {Label: "Default", Value: translator.DefaultLangOption}, |
| 60 | + {Label: "English", Value: "en_US"}, |
| 61 | + {Label: "Español", Value: "es_ES"}, |
| 62 | + {Label: "Português do Brasil", Value: "pt_BR"}, |
| 63 | + {Label: "Português", Value: "pt_PT"}, |
| 64 | + {Label: "简体中文", Value: "zh_CN"}, |
| 65 | + {Label: "繁體中文", Value: "zh_TW"}, |
| 66 | + }) |
| 67 | + t.Cleanup(func() { translator.SetLanguageOptions(original) }) |
| 68 | + |
| 69 | + tests := []struct { |
| 70 | + name string |
| 71 | + header string |
| 72 | + want i18n.Language |
| 73 | + }{ |
| 74 | + {"exact configured locale", "es-ES", "es_ES"}, |
| 75 | + {"exact locale with quality", "pt-BR,pt;q=0.8,en;q=0.5", "pt_BR"}, |
| 76 | + {"regional Spanish falls back to configured Spanish", "es-MX", "es_ES"}, |
| 77 | + {"Latin American Spanish", "es-419", "es_ES"}, |
| 78 | + {"regional Portuguese prefers European Portuguese", "pt-AO", "pt_PT"}, |
| 79 | + {"Hong Kong Chinese resolves to Traditional", "zh-HK", "zh_TW"}, |
| 80 | + {"Singapore Chinese resolves to Simplified", "zh-SG", "zh_CN"}, |
| 81 | + {"regional English", "en-GB", "en_US"}, |
| 82 | + {"unsupported language uses English", "tlh", "en_US"}, |
| 83 | + {"empty header uses English", "", "en_US"}, |
| 84 | + {"malformed header uses English", "!!!", "en_US"}, |
| 85 | + {"oversized header uses English", strings.Repeat("es-MX,", 100), "en_US"}, |
| 86 | + } |
| 87 | + for _, tt := range tests { |
| 88 | + t.Run(tt.name, func(t *testing.T) { |
| 89 | + assert.Equal(t, tt.want, resolveAcceptLanguage(t, tt.header)) |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments