GhEx.Testing (gh_ex v0.3.4)

Copy Markdown View Source

Helpers for testing gh_ex consumers with Req.Test.

client/1 installs a named Req.Test stub and disables retries so a stubbed transient failure returns immediately. The response builders cover common GitHub metadata, conditional requests, and rate-limit classifications.

Req.Test uses Plug connections. Add Plug to the consuming application's test dependencies when using this module:

{:plug, "~> 1.16", only: :test}

Summary

Functions

Builds a retry-disabled client that sends requests through a named Req.Test stub.

Sends a JSON response with a standard GitHub core rate-limit snapshot.

Sends an empty 304 Not Modified response.

Sends one of GitHub's rate-limit response shapes.

Types

rate_limit_kind()

@type rate_limit_kind() :: :retry_after | :primary | :secondary

Functions

client(stub)

@spec client(term()) :: GhEx.Client.t()

Builds a retry-disabled client that sends requests through a named Req.Test stub.

Examples

iex> client = GhEx.Testing.client(MyApp.GitHubStub)
iex> client.req_options
[plug: {Req.Test, MyApp.GitHubStub}, retry: false]

json(conn, body)

@spec json(term(), term()) :: term()

Sends a JSON response with a standard GitHub core rate-limit snapshot.

Respects a response status already set on the connection, as Req.Test.json/2 does.

Examples

iex> plug = fn conn -> GhEx.Testing.json(conn, %{ok: true}) end
iex> response = Req.get!(url: "https://example.test", plug: plug, retry: false)
iex> response.body
%{"ok" => true}
iex> response.headers["x-ratelimit-remaining"]
["4999"]

not_modified(conn)

@spec not_modified(term()) :: term()

Sends an empty 304 Not Modified response.

Examples

iex> plug = &GhEx.Testing.not_modified/1
iex> response = Req.get!(url: "https://example.test", plug: plug, retry: false)
iex> response.status
304

rate_limited(conn, atom)

@spec rate_limited(term(), rate_limit_kind()) :: term()

Sends one of GitHub's rate-limit response shapes.

  • :retry_after sends a 429 with retry-after: 1.
  • :primary sends a 403 with an exhausted core bucket and reset time.
  • :secondary sends a body-only secondary-limit 403.

Examples

iex> plug = fn conn -> GhEx.Testing.rate_limited(conn, :secondary) end
iex> response = Req.get!(url: "https://example.test", plug: plug, retry: false)
iex> response.status
403
iex> response.body["message"] =~ "secondary rate limit"
true