diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-09-14 13:56:49 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-09-14 13:56:49 +0200 |
| commit | da83f54606c9c1d2786d87074ee17ed972d2e1b2 (patch) | |
| tree | 0be57934f9f467c87576abb0b457fb54b2d25d52 /internal/util | |
| parent | fd34e72da8c604517050ada7e883ba982829d985 (diff) | |
Refactor: Return without json
Diffstat (limited to 'internal/util')
| -rw-r--r-- | internal/util/util.go | 49 | ||||
| -rw-r--r-- | internal/util/util_test.go | 52 |
2 files changed, 100 insertions, 1 deletions
diff --git a/internal/util/util.go b/internal/util/util.go index b104476..e652779 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -83,3 +83,52 @@ func ReplaceWAYF(authTemplate string, authURL string, orgID string) string { authTemplate = strings.Replace(authTemplate, "@ORG_ID@", WAYFEncode(orgID), 1) return authTemplate } + +// https://github.com/eduvpn/documentation/blob/dc4d53c47dd7a69e95d6650eec408e16eaa814a2/SERVER_DISCOVERY.md#language-matching +func GetLanguageMatched(languageMap map[string]string, languageTag string) string { + // If no or empty map is given, return the empty string + if languageMap == nil || len(languageMap) == 0 { + return "" + } + // Try to find the exact match + if val, ok := languageMap[languageTag]; ok { + return val + } + // Try to find a key that starts with the OS language setting + for k := range languageMap { + if strings.HasPrefix(k, languageTag) { + return languageMap[k] + } + } + // Try to find a key that starts with the first part of the OS language (e.g. de-) + splitted := strings.Split(languageTag, "-") + // We have a "-" + if len(splitted) > 1 { + for k := range languageMap { + if strings.HasPrefix(k, splitted[0]+"-") { + return languageMap[k] + } + } + } + // search for just the language (e.g. de) + for k := range languageMap { + if k == splitted[0] { + return languageMap[k] + } + } + + // Pick one that is deemed best, e.g. en-US or en, but note that not all languages are always available! + // We force an entry that is english exactly or with an english prefix + for k := range languageMap { + if k == "en" || strings.HasPrefix(k, "en-") { + return languageMap[k] + } + } + + // Otherwise just return one + for k := range languageMap { + return languageMap[k] + } + + return "" +} diff --git a/internal/util/util_test.go b/internal/util/util_test.go index 9eda14e..31d01e7 100644 --- a/internal/util/util_test.go +++ b/internal/util/util_test.go @@ -80,7 +80,11 @@ func Test_WAYFEncode(t *testing.T) { func Test_ReplaceWAYF(t *testing.T) { // We expect url encoding but the spaces to be correctly replace with a + instead of a %20 // And we expect that the return to and org_id are correctly replaced - replaced := ReplaceWAYF("@RETURN_TO@@ORG_ID@", "127.0.0.1:8000/&%$3#kM_- ", "idp-test.nl.org/") + replaced := ReplaceWAYF( + "@RETURN_TO@@ORG_ID@", + "127.0.0.1:8000/&%$3#kM_- ", + "idp-test.nl.org/", + ) wantReplaced := "127.0.0.1%3A8000%2F%26%25%243%23kM_-++++++++++++idp-test.nl.org%2F" if replaced != wantReplaced { t.Fatalf("Got: %s, want: %s", replaced, wantReplaced) @@ -114,3 +118,49 @@ func Test_ReplaceWAYF(t *testing.T) { t.Fatalf("Got: %s, want: %s", replaced, wantReplaced) } } + +func Test_GetLanguageMatched(t *testing.T) { + // func GetLanguageMatched(languageMap map[string]string, languageTag string) string { + + // exact match + returned := GetLanguageMatched(map[string]string{"en": "test", "de": "test2"}, "en") + if returned != "test" { + t.Fatalf("Got: %s, want: %s", returned, "test") + } + + // starts with language tag + returned = GetLanguageMatched(map[string]string{"en-US-test": "test", "de": "test2"}, "en-US") + if returned != "test" { + t.Fatalf("Got: %s, want: %s", returned, "test") + } + + // starts with en- + returned = GetLanguageMatched(map[string]string{"en-UK": "test", "en": "test2"}, "en-US") + if returned != "test" { + t.Fatalf("Got: %s, want: %s", returned, "test") + } + + // exact match for en + returned = GetLanguageMatched(map[string]string{"de": "test", "en": "test2"}, "en-US") + if returned != "test2" { + t.Fatalf("Got: %s, want: %s", returned, "test2") + } + + // We default to english + returned = GetLanguageMatched(map[string]string{"es": "test", "en": "test2"}, "nl-NL") + if returned != "test2" { + t.Fatalf("Got: %s, want: %s", returned, "test2") + } + + // We default to english with a - as well + returned = GetLanguageMatched(map[string]string{"est": "test", "en-": "test2"}, "en-US") + if returned != "test2" { + t.Fatalf("Got: %s, want: %s", returned, "test2") + } + + // None found just return one + returned = GetLanguageMatched(map[string]string{"es": "test"}, "en-US") + if returned != "test" { + t.Fatalf("Got: %s, want: %s", returned, "test") + } +} |
