Skip to content

Honor the Allow header - #340

Merged
dblock merged 1 commit into
codegram:masterfrom
dblock:dblock/allow-header
Nov 22, 2026
Merged

dblock merged 1 commit into
codegram:masterfrom
dblock:dblock/allow-header

Conversation

@dblock

@dblock dblock commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

🤖 This PR was written by an AI (Claude) acting on behalf of @dblock.

Closes #4.

Design

A Link usually hasn't fetched its target before _post/_delete, so the client can only honor Allow when it has already seen a response for that same link. This PR uses exactly that knowledge and never sends implicit extra requests.

  • Each Link remembers the Allow header of its most recent response, from any method, including a 405 Method Not Allowed response (RFC 9110 requires it to carry Allow). With the default RaiseError middleware the header is read from the Faraday::ClientError response before re-raising.

  • Read-only accessors, always available: Link#_allow / Link#_allow?(method) and Resource#_allow / Resource#_allow?(method) (the latter from the response that created the resource). _allow returns nil when no Allow header was seen and _allow? then returns true.

  • Enforcement is opt-in with EntryPoint#enforce_allowed_methods (default false):

    api = Hyperclient.new('http://example.org/') do |client|
      client.enforce_allowed_methods = true
    end

    When enabled, if the remembered list excludes the method, the link raises Hyperclient::MethodNotAllowedError (with http_method, url and allowed) instead of contacting the server. Each response replaces what the link knows, so a later response without Allow lifts the restriction. _options is never blocked, so callers can always rediscover the allowed methods. New link objects (e.g. from _expand, _query, or a resource's _links) start without restrictions.

  • Parsing is case-insensitive, splits on commas, strips whitespace, ignores empty entries and duplicates. An empty Allow header means no methods are allowed. HEAD is not implied by GET.

Why opt-in: enforcing by default would change the error existing code sees for a disallowed request from the server's 405 (Faraday::ClientError) to Hyperclient::MethodNotAllowedError, in a minor release. Allow is also a snapshot that can change with authentication or resource state, and enforcement only saves one request the server would refuse anyway. With the default, every request is sent exactly as before.

Not covered: Resource#_post etc. delegate to the resource's self link, which is a separate Link object, so they are not restricted by the response that created the resource; Resource#_allow? is available for that check. I kept enforcement on the link that actually made the request, since the self link's URL may differ from the requested one.

Validation

  • bundle exec rake passes locally on Ruby 4.0.9: 195 runs, 32 Spinach steps, RuboCop clean.
  • Specs in test/hyperclient/link_test.rb, test/hyperclient/resource_test.rb and test/hyperclient/entry_point_test.rb cover parsing edge cases and the accessors; by default, a disallowed method is still sent and a 405 is repeated (these fail if enforcement is unconditional); with enforce_allowed_methods, allowed and disallowed methods, the error's attributes and message, empty Allow, OPTIONS exemption, HEAD not implied by GET, 405 responses with and without RaiseError (asserting the second request is never sent), most-recent-response replacement and per-link scope; and the setting's default and setter.
  • Ran both README examples (default and enforce_allowed_methods = true) against a Faraday test adapter and confirmed the documented output.

🤖 Generated with Claude Code

https://claude.ai/code/session_011Gr21htyJfr8XZGHBcPwkW

@dblock
dblock force-pushed the dblock/allow-header branch from 27500c3 to da94679 Compare November 22, 2026 14:02
@coveralls

coveralls commented Nov 22, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — dblock:dblock/allow-header into codegram:master

@dblock
dblock force-pushed the dblock/allow-header branch 2 times, most recently from 611733a to d2f65dc Compare November 22, 2026 14:40
Remember the Allow header of a link's most recent response, including
405 responses, and expose it with Link#_allow, Link#_allow?,
Resource#_allow and Resource#_allow?.

Add an opt-in EntryPoint#enforce_allowed_methods setting that raises
Hyperclient::MethodNotAllowedError before sending a method the link's
most recent Allow header doesn't list. It defaults to off, so existing
clients keep sending every request and receiving the server's 405.

Closes codegram#4.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011Gr21htyJfr8XZGHBcPwkW
@dblock
dblock force-pushed the dblock/allow-header branch from d2f65dc to 0f6678e Compare November 22, 2026 14:45
@dblock
dblock merged commit ee3a684 into codegram:master Nov 22, 2026
10 checks passed
@dblock
dblock deleted the dblock/allow-header branch November 22, 2026 14:47
@dblock dblock mentioned this pull request Nov 22, 2026
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.

Honour Allow headers

2 participants