appresult.Localize, the first admin middleware, rewrites JSON responses so
every {en, ru, tm} object becomes the plain string for ?lang= (tm, ru, en;
default tm), falling back tm -> ru -> en when that translation is empty.
?lang=all keeps the full objects for edit forms; other values are 400.
Key order and number precision are preserved; non-JSON responses and
/swagger are untouched.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
3.4 KiB
Go
107 lines
3.4 KiB
Go
package appresult
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func serve(t *testing.T, query string, handler gin.HandlerFunc) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
router := gin.New()
|
|
router.Use(Localize())
|
|
router.GET("/x", handler)
|
|
|
|
rec := httptest.NewRecorder()
|
|
router.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/x"+query, nil))
|
|
return rec
|
|
}
|
|
|
|
func jsonHandler(body string) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Data(http.StatusOK, "application/json; charset=utf-8", []byte(body))
|
|
}
|
|
}
|
|
|
|
const branchBody = `{"id":1,"name":{"en":"Branch","ru":"Филиал","tm":"Şahamça"}}`
|
|
|
|
func TestLocalizeLanguages(t *testing.T) {
|
|
tests := []struct {
|
|
query string
|
|
want string
|
|
}{
|
|
{"", `{"id":1,"name":"Şahamça"}`},
|
|
{"?lang=tm", `{"id":1,"name":"Şahamça"}`},
|
|
{"?lang=ru", `{"id":1,"name":"Филиал"}`},
|
|
{"?lang=EN", `{"id":1,"name":"Branch"}`},
|
|
{"?lang=all", branchBody},
|
|
}
|
|
for _, tt := range tests {
|
|
rec := serve(t, tt.query, jsonHandler(branchBody))
|
|
if rec.Code != http.StatusOK || rec.Body.String() != tt.want {
|
|
t.Errorf("%q: got %d %s, want %s", tt.query, rec.Code, rec.Body.String(), tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLocalizeRejectsUnknownLang(t *testing.T) {
|
|
called := false
|
|
rec := serve(t, "?lang=xx", func(c *gin.Context) { called = true })
|
|
if rec.Code != http.StatusBadRequest || called {
|
|
t.Fatalf("got %d (handler called: %v), want 400 without calling the handler", rec.Code, called)
|
|
}
|
|
}
|
|
|
|
func TestLocalizeFallback(t *testing.T) {
|
|
tests := []struct {
|
|
body string
|
|
want string
|
|
}{
|
|
{`{"tm":"","ru":"Филиал","en":"Branch"}`, `"Филиал"`},
|
|
{`{"tm":null,"ru":"","en":"Branch"}`, `"Branch"`},
|
|
{`{"tm":"","ru":"","en":""}`, `""`},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := serve(t, "", jsonHandler(tt.body)).Body.String(); got != tt.want {
|
|
t.Errorf("%s: got %s, want %s", tt.body, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLocalizeKeepsStructure(t *testing.T) {
|
|
body := `{"z":9007199254740993,"a":[{"b":{"en":"E","ru":"R","tm":"T"},"f":1.10}],` +
|
|
`"extra":{"en":"E","ru":"R","tm":"T","id":2},"partial":{"en":"E","ru":"R"},` +
|
|
`"nested":{"en":{"x":1},"ru":"R","tm":"T"},"ok":true,"none":null,"html":"<a&b>"}`
|
|
want := `{"z":9007199254740993,"a":[{"b":"T","f":1.10}],` +
|
|
`"extra":{"en":"E","ru":"R","tm":"T","id":2},"partial":{"en":"E","ru":"R"},` +
|
|
`"nested":{"en":{"x":1},"ru":"R","tm":"T"},"ok":true,"none":null,"html":` + "\"\\u003ca\\u0026b\\u003e\"}"
|
|
if got := serve(t, "", jsonHandler(body)).Body.String(); got != want {
|
|
t.Fatalf("got %s\nwant %s", got, want)
|
|
}
|
|
}
|
|
|
|
func TestLocalizeMatchesGinJSON(t *testing.T) {
|
|
rec := serve(t, "?lang=ru", func(c *gin.Context) {
|
|
c.JSON(http.StatusCreated, SuccessWithData(map[string]interface{}{
|
|
"name": map[string]string{"en": "Branch", "ru": "Филиал", "tm": "Şahamça"},
|
|
}))
|
|
})
|
|
want := `{"status":true,"message":"Request completed successfully.","code":"SS-10000","data":{"name":"Филиал"}}`
|
|
if rec.Code != http.StatusCreated || rec.Body.String() != want {
|
|
t.Fatalf("got %d %s, want 201 %s", rec.Code, rec.Body.String(), want)
|
|
}
|
|
}
|
|
|
|
func TestLocalizeSkipsNonJSON(t *testing.T) {
|
|
body := `{"en":"E","ru":"R","tm":"T"}`
|
|
rec := serve(t, "", func(c *gin.Context) {
|
|
c.Data(http.StatusOK, "text/plain", []byte(body))
|
|
})
|
|
if rec.Body.String() != body {
|
|
t.Fatalf("got %s, want body untouched", rec.Body.String())
|
|
}
|
|
}
|