Let clients configure a redirect policy - #277
Closed
williammartin wants to merge 1 commit into
Closed
Conversation
NewHTTPClient built its http.Client from the transport alone, so a CheckRedirect supplied by the caller was silently discarded. There was no way to express a redirect policy through this package. That silence has teeth. Go's default policy converts a DELETE into a GET when it follows a 301, so deleting a resource that has since been renamed follows the redirect, issues a GET against the new location, and returns its success status. The caller is told the delete succeeded when nothing was deleted. cli/cli hits exactly this in `gh repo delete`, and works around it today by building its own client and bypassing this package. Add CheckRedirect to ClientOptions and pass it through, mirroring the field of the same name on http.Client. Leaving it nil keeps the existing behaviour of following up to 10 redirects. The test drives a stub transport that answers the first request with a 301. Redirect handling belongs to http.Client rather than the transport, so the stub still exercises the real policy: the client only asks it for the redirected request if the policy allows the redirect. Without the change, the second case sees the DELETE arrive as a GET and reports 204.
Member
Author
|
Folded into #275 so there is a single PR carrying everything go-gh needs for the cli/cli api_host migration. The change itself is unmodified. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
NewHTTPClientbuilds anhttp.ClientfromClientOptionsbut never setsCheckRedirect, so a caller has no way to control redirect handling and alwaysgets Go's default policy.
That default is not always what a caller wants. Go converts a
DELETEinto aGETwhen it follows a 301, so a client deleting a resource that has beenrenamed receives a success response having deleted nothing.
This adds a
CheckRedirectfield toClientOptionsand threads it through, inthe same shape as
http.Client's field of the same name. Behaviour is unchangedwhen it is nil.
Why now
cli/cli is migrating its remaining raw
httpClient.Docall sites onto itsapi.Client, so that all API traffic honours the per-hostapi_hostconfigadded in #275.
gh repo deletewas the one site that could not migrate: itneeds this policy, and because go-gh discarded it, the command had to build its
own client and hold an absolute
api.github.comURL, which meant it silentlyignored
api_host.With this change it migrates, and an end-to-end test that routes every request
through a gateway with
api.github.comblackholed passes in full. That test iswhat surfaced this:
gh repo deletewas the last command still reachingGitHub directly.
Together with #275, this is everything go-gh needs for that work. Draft because
#275 should land first.
Testing
TestNewHTTPClientCheckRedirectcovers both the default policy and a custom onethat stops at the redirect. It uses the package's existing
tripperstub;redirect policy lives on
http.Clientabove the transport, so a stubbedtransport still exercises the real policy.
Verified the test detects the bug: reverting the one-line change to
NewHTTPClientfails it withexpected: 301, actual: 204.