Skip to content

Commit

Permalink
add context test
Browse files Browse the repository at this point in the history
  • Loading branch information
soneda-yuya committed Jan 12, 2025
1 parent 0824a31 commit 3a2bcdc
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 11 deletions.
8 changes: 4 additions & 4 deletions server/internal/adapter/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type AuthInfo struct {
EmailVerified *bool
}

func AttachLang(ctx context.Context, lang string) context.Context {
func AttachLang(ctx context.Context, lang language.Tag) context.Context {
return context.WithValue(ctx, contextLang, lang)
}

Expand Down Expand Up @@ -74,11 +74,11 @@ func Lang(ctx context.Context, lang *language.Tag) string {
}

if v := ctx.Value(contextLang); v != nil {
if lang, ok := v.(string); ok {
if lang == "" {
if lang, ok := v.(language.Tag); ok {
if lang.IsRoot() {
return defaultLang.String()
}
return lang
return lang.String()
}
}

Expand Down
54 changes: 54 additions & 0 deletions server/internal/adapter/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package adapter

import (
"context"
"testing"

"github.com/reearth/reearthx/log"
"github.com/stretchr/testify/assert"
"golang.org/x/text/language"
)

func TestLang(t *testing.T) {

// Default language for testing
defaultLang := language.English // or set it to whatever your defaultLang is

t.Run("Lang is provided and valid", func(t *testing.T) {
lang := language.Japanese
result := Lang(context.Background(), &lang)
assert.Equal(t, "ja", result)
})

t.Run("Lang is nil, context has valid lang", func(t *testing.T) {
lang := language.French
ctx := context.WithValue(context.Background(), contextLang, lang)
result := Lang(ctx, nil)
log.Infofc(ctx, "result: %s", result)
assert.Equal(t, "fr", result)
})

t.Run("Lang is nil, context lang is empty", func(t *testing.T) {
ctx := context.WithValue(context.Background(), contextLang, language.Make(""))
result := Lang(ctx, nil)
assert.Equal(t, defaultLang.String(), result)
})

t.Run("Lang is nil, context has no lang", func(t *testing.T) {
result := Lang(context.Background(), nil)
assert.Equal(t, defaultLang.String(), result)
})

t.Run("Lang is root, context has no lang", func(t *testing.T) {
rootLang := language.Und
result := Lang(context.Background(), &rootLang)
assert.Equal(t, defaultLang.String(), result)
})

t.Run("Lang is french, context has no lang", func(t *testing.T) {
lang := language.French
result := Lang(context.Background(), &lang)
assert.Equal(t, lang.String(), result)
})

}
3 changes: 2 additions & 1 deletion server/internal/app/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"github.com/reearth/reearth/server/pkg/apperror"
"github.com/stretchr/testify/assert"
"github.com/vektah/gqlparser/v2/ast"
"golang.org/x/text/language"
)

func TestCustomErrorPresenter(t *testing.T) {
ctx := context.Background()
ctx = adapter.AttachLang(ctx, "en")
ctx = adapter.AttachLang(ctx, language.English)

appErr := &apperror.AppError{
LocalesError: map[string]*apperror.LocalesError{
Expand Down
10 changes: 8 additions & 2 deletions server/internal/app/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (

"github.com/labstack/echo/v4"
"github.com/reearth/reearth/server/internal/adapter"
"golang.org/x/text/language"
)

// LanguageExtractor extracts the appropriate language from the request.
func LanguageExtractor(req *http.Request) string {
func LanguageExtractor(req *http.Request) language.Tag {
// Extract browser language from header
lang := req.Header.Get("lang")

Expand All @@ -20,7 +21,12 @@ func LanguageExtractor(req *http.Request) string {
lang = u.Lang().String()
}

return lang
tag, err := language.Parse(lang)
if err != nil {
return language.English
}

return tag
}

// AttachLanguageMiddleware attaches the detected language to the request context.
Expand Down
8 changes: 4 additions & 4 deletions server/internal/app/lang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ func TestLanguageExtractor(t *testing.T) {
name string
headerLang string
userLang language.Tag
expected string
expected language.Tag
}{
{
name: "User language overrides browser language",
headerLang: "fr",
userLang: language.English,
expected: "en",
expected: language.English,
},
{
name: "No user language, use browser language",
headerLang: "fr",
userLang: language.Und,
expected: "fr",
expected: language.French,
},
{
name: "No browser language or user language is und, fallback to default",
headerLang: "",
userLang: language.Und,
expected: "",
expected: language.English,
},
}

Expand Down

0 comments on commit 3a2bcdc

Please sign in to comment.