mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2026-12-01 23:07:51 +00:00
Compare commits
3
Commits
v1.2.3
..
e5d8e78f79
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e5d8e78f79 | ||
|
|
66090baa7e | ||
|
|
6012d72ba0 |
@@ -0,0 +1,151 @@
|
|||||||
|
package translate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"compress/flate"
|
||||||
|
"compress/gzip"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/andybalholm/brotli"
|
||||||
|
"github.com/imroc/req/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// useHermeticClient pre-seeds the process-wide client cache for the empty
|
||||||
|
// proxy key so callOneshot reuses it instead of building the real
|
||||||
|
// ImpersonateChrome client, whose creation also fires a background warmup
|
||||||
|
// GET to www.deepl.com. The client mirrors production where it matters for
|
||||||
|
// this layer: it pins Accept-Encoding so Go's transport does not auto-
|
||||||
|
// decompress, which is exactly why callOneshot decompresses by hand.
|
||||||
|
func useHermeticClient(t *testing.T) {
|
||||||
|
t.Helper()
|
||||||
|
c := req.C().
|
||||||
|
SetTimeout(5*time.Second).
|
||||||
|
SetCommonHeader("Accept-Encoding", "gzip, deflate, br")
|
||||||
|
oneshotClients.Store("", c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCallOneshotRequestShaping(t *testing.T) {
|
||||||
|
useHermeticClient(t)
|
||||||
|
|
||||||
|
var gotHeader http.Header
|
||||||
|
var gotBody []byte
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
gotHeader = r.Header.Clone()
|
||||||
|
gotBody, _ = io.ReadAll(r.Body)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(`{"translations":[{"text":"hallo","detected_source_language":"EN"}]}`))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
body := []byte(`{"text":["hello"],"target_lang":"de"}`)
|
||||||
|
|
||||||
|
t.Run("anonymous sends Authorization None and extension headers", func(t *testing.T) {
|
||||||
|
res, status, err := callOneshot(srv.URL, body, "", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("callOneshot error: %v", err)
|
||||||
|
}
|
||||||
|
if status != http.StatusOK {
|
||||||
|
t.Fatalf("status = %d, want 200", status)
|
||||||
|
}
|
||||||
|
if got := res.Get("translations.0.text").String(); got != "hallo" {
|
||||||
|
t.Errorf("parsed body wrong, translations.0.text = %q", got)
|
||||||
|
}
|
||||||
|
if h := gotHeader.Get("Authorization"); h != "None" {
|
||||||
|
t.Errorf("Authorization = %q, want %q", h, "None")
|
||||||
|
}
|
||||||
|
if h := gotHeader.Get("Content-Type"); h != "application/json" {
|
||||||
|
t.Errorf("Content-Type = %q, want application/json", h)
|
||||||
|
}
|
||||||
|
if h := gotHeader.Get("Origin"); h != "chrome-extension://"+chromeExtensionID {
|
||||||
|
t.Errorf("Origin = %q, want chrome-extension://%s", h, chromeExtensionID)
|
||||||
|
}
|
||||||
|
for k, want := range map[string]string{
|
||||||
|
"Sec-Fetch-Site": "cross-site",
|
||||||
|
"Sec-Fetch-Mode": "cors",
|
||||||
|
"Sec-Fetch-Dest": "empty",
|
||||||
|
} {
|
||||||
|
if h := gotHeader.Get(k); h != want {
|
||||||
|
t.Errorf("%s = %q, want %q", k, h, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !bytes.Equal(gotBody, body) {
|
||||||
|
t.Errorf("forwarded body = %q, want %q", gotBody, body)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("bearer token used when session provided", func(t *testing.T) {
|
||||||
|
if _, _, err := callOneshot(srv.URL, body, "tok123", ""); err != nil {
|
||||||
|
t.Fatalf("callOneshot error: %v", err)
|
||||||
|
}
|
||||||
|
if h := gotHeader.Get("Authorization"); h != "Bearer tok123" {
|
||||||
|
t.Errorf("Authorization = %q, want %q", h, "Bearer tok123")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCallOneshotDecompression covers the three hand-rolled decode paths
|
||||||
|
// (gzip/deflate/br) plus identity. This code exists only because the client
|
||||||
|
// pins Accept-Encoding, and it was previously untested — a decode regression
|
||||||
|
// would surface to users as a blind "Translation failed".
|
||||||
|
func TestCallOneshotDecompression(t *testing.T) {
|
||||||
|
useHermeticClient(t)
|
||||||
|
|
||||||
|
const payload = `{"translations":[{"text":"decoded"}]}`
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
encoding string
|
||||||
|
compress func([]byte) []byte
|
||||||
|
}{
|
||||||
|
{"identity", "", func(b []byte) []byte { return b }},
|
||||||
|
{"gzip", "gzip", func(b []byte) []byte {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
w := gzip.NewWriter(&buf)
|
||||||
|
_, _ = w.Write(b)
|
||||||
|
_ = w.Close()
|
||||||
|
return buf.Bytes()
|
||||||
|
}},
|
||||||
|
{"deflate", "deflate", func(b []byte) []byte {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
w, _ := flate.NewWriter(&buf, flate.DefaultCompression)
|
||||||
|
_, _ = w.Write(b)
|
||||||
|
_ = w.Close()
|
||||||
|
return buf.Bytes()
|
||||||
|
}},
|
||||||
|
{"brotli", "br", func(b []byte) []byte {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
w := brotli.NewWriter(&buf)
|
||||||
|
_, _ = w.Write(b)
|
||||||
|
_ = w.Close()
|
||||||
|
return buf.Bytes()
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if tc.encoding != "" {
|
||||||
|
w.Header().Set("Content-Encoding", tc.encoding)
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write(tc.compress([]byte(payload)))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
res, status, err := callOneshot(srv.URL, []byte(`{}`), "", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("callOneshot(%s) error: %v", tc.name, err)
|
||||||
|
}
|
||||||
|
if status != http.StatusOK {
|
||||||
|
t.Fatalf("status = %d, want 200", status)
|
||||||
|
}
|
||||||
|
if got := res.Get("translations.0.text").String(); got != "decoded" {
|
||||||
|
t.Errorf("decoded text = %q, want %q (encoding %q)", got, "decoded", tc.encoding)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
+57
-24
@@ -42,28 +42,52 @@ import (
|
|||||||
// traffic hard; oneshot lives on a separate pool and accepts the literal
|
// traffic hard; oneshot lives on a separate pool and accepts the literal
|
||||||
// header `Authorization: None` for free requests.
|
// header `Authorization: None` for free requests.
|
||||||
//
|
//
|
||||||
// Request shape below is reverse-engineered from DeepL iOS 26.42
|
// Request shape reverse-engineered from DeepL iOS 26.42 (build 5443737,
|
||||||
// (build 5443737, bundle com.linguee.DeepLMobileTranslator):
|
// bundle com.linguee.DeepLMobileTranslator, IPA Info.plist + ItaClient.framework):
|
||||||
// - Free URL → https://oneshot-free.www.deepl.com/v1/translate
|
//
|
||||||
// - Pro URL → https://oneshot-pro.www.deepl.com/v1/translate
|
// Transport
|
||||||
// (iOS also constructs https://oneshot. + .pro.deepl.com/v1/translate)
|
// ItaClient oneshot uses Ktor Darwin engine
|
||||||
// - Body → OneShotTranslator + ItaClient.AppInformation
|
// (io.ktor.client.engine.darwin.KtorNSURLSessionDelegate) → real
|
||||||
// - Headers → ClientInfos.appHeaders (x-app-*) + Authorization
|
// URLSession TLS. We approximate that ClientHello with utls HelloIOS.
|
||||||
// - usage_type → ItaClient.OneShotUsageType.translate
|
//
|
||||||
|
// Free URL → https://oneshot-free.www.deepl.com/v1/translate
|
||||||
|
// Pro URL → https://oneshot. + <cell> + .pro.deepl.com/v1/translate
|
||||||
|
// (we keep oneshot-pro.www as the free-tier Pro fallback)
|
||||||
|
//
|
||||||
|
// Headers (ClientInfos.appHeaders + LoginNone):
|
||||||
|
// Authorization: None (ItaClient.LoginNone)
|
||||||
|
// x-app-os-version (UIDevice.systemVersion)
|
||||||
|
// x-app-instance-id (stable install UUID)
|
||||||
|
// x-app-session-id (session UUID)
|
||||||
|
// User-Agent (URLSession / CFNetwork product form)
|
||||||
|
// Accept / Accept-Encoding (URLSession defaults)
|
||||||
|
//
|
||||||
|
// Body (ItaClient OneShotTranslationRequestDto + AppInformation):
|
||||||
|
// text[], target_lang, source_lang?, usage_type, app_information
|
||||||
|
// usage_type ∈ {translate, ocr, voiceforconversations}
|
||||||
|
//
|
||||||
|
// DeepL rate-limits / temporarily bans clients whose TLS + UA + app_information
|
||||||
|
// story is inconsistent (e.g. iOS TLS fingerprint + "iOS 27.0" in the UA when
|
||||||
|
// 27 is not a shipping OS). Keep every field on one coherent iOS profile.
|
||||||
const (
|
const (
|
||||||
oneshotFreeEndpoint = "https://oneshot-free.www.deepl.com/v1/translate"
|
oneshotFreeEndpoint = "https://oneshot-free.www.deepl.com/v1/translate"
|
||||||
oneshotProEndpoint = "https://oneshot-pro.www.deepl.com/v1/translate"
|
oneshotProEndpoint = "https://oneshot-pro.www.deepl.com/v1/translate"
|
||||||
|
|
||||||
// Pinned to DeepL iOS IPA (Info.plist CFBundleShortVersionString /
|
// Pinned to DeepL iOS IPA (CFBundleShortVersionString / CFBundleVersion).
|
||||||
// CFBundleVersion). Keep app_information in lockstep with the TLS
|
|
||||||
// fingerprint and User-Agent so the request tells one consistent story.
|
|
||||||
iosAppVersion = "26.42"
|
iosAppVersion = "26.42"
|
||||||
iosAppBuild = "5443737"
|
iosAppBuild = "5443737"
|
||||||
iosBundleID = "com.linguee.DeepLMobileTranslator"
|
|
||||||
// Stable OS version reported in app_information + x-app-os-version.
|
// Reported OS version for app_information.os_version + x-app-os-version.
|
||||||
// HelloIOS_Auto does not pin a specific iOS minor; pin a fixed value
|
// IPA is built against iphoneos26.5 (DTPlatformVersion). Must be a real
|
||||||
// so UA / x-app-os-version / app_information stay consistent.
|
// shipping major — a future value (e.g. 27.0) combined with an iOS TLS
|
||||||
iosOSVersion = "27.0"
|
// fingerprint is rejected with HTTP 429 and can temp-ban the IP.
|
||||||
|
iosOSVersion = "26.0"
|
||||||
|
|
||||||
|
// CFNetwork / Darwin versions that accompany URLSession User-Agents on
|
||||||
|
// the same OS generation as DTPlatformVersion 26.x (build machine
|
||||||
|
// BuildMachineOSBuild 25E246 → Darwin 25).
|
||||||
|
iosCFNetworkVersion = "3826.600.41"
|
||||||
|
iosDarwinVersion = "25.0.0"
|
||||||
|
|
||||||
// oneshot enforces a 1500-character hard cap on the total length of
|
// oneshot enforces a 1500-character hard cap on the total length of
|
||||||
// the `text` array for anonymous traffic (same limit the Chrome
|
// the `text` array for anonymous traffic (same limit the Chrome
|
||||||
@@ -277,16 +301,21 @@ func getOneshotClient(proxyURL string) (*req.Client, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newOneshotClient(proxyURL string) (*req.Client, error) {
|
func newOneshotClient(proxyURL string) (*req.Client, error) {
|
||||||
// iOS TLS ClientHello via utls HelloIOS_Auto. Headers are set per
|
// Match ItaClient's Ktor Darwin / URLSession profile:
|
||||||
// request in callOneshot to match ClientInfos.appHeaders rather than
|
// - TLS ClientHello ≈ real iOS (utls HelloIOS_Auto)
|
||||||
// a browser navigation profile.
|
// - Cookie jar shared like URLSession's HTTPCookieStorage
|
||||||
|
// - Common headers = what URLSession attaches by default
|
||||||
|
// Per-request headers (Authorization, x-app-*) are set in callOneshot.
|
||||||
client := req.C().
|
client := req.C().
|
||||||
SetTLSFingerprintIOS().
|
SetTLSFingerprintIOS().
|
||||||
SetCookieJar(sharedCookieJar()).
|
SetCookieJar(sharedCookieJar()).
|
||||||
SetTimeout(oneshotTimeout).
|
SetTimeout(oneshotTimeout).
|
||||||
SetUserAgent(iosUserAgent()).
|
SetUserAgent(iosUserAgent()).
|
||||||
|
// URLSession default Accept-Encoding for data tasks.
|
||||||
SetCommonHeader("Accept-Encoding", "gzip, deflate, br").
|
SetCommonHeader("Accept-Encoding", "gzip, deflate, br").
|
||||||
SetCommonHeader("Accept", "*/*").
|
SetCommonHeader("Accept", "*/*").
|
||||||
|
// Preferred language list — mirrors a US-locale device; DeepL
|
||||||
|
// does not hard-require a specific value for free oneshot.
|
||||||
SetCommonHeader("Accept-Language", "en-US,en;q=0.9")
|
SetCommonHeader("Accept-Language", "en-US,en;q=0.9")
|
||||||
|
|
||||||
if proxyURL != "" {
|
if proxyURL != "" {
|
||||||
@@ -299,12 +328,14 @@ func newOneshotClient(proxyURL string) (*req.Client, error) {
|
|||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// iosUserAgent approximates the CFNetwork-style UA the DeepL iOS app
|
// iosUserAgent is the URLSession product-style User-Agent the app's Ktor
|
||||||
// advertises via ClientInfos.userAgent.
|
// Darwin stack emits (CFBundleName/CFBundleShortVersionString + CFNetwork
|
||||||
|
// + Darwin). Do NOT invent alternate formats (e.g. embedding the bundle
|
||||||
|
// ID) — mismatched UA + iOS TLS fingerprint is a cheap ban signal.
|
||||||
func iosUserAgent() string {
|
func iosUserAgent() string {
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"DeepL/%s (%s; build:%s; iOS %s)",
|
"DeepL/%s CFNetwork/%s Darwin/%s",
|
||||||
iosAppVersion, iosBundleID, iosAppBuild, iosOSVersion,
|
iosAppVersion, iosCFNetworkVersion, iosDarwinVersion,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -318,6 +349,7 @@ func callOneshot(endpoint string, body []byte, bearerToken, proxyURL string) (gj
|
|||||||
return gjson.Result{}, 0, err
|
return gjson.Result{}, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoginNone → literal "None"; LoginPro/Free → "Bearer <access_token>".
|
||||||
authValue := "None"
|
authValue := "None"
|
||||||
if bearerToken != "" {
|
if bearerToken != "" {
|
||||||
authValue = "Bearer " + bearerToken
|
authValue = "Bearer " + bearerToken
|
||||||
@@ -327,7 +359,8 @@ func callOneshot(endpoint string, body []byte, bearerToken, proxyURL string) (gj
|
|||||||
DisableAutoReadResponse().
|
DisableAutoReadResponse().
|
||||||
SetHeader("Content-Type", "application/json").
|
SetHeader("Content-Type", "application/json").
|
||||||
SetHeader("Authorization", authValue).
|
SetHeader("Authorization", authValue).
|
||||||
// ClientInfos.appHeaders from DeepL iOS (Util/ClientInfos.swift).
|
// ClientInfos.appHeaders (Util/ClientInfos.swift) — only these three
|
||||||
|
// x-app-* keys exist in the iOS binary.
|
||||||
SetHeader("x-app-os-version", iosOSVersion).
|
SetHeader("x-app-os-version", iosOSVersion).
|
||||||
SetHeader("x-app-instance-id", instanceID).
|
SetHeader("x-app-instance-id", instanceID).
|
||||||
SetHeader("x-app-session-id", sessionID).
|
SetHeader("x-app-session-id", sessionID).
|
||||||
|
|||||||
@@ -0,0 +1,206 @@
|
|||||||
|
package translate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveTargetLang(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
in string
|
||||||
|
want string
|
||||||
|
wantErr bool
|
||||||
|
errHas string
|
||||||
|
}{
|
||||||
|
{"lowercase normalizes to canonical", "zh-hans", "zh-Hans", false, ""},
|
||||||
|
{"uppercase ZH maps to Simplified", "ZH", "zh-Hans", false, ""},
|
||||||
|
{"EN convenience alias resolves to en-US", "EN", "en-US", false, ""},
|
||||||
|
{"PT convenience alias resolves to pt-BR", "PT", "pt-BR", false, ""},
|
||||||
|
{"regional EN-GB preserved", "en-gb", "en-GB", false, ""},
|
||||||
|
{"plain de", "de", "de", false, ""},
|
||||||
|
{"empty is required", "", "", true, "required"},
|
||||||
|
{"auto is rejected as a target", "auto", "", true, "cannot be"},
|
||||||
|
{"AUTO rejected case-insensitively", "AUTO", "", true, "cannot be"},
|
||||||
|
{"unsupported code is reported", "xx", "", true, "unsupported target_lang"},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got, err := resolveTargetLang(tc.in)
|
||||||
|
if tc.wantErr {
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("resolveTargetLang(%q) = %q, want error", tc.in, got)
|
||||||
|
}
|
||||||
|
if tc.errHas != "" && !strings.Contains(err.Error(), tc.errHas) {
|
||||||
|
t.Errorf("error %q does not contain %q", err.Error(), tc.errHas)
|
||||||
|
}
|
||||||
|
if got != "" {
|
||||||
|
t.Errorf("on error want empty result, got %q", got)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("resolveTargetLang(%q) unexpected error: %v", tc.in, err)
|
||||||
|
}
|
||||||
|
if got != tc.want {
|
||||||
|
t.Errorf("resolveTargetLang(%q) = %q, want %q", tc.in, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveSourceLang(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
in string
|
||||||
|
want string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"empty means autodetect", "", "", false},
|
||||||
|
{"auto means autodetect", "auto", "", false},
|
||||||
|
{"AUTO case-insensitive", "AUTO", "", false},
|
||||||
|
// EN/PT are first-class *source* codes and resolve to the generic
|
||||||
|
// form, unlike the target map which forces a regional default
|
||||||
|
// (EN -> en-US there). This asymmetry is easy to regress.
|
||||||
|
{"EN as source resolves to generic en", "EN", "en", false},
|
||||||
|
{"PT as source resolves to generic pt", "PT", "pt", false},
|
||||||
|
{"target-only code ZH is also a valid source", "ZH", "zh-Hans", false},
|
||||||
|
{"plain de", "de", "de", false},
|
||||||
|
{"unsupported code errors", "xx", "", true},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got, err := resolveSourceLang(tc.in)
|
||||||
|
if tc.wantErr {
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("resolveSourceLang(%q) = %q, want error", tc.in, got)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("resolveSourceLang(%q) unexpected error: %v", tc.in, err)
|
||||||
|
}
|
||||||
|
if got != tc.want {
|
||||||
|
t.Errorf("resolveSourceLang(%q) = %q, want %q", tc.in, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestTranslateByDeepLXPreflight exercises only the early-return validation
|
||||||
|
// paths that complete BEFORE any network I/O, so the suite stays hermetic.
|
||||||
|
func TestTranslateByDeepLXPreflight(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
source string
|
||||||
|
target string
|
||||||
|
text string
|
||||||
|
wantCode int
|
||||||
|
msgHas string
|
||||||
|
}{
|
||||||
|
{"empty text returns 404 (shipped contract)", "", "EN", "", http.StatusNotFound, "No text to translate"},
|
||||||
|
{"invalid target returns 400", "", "XX", "hello", http.StatusBadRequest, "unsupported target_lang"},
|
||||||
|
{"invalid source returns 400", "XX", "EN", "hello", http.StatusBadRequest, "unsupported source_lang"},
|
||||||
|
{"over-length ASCII returns 413", "", "EN", strings.Repeat("a", maxFreeTextLength+1), http.StatusRequestEntityTooLarge, "exceeds maximum length"},
|
||||||
|
// The cap counts runes, not bytes: 1501 multibyte runes is well over
|
||||||
|
// 1500 even though it is ~4500 bytes.
|
||||||
|
{"over-length multibyte counts runes returns 413", "", "EN", strings.Repeat("你", maxFreeTextLength+1), http.StatusRequestEntityTooLarge, "exceeds maximum length"},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
res, err := TranslateByDeepLX(tc.source, tc.target, tc.text, "", "", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if res.Code != tc.wantCode {
|
||||||
|
t.Fatalf("Code = %d, want %d (message: %q)", res.Code, tc.wantCode, res.Message)
|
||||||
|
}
|
||||||
|
if tc.msgHas != "" && !strings.Contains(res.Message, tc.msgHas) {
|
||||||
|
t.Errorf("Message %q does not contain %q", res.Message, tc.msgHas)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var uuidV4Re = regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`)
|
||||||
|
|
||||||
|
func TestNewInstanceIDIsUUIDv4(t *testing.T) {
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
id := newInstanceID()
|
||||||
|
if !uuidV4Re.MatchString(id) {
|
||||||
|
t.Fatalf("newInstanceID() = %q, not a valid RFC 4122 v4 UUID", id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// The process-wide instanceID must itself be a valid v4 UUID.
|
||||||
|
if !uuidV4Re.MatchString(instanceID) {
|
||||||
|
t.Errorf("package instanceID = %q, not a valid v4 UUID", instanceID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestOneshotRequestJSONShape locks the serialized request body: the field
|
||||||
|
// order and omitempty behavior are reverse-engineered to be byte-identical
|
||||||
|
// to the Chrome extension, so a struct-field reorder or tag change is a
|
||||||
|
// silent WAF signal that this test turns into a failure.
|
||||||
|
func TestOneshotRequestJSONShape(t *testing.T) {
|
||||||
|
anon := oneshotRequest{
|
||||||
|
Text: []string{"hello"},
|
||||||
|
TargetLang: "de",
|
||||||
|
UsageType: "Translate",
|
||||||
|
AppInformation: appInformation{
|
||||||
|
OS: "brex_macOS",
|
||||||
|
OSVersion: "brex_chrome_" + impersonatedChromeMajor + ".0.0.0",
|
||||||
|
AppVersion: chromeExtensionVersion,
|
||||||
|
AppBuild: "chrome_web_store",
|
||||||
|
InstanceID: instanceID,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
b, err := json.Marshal(anon)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("marshal: %v", err)
|
||||||
|
}
|
||||||
|
s := string(b)
|
||||||
|
|
||||||
|
if strings.Contains(s, "source_lang") {
|
||||||
|
t.Errorf("source_lang must be omitted when empty, got %s", s)
|
||||||
|
}
|
||||||
|
for _, key := range []string{`"text"`, `"target_lang"`, `"usage_type"`, `"app_information"`, `"os_version"`, `"app_version"`, `"app_build"`, `"instance_id"`} {
|
||||||
|
if !strings.Contains(s, key) {
|
||||||
|
t.Errorf("missing expected key %s in %s", key, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// encoding/json emits struct fields in declaration order; the body
|
||||||
|
// contract depends on it.
|
||||||
|
assertOrder(t, s, `"text"`, `"target_lang"`, `"usage_type"`, `"app_information"`)
|
||||||
|
|
||||||
|
// os_version must track the pinned Chrome major so the TLS handshake,
|
||||||
|
// user-agent, and self-reported version all tell one consistent story.
|
||||||
|
if !strings.Contains(s, "brex_chrome_"+impersonatedChromeMajor+".0.0.0") {
|
||||||
|
t.Errorf("os_version should embed impersonatedChromeMajor %q; got %s", impersonatedChromeMajor, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// With a source language set, the field appears with its value.
|
||||||
|
withSrc := anon
|
||||||
|
withSrc.SourceLang = "en"
|
||||||
|
b2, _ := json.Marshal(withSrc)
|
||||||
|
if !strings.Contains(string(b2), `"source_lang":"en"`) {
|
||||||
|
t.Errorf("source_lang should be present when set, got %s", string(b2))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertOrder(t *testing.T, haystack string, needles ...string) {
|
||||||
|
t.Helper()
|
||||||
|
last := -1
|
||||||
|
for _, n := range needles {
|
||||||
|
idx := strings.Index(haystack, n)
|
||||||
|
if idx == -1 {
|
||||||
|
t.Fatalf("missing %s in %s", n, haystack)
|
||||||
|
}
|
||||||
|
if idx < last {
|
||||||
|
t.Fatalf("field %s appears out of order in %s", n, haystack)
|
||||||
|
}
|
||||||
|
last = idx
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user