Skip to content

test(axum-http-server): strengthen package-local test coverage (#2136) - #2137

Merged
josecelano merged 59 commits into
torrust:developfrom
josecelano:2136-add-tests-axum-http-server
Sep 4, 2026
Merged

josecelano merged 59 commits into
torrust:developfrom
josecelano:2136-add-tests-axum-http-server

Conversation

@josecelano

@josecelano josecelano commented Sep 3, 2026 •

Copy link
Copy Markdown
Member

Summary

Strengthens package-local test coverage for the Axum HTTP tracker server.

  • Adds focused bencoded announce and scrape response-mapping coverage.
  • Adds authentication-key extraction and request-ID middleware coverage.
  • Covers listener-registration cleanup and clarifies lifecycle scenarios.
  • Records coverage evidence, refactor plans, and reusable test-design guidance.

The package-source coverage evidence is test-inclusive and records an increase from 93.82% to 95.07% lines, 91.66% to 92.99% regions, and 89.54% to 90.86% functions.

Tracking correction

Closes #2136.

Supersedes #2129, which was opened against #1348 before historical package-name changes were reconciled. Issue #1348 remains the separate udp-core testing subissue; #1349 remains the separate http-core testing subissue.

Validation

  • cargo test -p torrust-tracker-axum-http-server (34 unit tests, 55 integration tests)
  • cargo +nightly fmt --all -- --check
  • linter all
  • mandatory pre-commit gate

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A few references still point to the old #1348 identity (breaking links/traceability) and one test helper should use a bounded body read limit for consistency with the new patterns.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR strengthens the torrust-tracker-axum-http-server package’s local “safety net” by adding focused, fast tests around HTTP tracker transport boundaries (bencoded announce/scrape mapping, request-id middleware, auth-key extraction) and server lifecycle behavior, while also updating the project’s testing documentation and issue tracking to correctly close #2136.

Changes:

  • Add/reshape package-local tests for request-id propagation/generation, announce/scrape response bencode mapping, auth-key extraction behavior, and HTTP server lifecycle/registration cleanup.
  • Introduce durable testing guidance + a refactoring-pattern catalog, and wire it into the write-unit-test / create-issue skills.
  • Correct the historical issue identity mismatch (move axum-http-server spec from the old #1348 identity to #2136) and update EPIC tracking/coverage evidence.
File summaries
File Description
packages/axum-http-server/src/v1/routes.rs Adds in-process router tests for request-id propagation and UUID generation.
packages/axum-http-server/src/v1/handlers/scrape.rs Extracts a narrow service seam and expands handler-level tests for bencoded scrape success/failure mapping.
packages/axum-http-server/src/v1/handlers/announce.rs Adds scenario-based tests asserting compact vs non-compact announce bencode mapping + refines error tests.
packages/axum-http-server/src/v1/extractors/authentication_key.rs Fixes doc contract (HTTP 200 failure) and adds parsing + in-process wire-level extractor tests.
packages/axum-http-server/src/server.rs Refactors lifecycle tests into explicit “scenario fixture” arrangements and adds duplicate-registration cleanup assertions.
packages/axum-http-server/README.md Documents how to run tests and measure coverage for the package.
packages/axum-http-server/Cargo.toml Adds test-only tower util support via dev-dependencies.
docs/testing/refactoring-patterns/scenario-fixtures-for-causal-initial-state.md Adds a documented pattern for scenario fixtures that name the causal initial state.
docs/testing/refactoring-patterns/scenario-fixture-independent-expected-outputs.md Adds a documented pattern for scenario fixtures that own independent expected outputs.
docs/testing/refactoring-patterns/README.md Adds a catalog index for the testing refactoring patterns.
docs/testing/README.md Adds top-level durable testing guidance entry point.
docs/issues/open/2136-1347-add-tests-axum-http-server/test-refactor-plans/server-tests.md Records the server test refactor plan and decisions/evidence.
docs/issues/open/2136-1347-add-tests-axum-http-server/test-refactor-plans/scrape-tests.md Records the scrape handler refactor plan and decisions/evidence.
docs/issues/open/2136-1347-add-tests-axum-http-server/test-refactor-plans/routes-tests.md Records the routes test plan and decisions/evidence.
docs/issues/open/2136-1347-add-tests-axum-http-server/test-refactor-plans/README.md Adds shared test-refactor plan guidance for the issue-local plans.
docs/issues/open/2136-1347-add-tests-axum-http-server/test-refactor-plans/drafts/shared-handler-test-bootstrap.md Documents and closes the cross-file bootstrap draft assessment.
docs/issues/open/2136-1347-add-tests-axum-http-server/test-refactor-plans/authentication-key-extractor-tests.md Records the auth-key extractor refactor plan and decisions/evidence.
docs/issues/open/2136-1347-add-tests-axum-http-server/test-refactor-plans/announce-tests.md Records the announce handler refactor plan and decisions/evidence.
docs/issues/open/2136-1347-add-tests-axum-http-server/ISSUE.md Introduces the corrected axum-http-server testing subissue spec (#2136).
docs/issues/open/2136-1347-add-tests-axum-http-server/coverage-evidence.md Records reproducible, human-readable package-source coverage evidence for #2136.
docs/issues/open/1349-1347-add-tests-axum-rest-api-server/ISSUE.md Updates testing/coverage requirements for the rest-api-server subissue spec.
docs/issues/open/1348-1347-add-tests-axum-http-server/ISSUE.md Removes the now-invalid axum-http-server spec that incorrectly used the #1348 identity.
docs/issues/open/1347-overhaul-packages-testing/EPIC.md Updates EPIC tracking, adds #2136 row, and records improved coverage evidence expectations.
docs/index.md Adds docs/testing/README.md to the docs index.
docs/AGENTS.md Documents docs/testing/ as a durable guidance location.
.github/skills/dev/testing/write-unit-test/SKILL.md Links to the testing catalog and adds guidance on state-centered Arrange patterns (scenario fixtures/builders).
.github/skills/dev/planning/create-issue/SKILL.md Adds requirements for coverage-evidence documents and progressive test-development loops in specs.
Review details
  • Files reviewed: 27/27 changed files
  • Comments generated: 3
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread packages/axum-http-server/README.md Resolved
@codecov

codecov Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.61111% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.36%. Comparing base (d54d418) to head (7079ad3).

Files with missing lines Patch % Lines
packages/axum-http-server/src/server.rs 96.46% 3 Missing and 1 partial ⚠️
...ttp-server/src/v1/extractors/authentication_key.rs 91.66% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2137      +/-   ##
===========================================
+ Coverage    84.26%   84.36%   +0.09%     
===========================================
  Files          351      351              
  Lines        28008    28336     +328     
  Branches     28008    28336     +328     
===========================================
+ Hits         23602    23906     +304     
- Misses        4005     4029      +24     
  Partials       401      401              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@josecelano
josecelano force-pushed the 2136-add-tests-axum-http-server branch from ca2f7ca to 4e3a8ce Compare September 4, 2026 08:09
@josecelano
josecelano requested a lite review from Copilot September 4, 2026 09:03
@josecelano josecelano changed the title test(axum-http-server): add package-local transport tests (#2136) test(axum-http-server): strengthen package-local test coverage (#2136) Sep 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The repository-local spec for issue #1349 conflicts with the PR’s stated issue mapping/EPIC table updates and should be reconciled to avoid incorrect tracking/navigation.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (1)

docs/issues/open/1349-1347-add-tests-axum-rest-api-server/ISSUE.md:41

  • This document is still keyed to GitHub issue #1349 and describes the Axum REST API server, but the PR’s tracking correction and the updated EPIC subissue table now treat #1349 as the separate http-core testing subissue (with no local spec yet). This leaves the repository-local spec set inconsistent and will mislead navigation (EPIC says “Not yet created” while this file exists). Reconcile by either renumbering/renaming this spec to the correct GitHub issue for axum-rest-api-server, or updating the EPIC + mentioned-issue mapping if that correction is wrong.
- Record the starting package coverage baseline and the coverage increase achieved while testing critical behavior in an issue-local `coverage-evidence.md` document. Include the reproducible command, measurement scope, aggregate comparison, per-file results, and prioritized uncovered functions or regions; do not commit raw tool reports unless they are made human-readable.
- Prefer fast unit tests close to the implementation whenever they provide the appropriate regression boundary.
- Review and improve tests for HTTP/HTTPS startup, registration failure cleanup, listener errors, and graceful shutdown.
- Test public routing and middleware contracts: unauthenticated health checks, protected API routes, token sources and precedence, and transport headers.
- Test private and listed configuration-gated route composition and unavailable-route behavior.
  • Files reviewed: 28/28 changed files
  • Comments generated: 1
  • Review effort level: Lite

@josecelano

Copy link
Copy Markdown
Member Author

ACK 7079ad3

@josecelano
josecelano merged commit 6f813ae into torrust:develop Sep 4, 2026
18 checks passed
josecelano added a commit that referenced this pull request Sep 4, 2026
84902b0 docs(review): align PR 2139 review audit table (Jose Celano)
d7c3419 docs(review): document PR #2139 Copilot suggestions audit (Jose Celano)
83b2372 docs(issues): address testing strategy review feedback (Jose Celano)
e0e3314 docs(issues): add issue specification for #2138 (Jose Celano)

Pull request description:

  ## Summary

  - Add the approved folder-style specification for issue #2138, documenting the proposed testing strategy guide.
  - Define the test taxonomy, test-layer selection strategy, documentation ownership boundaries, acceptance criteria, and verification evidence.
  - Record a non-blocking implementation-time check for the refactoring-pattern catalog proposed in PR #2137.

  ## Files changed

  - `docs/issues/open/2138-document-testing-strategy/ISSUE.md`

  ## Validation

  - Pre-commit hook: passed (`cargo machete`, `cargo deny check bans`, `linter all`, hadolint, and documentation tests).
  - Pre-push hook: passed (nightly formatting, nightly workspace check, nightly documentation build, and full tests).

  Related to #2138

ACKs for top commit:
  josecelano:
    ACK 84902b0

Tree-SHA512: 80e57548ef1d054ea922aea9ca02e82e4c12d57323c468d7b4efd16488d983ec89f90b6b303d4eb77f66bda76696f5ee07196be71d773f4bf58c9731e93b3f6f

@da2ce7 da2ce7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a post-merge review of #2137 as it stands at e170b9620 on the default branch.

What still runs: the one production edit, the private handle_scrape delegation that handle calls (packages/axum-http-server/src/v1/handlers/scrape.rs:83-93), is unchanged and behaviour-preserving. The scrape and announce build_response mapping tests, the handle failure-response test, the reworked scrape mode and client-IP tests, the authentication-key extractor tests and the two request-ID tests in routes.rs are all present, with expected error texts updated by later changes (#2915, #2999). The server.rs scenario structs remain. The duplicate-registration listener-release test now runs through start_with_cancellation, and the start/stop test went with the legacy shutdown API in ef59834. The release test's original port choice could race with parallel tests binding 127.0.0.1:0; ccf0a54 (#2393) fixed that, so it is history here.

[Minor] The three scrape tests named for the access checks cannot fail if those checks are removed: packages/axum-http-server/src/v1/handlers/scrape.rs:379 (missing key), scrape.rs:404 (unregistered key) and scrape.rs:437 (torrent not whitelisted). The fixture at scrape.rs:187-191 builds an empty InMemoryTorrentRepository with persisted downloads disabled. For such a repository, ScrapeHandler::handle_scrape also returns SwarmMetadata::zeroed() for an authorized info hash (packages/tracker-core/src/scrape_handler.rs:172-179). So if the authentication_is_required() && !is_authenticated(..) branch at packages/http-core/src/services/scrape.rs:109 were deleted, or the whitelist check skipped, all three tests would still equal ScrapeData::zeroed(..) and pass. The same rules are pinned with a seeded swarm at packages/http-core/src/services/scrape.rs:504 and :528 (added in 8d1a31d) and at packages/axum-http-server/tests/server/v1/contract/configured_as_whitelisted.rs:101. The effect is therefore coverage that reads as more than it is, not missing protection. Smallest fix: keep the torrent repository in TestServices and announce one seeder for the sample info hash before scraping, so that losing a check shows up as non-zero counts against the zeroed expectation. Alternatively, drop the three tests as duplicates of the cases above.

[Nit] The handle_scrape seam at packages/axum-http-server/src/v1/handlers/scrape.rs:83 has no test caller. The five mode and client-IP tests (scrape.rs:386, :412, :443, :476, :512) still call the http-core ScrapeService::handle_scrape directly, while the announce tests go through the matching handle_announce (packages/axum-http-server/src/v1/handlers/announce.rs:453). As it stands, the wrapper is an indirection nothing exercises, and those five tests exercise another package's service rather than this module. Smallest fix: call super::handle_scrape(&test_services.scrape_service, ..) in those five tests, or fold the wrapper back into handle.

[Nit] Since 4b2af7a, the router-level test it_should_encode_an_invalid_key_format_failure_response_for_an_invalid_path_key (packages/axum-http-server/src/v1/extractors/authentication_key.rs:248) pins the wrong-length reply (expected 32 characters, got 11), so its name no longer says what it asserts. Its fixture invalid_key breaks both the length rule and the character rule. Its substring check at authentication_key.rs:261 is weaker than the whole-text assert_eq its unit siblings use at :187 and :202, and would also accept a reply that carried more text after the message. The FailedToDeserializePathParams branch of custom_error (authentication_key.rs:128-130) has no test; a path key that percent-decodes to invalid UTF-8, such as /announce/%FF, reaches it. Smallest fix: name the test after the length case, assert the whole failure_reason, and add a /%FF router case that expects the allowed-characters reply.

Checked and found sound:

  • handle still turns an HttpScrapeError into HTTP 200 with a bencoded failure reason (scrape.rs:74-77), and the new handle test pins that.
  • The build_response tests decode the body and compare it with independently written expectations for one and two scrape files and for the compact, omitted-compact and non-compact announce replies.
  • The announce private, listed and client-IP tests assert errors, so they do discriminate.
  • The scrape test services run with no event sender, which ScrapeService::send_event treats as no work.
  • The extractor's module text and its code agree on status 200.
  • The request-ID tests match the layers at routes.rs:80-81 and routes.rs:156.
  • The registration-failure path awaits the aborted server task and controller before returning (packages/axum-http-server/src/server.rs:246-256), so the release check at server.rs:889 does not race the drop.
  • The added tower util feature is a dev-dependency only.
  • No changed line panics on request input or carries a secret.

This branch was previously deployed

1 inactive deployment
coverage — 7079ad37 Deployed Sep 4, 2026 by josecelano via Generate Coverage Report #1915
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Overhaul Packages Testing: Add tests for axum-http-server package

3 participants