Make the request->response path unit-testable by confining actix ServiceRequest to the edge

Abierto
#1,316 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
55/100
Tipo de issue
Refactorización
Claridad
Bien especificado
Estado de actividad
Tranquilo
Stack tecnológico
rust
Área
backend, testing

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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:

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:

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 RequestInfo boundary at the edge and migrating call sites incrementally. ExecutionContext already exists and already wraps RequestInfo, 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.

Lenguaje dominante
Rust
Estrellas
2.6k
Forks
175
Merge medio
14 h 16 min
PR fusionados (30 d)
43

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de sqlpage/SQLPage

Todos los issues de sqlpage/SQLPage

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.