Skip to content

Extract reusable HTTP assertion library - #105

Merged
korya merged 3 commits into
masterfrom
korya-refactor-http-assert-library
Aug 30, 2026
Merged

Extract reusable HTTP assertion library#105
korya merged 3 commits into
masterfrom
korya-refactor-http-assert-library

Conversation

@korya

@korya korya commented Aug 30, 2026

Copy link
Copy Markdown
Owner

Problem

Go programs cannot reuse http-assert's client or assertions because every exported type currently lives inside the CLI package.

The command already has a composable assertion model, response decoding, and an HTTP client, but package main makes that functionality unimportable. Keeping retry orchestration and formatted error strings in the same layer would also freeze CLI policy into any public API. Using Go's default HTTP client would additionally leave library calls without a total request deadline.

Solution

Make the root module an importable assertion library and move the command, retries, and presentation into cmd/http-assert.

flowchart LR
    Consumer["Go package"] --> Library["github.com/korya/http-assert"]
    CLI["cmd/http-assert"] --> Library
    Library --> HTTP["net/http"]
    CLI --> Policy["retries, logs, formatting, exit codes"]
Loading

The library invokes its configured http.Client once and returns ordered, structured outcomes that distinguish assertion failures from evaluation errors. It validates requests and assertions before transport, retains decoded response bytes for consumers, and exposes all existing assertion constructors without adding retry or formatting helpers to the initial API.

The zero-value client uses a shared 20-second whole-request timeout instead of the unbounded http.DefaultClient; callers can still inject another client or apply a shorter context deadline. Constructors that parse status expressions, regular expressions, or jq queries retain explicit error returns, while ha.Must(...) provides the conventional one-line panic form for static, programmer-controlled expressions.

The CLI imports the library as ha, preserves its existing human-readable output and exit behavior, and remains the sole owner of retries. Source installs now use github.com/korya/http-assert/cmd/http-assert; published binary names and archives remain unchanged.

No visual change: this refactors a Go API and CLI boundary while intentionally preserving terminal output.

Other Changes

  • Relocate CLI unit and subprocess E2E tests with the command, while adding external-package examples and focused public API tests.
  • Update README examples, build/install recipes, release configuration, timeout guidance, and the changelog for the new package boundary.
  • Validate all public library behaviors, including input rejection, ordered outcomes, response ownership, body decoding, transport/read failures, the bounded default, and both ha.Must paths. Merged E2E coverage reaches 100% of CLI statements; library coverage reaches 99.6%, with only the defensive zstd constructor-error branch not inducible through its fixed valid options.
  • Confirm the root package dependency graph does not include Cobra or pflag.

🤖 Generated with Claude Code

korya and others added 3 commits August 30, 2026 18:30
Previously, every exported assertion and HTTP client lived in package main, so Go programs could not
reuse them. Move the CLI to cmd/http-assert and make the root module an importable package with
structured failures, evaluation errors, and ordered assertion outcomes.

Keep retries, logging, exit codes, and human-readable formatting in the CLI. Preserve its behavior
with the relocated subprocess suite while adding focused coverage for the public API and its error
paths.

Co-Authored-By: OpenAI Codex (GPT-5) <noreply@openai.com>
The zero-value library client previously inherited net/http's unbounded total request timeout, so a
stalled endpoint could prevent a health check from returning. Use a shared client with a 20-second
whole-request limit while preserving custom clients and shorter request-context deadlines.

Co-Authored-By: OpenAI Codex (GPT-5) <noreply@openai.com>
Parsed assertion constructors correctly return errors, but static expressions become noisy when
used inline. Add Must to preserve the constructor contract while giving programmer-owned status,
regular-expression, and jq assertions the conventional one-line panic form.

Co-Authored-By: OpenAI Codex (GPT-5) <noreply@openai.com>
@korya
korya marked this pull request as ready for review August 30, 2026 23:01
@korya
korya merged commit 84b8a64 into master Aug 30, 2026
8 checks passed
@korya
korya deleted the korya-refactor-http-assert-library branch August 30, 2026 23:03
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.

1 participant