Make the request->response path unit-testable by confining actix ServiceRequest to the edge
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
Start with src/webserver/http_request_info.rs and src/webserver/http.rs, especially RequestInfo, render_sql, build_response_header_and_stream, stream_response, and main_handler. Trace how ServiceRequest is extracted and passed through these functions, then move Actix-specific handling to an edge adapter and make downstream decisions use request data and a stream. Done means the response path can be unit-tested without an Actix runtime or live database, covering negotiation, status mapping, streaming, and 404 behavior.
由索引模型根据 Issue 内容生成。
描述
The request -> response path is not unit-testable
The high-value HTTP logic in SQLPage (Accept negotiation, error -> status mapping, the header/body streaming transition, and the 404 status decision) currently cannot be exercised without spinning up a full actix runtime and a real DB-backed AppState. That is why none of it has unit coverage today, and why subtle HTTP-correctness bugs (wrong status codes, wrong content type for a given Accept, 404-vs-200) can slip in unnoticed.
The root cause is that actix's &mut ServiceRequest is threaded all the way through routing, execution, and rendering instead of being confined to the edge.
Where the type flows
The actix request type is dragged through every layer of the core:
extract_request_info(req: &mut ServiceRequest, ...)render_sql(srv_req: &mut ServiceRequest, ...)process_sql_request(req: &mut ServiceRequest, ...)main_handler(mut service_request: ServiceRequest)
A plain RequestInfo data struct already exists, which is exactly the shape downstream code wants. But it is only ever constructed inside extract_request_info, after the actix extraction, so all the interesting logic stays locked behind the actix request type.
The decisions worth testing all live behind that wall:
- Accept negotiation in
render_sql - error -> status mapping and the header/body transition in
build_response_header_and_streamandstream_response - the 404 status decision (
Accept: html-> render404.sqland forceStatusCode::NOT_FOUND, otherwise plaintext) inmain_handler
Evidence of the gap
render_sql, process_sql_request, build_response_header_and_stream, and stream_response have no unit tests. The only tests in http.rs cover span-name string formatting (request_span_name, sql_execution_span_name).
$ grep -rn "ServiceRequest" src | wc -l
45
$ grep -n "fn .*ServiceRequest" src/webserver/http.rs
314:fn request_span_route(request: &ServiceRequest) -> Cow<'_, str> {
320:fn request_span_name(request: &ServiceRequest) -> String {
343:fn set_otel_parent(request: &ServiceRequest, span: &Span) {
355: fn on_request_start(request: &ServiceRequest) -> Span {
(render_sql, process_sql_request, and main_handler take the request as an async fn argument, so they do not show up in that one-line grep, but they all carry &mut ServiceRequest / ServiceRequest.)
Proposed change
Push the actix-touching code to a thin adapter at the very edge, e.g. fn from_actix(req) -> RequestInfo, and make everything downstream operate on the existing RequestInfo / ExecutionContext plus a Stream<DbItem>.
build_response_header_and_stream is already generic over S: Stream<Item = DbItem>, so once the actix request is gone from its inputs it can be driven by a hand-rolled in-memory stream in tests. "Given this request + this sequence of DbItems, what status / headers / body come out" then becomes a plain unit test, with no actix runtime and no live DB.
Risks and mitigations
- Touches many signatures. Mitigate by introducing the
RequestInfoboundary at the edge and migrating call sites incrementally.ExecutionContextalready exists and already wrapsRequestInfo, so most downstream code can switch to it with minimal churn. - Behavior drift during the refactor. The extraction itself is mechanical; the move is from "actix type in, decision out" to "data struct in, decision out", with the actix-specific parsing isolated in the adapter.
Expected win
A unit-testable request -> response path, which unblocks fixing status-code and content-format bugs with proper regression tests instead of relying on full end-to-end runs.
- 主要语言
- Rust
- 星标
- 2.6k
- 派生
- 175
- 平均合并
- 14 小时 16 分钟
- 30 天内合并 PR
- 43
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
sqlpage/SQLPage 的其他 Issue
-
enhancement
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 65/100
sqlpage/SQLPage#1218 · 3 条评论 ·
维护者通常 1 天内回复
-
There is no convenient way to reproduce issues可能已有人在做 @81reap 于 26 天前认领。 未关闭enhancement
sqlpage/SQLPage#1566 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Confusing sort interface可能已有人在做 @lovasoa 于 73 天前认领。 未关闭bug
难度 4/5 3-5 天 新手友好度 20/100
sqlpage/SQLPage#1442 · 3 条评论 ·
维护者通常 1 天内回复
-
Mutation Testing Results :: Dead Code + Missing Tests可能重新可做 @81reap 于 61 天前认领,目前没有进行中的 PR。 未关闭
sqlpage/SQLPage#1396 · 5 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 70/100
symposium-dev/symposium#530 ·
维护者通常 5 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
topgrade-rs/topgrade#2540 ·
维护者通常 1 天内回复
-
Invalid JSON options for ngram and edge_ngram silently use defaults可能已有人在做 @Tilendlesa 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 76/100
noemaforge/vimanam#153 ·
维护者通常 1 天内回复