-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: allow client redirects via 302 responses (#3130) #3131
Open
FergusInLondon
wants to merge
1
commit into
zalando:master
Choose a base branch
from
FergusInLondon:filters/webhook-302-redirects
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,15 +15,17 @@ import ( | |
) | ||
|
||
const headerToCopy = "X-Copy-Header" | ||
const webhookRedirectLocation = "https://example.com/auth" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use: |
||
|
||
func TestWebhook(t *testing.T) { | ||
for _, ti := range []struct { | ||
msg string | ||
token string | ||
expected int | ||
authorized bool | ||
timeout bool | ||
copyHeaders bool | ||
msg string | ||
token string | ||
expected int | ||
authorized bool | ||
timeout bool | ||
copyHeaders bool | ||
expectRedirect bool | ||
}{{ | ||
msg: "invalid-token-should-be-unauthorized", | ||
token: "invalid-token", | ||
|
@@ -47,6 +49,12 @@ func TestWebhook(t *testing.T) { | |
token: testWebhookInvalidScopeToken, | ||
expected: http.StatusForbidden, | ||
authorized: false, | ||
}, { | ||
msg: "auth-redirects-should-be-sent", | ||
token: testWebhookRedirectToken, | ||
expected: http.StatusFound, | ||
expectRedirect: true, | ||
authorized: false, | ||
}} { | ||
t.Run(ti.msg, func(t *testing.T) { | ||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
@@ -78,6 +86,10 @@ func TestWebhook(t *testing.T) { | |
tok := r.Header.Get(authHeaderName) | ||
tok = tok[len(authHeaderPrefix):] | ||
switch tok { | ||
case testWebhookRedirectToken: | ||
w.Header().Set("Location", webhookRedirectLocation) | ||
w.WriteHeader(http.StatusFound) | ||
return | ||
case testToken: | ||
w.WriteHeader(http.StatusOK) | ||
fmt.Fprintln(w, "OK - Got token: "+tok) | ||
|
@@ -125,7 +137,7 @@ func TestWebhook(t *testing.T) { | |
} | ||
req.Header.Set(authHeaderName, authHeaderPrefix+ti.token) | ||
|
||
rsp, err := proxy.Client().Do(req) | ||
rsp, err := proxy.ClientWithoutRedirectFollow().Do(req) | ||
if err != nil { | ||
t.Fatalf("failed to get response: %v", err) | ||
} | ||
|
@@ -141,6 +153,13 @@ func TestWebhook(t *testing.T) { | |
t.Fatalf("unexpected status code: %v != %v %d %s", rsp.StatusCode, ti.expected, n, buf) | ||
} | ||
|
||
// check that the location header is forwarded for a redirect | ||
if ti.expectRedirect { | ||
if loc := rsp.Header.Get("Location"); loc != webhookRedirectLocation { | ||
t.Fatalf("expected webhook location header to be forwarded: %v != %v", loc, webhookRedirectLocation) | ||
} | ||
} | ||
|
||
// check that the header was passed forward to the backend request, if it should have been | ||
if ti.authorized && ti.copyHeaders { | ||
if rsp.Header.Get(headerToCopy) != "test" { | ||
|
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please provide the body of the function and not re-use
redirect
.This will add a bit of code, but reduce the size of change.
reject
should only be used for rejection as 4xx, thanks!