3772998137
GUI.html 里有一句硬语法错误:
progressFill?.style.width = '100%';
可选链不能做赋值目标,整个 <script> 块解析失败——页面加载看着正常,但所有按钮
都没反应,浏览器只在控制台报一条错,极容易被误判成「服务连不上」。
改成 if (progressFill) ...,并用 node --check 加了回归测试。
网页端调接口是跨域请求,而服务端没开 CORS:/api/v1/i2t 发 JSON 会先发预检
OPTIONS,直接失败;上传虽然能发出去,但响应读不到。加了 CORSMiddleware,
来源由 cors_allow_origins 配置(默认 ["*"],[] 可关)。命令行脚本不受影响,
所以这个坑只在网页端暴露。
顺带修掉 GUI 里几处:
· 安静模式下没有进度条,onProgress 会拿 undefined 赋值
· 字段渲染用 innerHTML 拼值——OCR 结果来自用户上传的图片,内容不可信,改 textContent
· 置信度/超时输入框清空时 parseFloat 得到 NaN,会让服务端返回 422,加回退值
· 处理完重置 fileInput.value,否则再选同一个文件不触发 change
· 页面经 http(s) 打开时,服务地址默认填 location.origin(同源,免配)
测试:新增 CORS 预检/关闭两条用例,以及 GUI.html 的 JS 语法检查(用 node,
没装 node 则跳过)。101 passed, 1 skipped。
Co-Authored-By: Claude Code <noreply@anthropic.com>
99 lines
3.1 KiB
Python
99 lines
3.1 KiB
Python
from io import BytesIO
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
from uuid import uuid4
|
|
|
|
from fastapi.testclient import TestClient
|
|
from PIL import Image
|
|
|
|
from ocr_server.app import create_app
|
|
from ocr_server.config import Settings
|
|
|
|
|
|
def client() -> TestClient:
|
|
directory = TemporaryDirectory()
|
|
app = create_app(Settings(upload_dir=Path(directory.name)))
|
|
test_client = TestClient(app)
|
|
test_client._ocr_test_directory = directory # keep temporary storage alive
|
|
return test_client
|
|
|
|
|
|
def png_bytes() -> bytes:
|
|
output = BytesIO()
|
|
Image.new("RGB", (20, 10), "white").save(output, format="PNG")
|
|
return output.getvalue()
|
|
|
|
|
|
def test_health() -> None:
|
|
response = client().get("/healthz")
|
|
assert response.status_code == 200
|
|
assert response.json() == {
|
|
"code": 0,
|
|
"message": "success",
|
|
"data": {"status": "ok"},
|
|
}
|
|
|
|
|
|
def test_cors_allows_browser_page_clients() -> None:
|
|
"""网页客户端(scripts/GUI.html)从别的来源调接口时,没有 CORS 头浏览器会把响应全拦掉。
|
|
|
|
`/api/v1/i2t` 发的是 JSON,浏览器还会先发一个预检 OPTIONS,缺 CORS 直接失败。
|
|
命令行脚本不受影响,所以这个问题只在网页端暴露。
|
|
"""
|
|
response = client().options(
|
|
"/api/v1/i2t",
|
|
headers={
|
|
"Origin": "null",
|
|
"Access-Control-Request-Method": "POST",
|
|
"Access-Control-Request-Headers": "content-type",
|
|
},
|
|
)
|
|
assert response.status_code == 200
|
|
assert response.headers["access-control-allow-origin"] == "*"
|
|
assert "POST" in response.headers["access-control-allow-methods"]
|
|
|
|
# 实际请求也要带上这个头,否则浏览器读不到响应
|
|
actual = client().post(
|
|
"/api/v1/i2t", json={"file_id": str(uuid4())}, headers={"Origin": "null"}
|
|
)
|
|
assert actual.headers["access-control-allow-origin"] == "*"
|
|
|
|
|
|
def test_cors_can_be_disabled_with_empty_origins() -> None:
|
|
directory = TemporaryDirectory()
|
|
app = create_app(
|
|
Settings(upload_dir=Path(directory.name), cors_allow_origins=[])
|
|
)
|
|
test_client = TestClient(app)
|
|
test_client._ocr_test_directory = directory
|
|
|
|
response = test_client.post(
|
|
"/api/v1/i2t", json={"file_id": str(uuid4())}, headers={"Origin": "null"}
|
|
)
|
|
assert "access-control-allow-origin" not in response.headers
|
|
|
|
|
|
def test_upload_and_missing_image() -> None:
|
|
response = client().post(
|
|
"/api/v1/upload",
|
|
files={"file": ("sample.png", png_bytes(), "image/png")},
|
|
)
|
|
assert response.status_code == 201
|
|
payload = response.json()
|
|
assert payload["code"] == 0
|
|
assert payload["message"] == "success"
|
|
assert payload["data"]["filename"] == "sample.png"
|
|
assert payload["data"]["width"] == 20
|
|
assert payload["data"]["height"] == 10
|
|
|
|
missing = client().post("/api/v1/i2t", json={"file_id": str(uuid4())})
|
|
assert missing.status_code == 404
|
|
|
|
|
|
def test_rejects_invalid_image() -> None:
|
|
response = client().post(
|
|
"/api/v1/upload",
|
|
files={"file": ("sample.txt", b"not an image", "text/plain")},
|
|
)
|
|
assert response.status_code == 400
|