-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf2d0c1
commit e62cd20
Showing
6 changed files
with
233 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/reearth/reearth/server/internal/adapter" | ||
"github.com/reearth/reearth/server/pkg/apperror" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/vektah/gqlparser/v2/ast" | ||
) | ||
|
||
func TestCustomErrorPresenter(t *testing.T) { | ||
// モック用のコンテキストとロケール | ||
ctx := context.Background() | ||
ctx = adapter.AttachLang(ctx, "en") | ||
|
||
// アプリケーション固有のエラー | ||
appErr := &apperror.AppError{ | ||
LocalesError: map[string]*apperror.LocalesError{ | ||
"en": { | ||
Code: "test_code", | ||
Message: "Test message", | ||
Description: "Test description", | ||
}, | ||
}, | ||
SystemError: errors.New("system error"), | ||
} | ||
|
||
t.Run("AppError with English language", func(t *testing.T) { | ||
graphqlErr := customErrorPresenter(ctx, appErr, false) | ||
|
||
// アサーション: GraphQL エラーの内容を検証 | ||
assert.NotNil(t, graphqlErr) | ||
assert.Equal(t, "Test message", graphqlErr.Message) | ||
assert.Equal(t, "test_code", graphqlErr.Extensions["code"]) | ||
assert.Equal(t, "Test description", graphqlErr.Extensions["description"]) | ||
assert.Equal(t, "system error", graphqlErr.Extensions["system_error"]) | ||
}) | ||
|
||
t.Run("Fallback to default GraphQL error", func(t *testing.T) { | ||
defaultErr := errors.New("default error") | ||
graphqlErr := customErrorPresenter(ctx, defaultErr, false) | ||
|
||
// アサーション: デフォルトのエラーハンドリングを検証 | ||
assert.NotNil(t, graphqlErr) | ||
assert.Equal(t, "default error", graphqlErr.Message) | ||
assert.Equal(t, "default error", graphqlErr.Extensions["system_error"]) | ||
}) | ||
|
||
t.Run("Development mode with debugging information(Path)", func(t *testing.T) { | ||
graphqlErr := customErrorPresenter(ctx, appErr, true) | ||
|
||
assert.NotNil(t, graphqlErr) | ||
assert.Equal(t, ast.Path{}, graphqlErr.Path) | ||
assert.Equal(t, "Test message", graphqlErr.Message) | ||
assert.Equal(t, "test_code", graphqlErr.Extensions["code"]) | ||
assert.Equal(t, "Test description", graphqlErr.Extensions["description"]) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package app | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/reearth/reearth/server/internal/adapter" | ||
) | ||
|
||
// LanguageExtractor extracts the appropriate language from the request. | ||
func LanguageExtractor(req *http.Request) string { | ||
// Extract browser language from header | ||
lang := req.Header.Get("lang") | ||
|
||
// Extract user language from the adapter | ||
// if user language is not "und", use user language | ||
// if user language is "und", use browser language | ||
u := adapter.User(req.Context()) | ||
if u != nil && u.Lang().String() != "und" { | ||
lang = u.Lang().String() | ||
} | ||
|
||
return lang | ||
} | ||
|
||
// AttachLanguageMiddleware attaches the detected language to the request context. | ||
func AttachLanguageMiddleware(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
lang := LanguageExtractor(c.Request()) | ||
ctx := adapter.AttachLang(c.Request().Context(), lang) | ||
c.SetRequest(c.Request().WithContext(ctx)) | ||
return next(c) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package app_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/reearth/reearth/server/internal/adapter" | ||
"github.com/reearth/reearth/server/internal/app" | ||
"github.com/reearth/reearthx/account/accountdomain/user" | ||
"golang.org/x/text/language" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLanguageExtractor(t *testing.T) { | ||
// Mock user with a language | ||
tests := []struct { | ||
name string | ||
headerLang string | ||
userLang language.Tag | ||
expected string | ||
}{ | ||
{ | ||
name: "User language overrides browser language", | ||
headerLang: "fr", | ||
userLang: language.English, | ||
expected: "en", | ||
}, | ||
{ | ||
name: "No user language, use browser language", | ||
headerLang: "fr", | ||
userLang: language.Und, | ||
expected: "fr", | ||
}, | ||
{ | ||
name: "No browser language or user language is und, fallback to default", | ||
headerLang: "", | ||
userLang: language.Und, | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Mock request and context | ||
req, _ := http.NewRequest("GET", "/", nil) | ||
req.Header.Set("lang", tt.headerLang) | ||
|
||
u := &user.User{} | ||
u.UpdateLang(tt.userLang) | ||
ctx := adapter.AttachUser(context.Background(), u) | ||
req = req.WithContext(ctx) | ||
|
||
lang := app.LanguageExtractor(req) | ||
assert.Equal(t, tt.expected, lang) | ||
}) | ||
} | ||
} | ||
|
||
func TestAttachLanguageMiddleware(t *testing.T) { | ||
e := echo.New() | ||
e.Use(app.AttachLanguageMiddleware) | ||
|
||
e.GET("/", func(c echo.Context) error { | ||
// get lang from context | ||
lang := adapter.Lang(c.Request().Context()) | ||
// include lang in response | ||
return c.String(http.StatusOK, lang) | ||
}) | ||
|
||
t.Run("Middleware attaches correct language", func(t *testing.T) { | ||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
req.Header.Set("lang", "fr") | ||
rec := httptest.NewRecorder() | ||
e.ServeHTTP(rec, req) | ||
assert.Equal(t, http.StatusOK, rec.Code) | ||
// check lang in response | ||
assert.Equal(t, "fr", rec.Body.String()) | ||
}) | ||
} |