-
Notifications
You must be signed in to change notification settings - Fork 66
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
support not follow redirect for HTTP client #702
Changes from 6 commits
e6bb77f
ba99d82
e7fa5db
22043dc
db3462a
0b5485a
69407c1
b1fd930
7a69c59
fec2a71
e0c495d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,6 +248,7 @@ func NewClient(deps *module.Dependencies) Client { | |
baseURL, | ||
defaultHeaders, | ||
timeout, | ||
true, | ||
), | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"dependencies": { | ||
"eclint": "^1.1.5", | ||
"uber-licence": "^3.1.1" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,7 @@ func TestReadAndUnmarshalNonStructBody(t *testing.T) { | |
baseURL, | ||
map[string]string{}, | ||
time.Second, | ||
true, | ||
) | ||
ctx := context.Background() | ||
|
||
|
@@ -139,6 +140,7 @@ func TestReadAndUnmarshalNonStructBodyUnmarshalError(t *testing.T) { | |
baseURL, | ||
map[string]string{}, | ||
time.Second, | ||
true, | ||
) | ||
ctx := context.Background() | ||
|
||
|
@@ -177,16 +179,25 @@ func TestUnknownStatusCode(t *testing.T) { | |
|
||
bgateway := gateway.(*benchGateway.BenchGateway) | ||
|
||
addr := bgateway.HTTPBackends()["bar"].RealAddr | ||
baseURL := "http://" + addr | ||
|
||
// test UnknownStatusCode along with follow redirect by default | ||
fakeEcho := func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Add("Location", baseURL+"/bar-path") | ||
w.WriteHeader(303) | ||
_, err := w.Write([]byte(`false`)) | ||
assert.NoError(t, err) | ||
} | ||
|
||
fakeNormal := func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(999) | ||
_, err := w.Write([]byte(`false`)) | ||
assert.NoError(t, err) | ||
} | ||
|
||
bgateway.HTTPBackends()["bar"].HandleFunc("POST", "/bar/echo", fakeEcho) | ||
|
||
addr := bgateway.HTTPBackends()["bar"].RealAddr | ||
baseURL := "http://" + addr | ||
bgateway.HTTPBackends()["bar"].HandleFunc("GET", "/bar-path", fakeNormal) | ||
|
||
client := zanzibar.NewHTTPClientContext( | ||
bgateway.ActualGateway.Logger, | ||
|
@@ -199,6 +210,7 @@ func TestUnknownStatusCode(t *testing.T) { | |
baseURL, | ||
map[string]string{}, | ||
time.Second, | ||
true, | ||
) | ||
|
||
ctx := context.Background() | ||
|
@@ -230,6 +242,79 @@ func TestUnknownStatusCode(t *testing.T) { | |
assert.Len(t, logs["Finished an outgoing client HTTP request"], 1) | ||
} | ||
|
||
func TestNotFollowRedirect(t *testing.T) { | ||
gateway, err := benchGateway.CreateGateway( | ||
defaultTestConfig, | ||
&testGateway.Options{ | ||
LogWhitelist: map[string]bool{ | ||
"Could not emit statusCode metric": true, | ||
}, | ||
KnownHTTPBackends: []string{"bar"}, | ||
ConfigFiles: util.DefaultConfigFiles("example-gateway"), | ||
}, | ||
exampleGateway.CreateGateway, | ||
) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
defer gateway.Close() | ||
|
||
bgateway := gateway.(*benchGateway.BenchGateway) | ||
|
||
addr := bgateway.HTTPBackends()["bar"].RealAddr | ||
baseURL := "http://" + addr | ||
|
||
redirectURI := baseURL + "/bar-path" | ||
fakeEcho := func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Add("Location", redirectURI) | ||
w.WriteHeader(303) | ||
_, err := w.Write([]byte(`false`)) | ||
assert.NoError(t, err) | ||
} | ||
|
||
fakeNormal := func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(999) | ||
_, err := w.Write([]byte(`false`)) | ||
assert.NoError(t, err) | ||
} | ||
|
||
bgateway.HTTPBackends()["bar"].HandleFunc("POST", "/bar/echo", fakeEcho) | ||
bgateway.HTTPBackends()["bar"].HandleFunc("GET", "/bar-path", fakeNormal) | ||
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. this doesn't seem to be used, remove it. 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. make sense, since we don't follow the redirect, we don't need it anymore. |
||
|
||
client := zanzibar.NewHTTPClientContext( | ||
bgateway.ActualGateway.Logger, | ||
bgateway.ActualGateway.ContextMetrics, | ||
jsonwrapper.NewDefaultJSONWrapper(), | ||
"bar", | ||
map[string]string{ | ||
"echo": "bar::echo", | ||
}, | ||
baseURL, | ||
map[string]string{}, | ||
time.Second, | ||
false, | ||
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. @rpatali does this test case address your ask? in this test case, it creates the gateway via exampleGateway and use example gateway's config files. And this test case is to verify the client call doesn't follow the redirect, so the caller will receive 303 status code. 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. this is fine but additional also set the config value for |
||
) | ||
|
||
ctx := context.Background() | ||
|
||
req := zanzibar.NewClientHTTPRequest(ctx, "bar", "echo", "bar::echo", client) | ||
|
||
err = req.WriteJSON("POST", baseURL+"/bar/echo", nil, myJson{}) | ||
assert.NoError(t, err) | ||
|
||
res, err := req.Do() | ||
assert.NoError(t, err) | ||
|
||
var resp string | ||
assert.Error(t, res.ReadAndUnmarshalBody(&resp)) | ||
assert.Equal(t, "", resp) | ||
assert.Equal(t, 303, res.StatusCode) | ||
assert.Equal(t, redirectURI, res.Header["Location"][0]) | ||
|
||
logs := bgateway.AllLogs() | ||
assert.Len(t, logs["Finished an outgoing client HTTP request"], 1) | ||
} | ||
|
||
type myJson struct{} | ||
|
||
func (sj myJson) MarshalJSON() ([]byte, error) { | ||
|
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.
this doesn't seem to be used.