mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2026-12-01 14:57:52 +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:
@@ -159,6 +159,17 @@ func Router(cfg *Config) *gin.Engine {
|
||||
return
|
||||
}
|
||||
|
||||
// A body without `text` never named a text to translate, which is a
|
||||
// payload error. `"text": []` names a list that happens to be empty,
|
||||
// and is answered with an empty list, so it is not caught here.
|
||||
if !req.Text.Batch && len(req.Text.Texts) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": http.StatusBadRequest,
|
||||
"message": "Invalid request payload",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
sourceLang := req.SourceLang
|
||||
targetLang := req.TargetLang
|
||||
texts := req.Text.Texts
|
||||
|
||||
@@ -2,9 +2,13 @@ package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// The `text` field of POST /translate accepts either shape; decoding uses the
|
||||
@@ -23,6 +27,7 @@ func TestPayloadTextUnmarshalJSON(t *testing.T) {
|
||||
{name: "single-element array", payload: `{"text":["Hello"]}`, wantTexts: []string{"Hello"}, wantBatch: true},
|
||||
{name: "empty array", payload: `{"text":[]}`, wantTexts: []string{}, wantBatch: true},
|
||||
{name: "null", payload: `{"text":null}`, wantTexts: []string{""}},
|
||||
{name: "no text field", payload: `{}`, wantTexts: nil},
|
||||
{name: "number", payload: `{"text":1}`, wantErr: true},
|
||||
{name: "object", payload: `{"text":{}}`, wantErr: true},
|
||||
{name: "mixed array", payload: `{"text":["Hello",1]}`, wantErr: true},
|
||||
@@ -46,3 +51,40 @@ func TestPayloadTextUnmarshalJSON(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// POST /translate mirrors the shape it was given, and no shape that carries
|
||||
// text is an error any more — an empty text is answered with itself and an
|
||||
// empty array with an empty array. Every case here is answered (or refused)
|
||||
// before a translation is attempted, so the test never reaches DeepL.
|
||||
func TestTranslateHandlerAnswersEmptyText(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := Router(&Config{})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
payload string
|
||||
wantCode int
|
||||
wantInBody string
|
||||
}{
|
||||
{"empty array", `{"text":[],"target_lang":"EN"}`, http.StatusOK, `"data":[]`},
|
||||
{"empty string", `{"text":"","target_lang":"EN"}`, http.StatusOK, `"data":""`},
|
||||
{"null", `{"text":null,"target_lang":"EN"}`, http.StatusOK, `"data":""`},
|
||||
{"array of empty", `{"text":[""],"target_lang":"EN"}`, http.StatusOK, `"data":[""]`},
|
||||
{"no text field", `{"target_lang":"EN"}`, http.StatusBadRequest, `Invalid request payload`},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodPost, "/translate", strings.NewReader(tt.payload))
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
router.ServeHTTP(recorder, request)
|
||||
|
||||
if recorder.Code != tt.wantCode {
|
||||
t.Fatalf("POST /translate %s = %d %s, want %d", tt.payload, recorder.Code, recorder.Body.String(), tt.wantCode)
|
||||
}
|
||||
if !strings.Contains(recorder.Body.String(), tt.wantInBody) {
|
||||
t.Fatalf("POST /translate %s body = %s, want it to contain %s", tt.payload, recorder.Body.String(), tt.wantInBody)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user