mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2026-12-01 23:07:51 +00:00
fix(translate): answer an empty text list with an empty list, not 404
`"text": []` names a list that happens to be empty, and the response can
render that perfectly well, so rejecting it bought nothing: it was the
original "No text to translate" condition surviving for the one shape the
code had no answer to. It is answered with `{"data": []}` now — the same
rule as every other text, applied to none of them.
A body that never named `text` is a different thing, a payload without its
parameter: it answers 400 Invalid request payload instead of the 404 that
stood in for it, which is also what the endpoint answers for a body it
cannot bind. That is the only refusal left.
Refs #244.
This commit is contained in:
@@ -118,8 +118,6 @@ func TestTranslateByDLXValidationFailsBeforeUpstream(t *testing.T) {
|
||||
texts []string
|
||||
code int
|
||||
}{
|
||||
{"no texts", nil, http.StatusNotFound},
|
||||
{"empty array", []string{}, http.StatusNotFound},
|
||||
{"over the limit", []string{strings.Repeat("a", maxFreeTextLength+1)}, http.StatusRequestEntityTooLarge},
|
||||
{"batch over the limit", []string{strings.Repeat("a", maxFreeTextLength), "b"}, http.StatusRequestEntityTooLarge},
|
||||
// 751 runes in the batch, but 1502 UTF-16 units: the case a rune count
|
||||
@@ -141,13 +139,16 @@ func TestTranslateByDLXValidationFailsBeforeUpstream(t *testing.T) {
|
||||
}
|
||||
|
||||
// Every text is answered — translated when it carries something, returned as it
|
||||
// is when it is blank — so only a request that names no text at all is an
|
||||
// error, and none of the cases below reaches DeepL.
|
||||
// is when it is blank — so nothing about emptiness is an error, and none of the
|
||||
// cases below reaches DeepL. Data is always allocated, so an empty request
|
||||
// marshals as `[]` rather than `null`.
|
||||
func TestTranslateByDLXAnswersBlankTextsWithThemselves(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
texts []string
|
||||
}{
|
||||
{"no texts", nil},
|
||||
{"empty array", []string{}},
|
||||
{"single empty", []string{""}},
|
||||
{"two empty", []string{"", ""}},
|
||||
{"whitespace only", []string{" \t "}},
|
||||
@@ -163,8 +164,16 @@ func TestTranslateByDLXAnswersBlankTextsWithThemselves(t *testing.T) {
|
||||
if result.Code != http.StatusOK {
|
||||
t.Fatalf("TranslateByDLX() code = %d, want %d", result.Code, http.StatusOK)
|
||||
}
|
||||
if !reflect.DeepEqual(result.Data, tt.texts) {
|
||||
t.Fatalf("TranslateByDLX() data = %q, want %q", result.Data, tt.texts)
|
||||
if result.Data == nil {
|
||||
t.Fatal("TranslateByDLX() data = nil, want an allocated slice so it marshals as []")
|
||||
}
|
||||
if len(result.Data) != len(tt.texts) {
|
||||
t.Fatalf("TranslateByDLX() data = %q, want %d element(s)", result.Data, len(tt.texts))
|
||||
}
|
||||
for i, text := range tt.texts {
|
||||
if result.Data[i] != text {
|
||||
t.Fatalf("TranslateByDLX() data[%d] = %q, want %q", i, result.Data[i], text)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user