Skip to content
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

Extend TestGateway request creation with header multi-values #737

Merged
merged 3 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.6.4 - 2020-07-27
## 0.6.5 - 2020-08-10
### Added
- Added support for fetching multiple header values. https://github.com/uber/zanzibar/pull/733.
- Set explicit import alias for github.com/uber/zanzibar/runtime in templates. https://github.com/uber/zanzibar/pull/734.
- Added support for multiple header values in TestGateway utility. https://github.com/uber/zanzibar/pull/737.

## 0.6.2 - 2020-07-17
### Added
Expand Down
28 changes: 28 additions & 0 deletions test/lib/bench_gateway/bench_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,34 @@ func (gateway *BenchGateway) MakeRequest(
return client.Do(req)
}

// MakeRequestWithHeaderValues helper
func (gateway *BenchGateway) MakeRequestWithHeaderValues(
method string, url string, headers zanzibar.Header, body io.Reader,
) (*http.Response, error) {
client := gateway.httpClient

fullURL := "http://" + gateway.ActualGateway.RealHTTPAddr + url

req, err := http.NewRequest(method, fullURL, body)

// For each key, fetch every disparate header value and add
// it to the bench gateway request.
keys := headers.Keys()
for _, key := range keys {
if values, found := headers.Values(key); found {
for _, value := range values {
req.Header.Add(key, value)
}
}
}

if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason why this isn't right after where this happens?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed its just how other ones are too but I don't think they need to be (or at least the new ones need to be).

Copy link
Contributor Author

@vermagav vermagav Aug 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I moved them to the correct location in both the old and new functions. Leaving it where it was also had the potential for a nil-pointer dereference with the following block accessing a property on req.

return nil, err
}

return client.Do(req)
}

// MakeTChannelRequest helper
func (gateway *BenchGateway) MakeTChannelRequest(
ctx context.Context,
Expand Down
40 changes: 40 additions & 0 deletions test/lib/test_gateway/test_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ type TestGateway interface {
headers map[string]string,
body io.Reader,
) (*http.Response, error)
// MakeRequestWithHeaderValues is an alternate version of `MakeRequest` that uses `zanzibar.Header`
// instead of a `map[string]string` to represent headers. This allows us to fetch multiple values
// for a given header key.
MakeRequestWithHeaderValues(
method string,
url string,
headers zanzibar.Header,
body io.Reader,
) (*http.Response, error)
MakeTChannelRequest(
ctx context.Context,
thriftService string,
Expand Down Expand Up @@ -344,6 +353,37 @@ func (gateway *ChildProcessGateway) MakeRequest(
return client.Do(req)
}

// MakeRequestWithHeaderValues helper
func (gateway *ChildProcessGateway) MakeRequestWithHeaderValues(
method string,
url string,
headers zanzibar.Header,
body io.Reader,
) (*http.Response, error) {
client := gateway.HTTPClient

fullURL := "http://" + gateway.RealHTTPAddr + url

req, err := http.NewRequest(method, fullURL, body)

// For each key, fetch every disparate header value and add
// it to the test gateway request.
keys := headers.Keys()
for _, key := range keys {
if values, found := headers.Values(key); found {
for _, value := range values {
req.Header.Add(key, value)
}
}
}

if err != nil {
return nil, err
}

return client.Do(req)
}

// MakeTChannelRequest helper
func (gateway *ChildProcessGateway) MakeTChannelRequest(
ctx context.Context,
Expand Down