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

refactor: introduce a fast path when headers don't need to injected #49

Merged
merged 1 commit into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 18 additions & 7 deletions kong/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,27 @@ type headerRoundTripper struct {

// RoundTrip satisfies the RoundTripper interface.
func (t headerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req = requestWithHeaders(req, t.headers)
return t.rt.RoundTrip(req)
}

func requestWithHeaders(req *http.Request, headers http.Header) *http.Request {
if req == nil {
return nil
}
// fast-path
if len(headers) == 0 {
return req
}
newRequest := new(http.Request)
*newRequest = *req
newRequest.Header = make(http.Header, len(req.Header))
for k, s := range req.Header {
newRequest.Header[k] = append([]string(nil), s...)
}
for k, v := range t.headers {
newRequest.Header[k] = v
newRequest.Header = req.Header.Clone()
for k, values := range headers {
for _, v := range values {
newRequest.Header.Add(k, v)
}
}
return t.rt.RoundTrip(newRequest)
return newRequest
}

// RoundTripperWithHTTPHeaders returns a client which injects headers
Expand Down
74 changes: 74 additions & 0 deletions kong/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kong

import (
"net/http"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -52,3 +54,75 @@ func TestStringSlice(t *testing.T) {
assert.Equal("foo", *arrp[0])
assert.Equal("bar", *arrp[1])
}

func Test_requestWithHeaders(t *testing.T) {
type args struct {
req *http.Request
headers http.Header
}
tests := []struct {
name string
args args
want *http.Request
}{
{
name: "returns request as is if no headers are set",
args: args{
req: &http.Request{
Method: "GET",
Header: http.Header{
"foo": []string{"bar", "baz"},
},
},
headers: http.Header{},
},
want: &http.Request{
Method: "GET",
Header: http.Header{
"foo": []string{"bar", "baz"},
},
},
},
{
name: "returns request with headers added",
args: args{
req: &http.Request{
Method: "GET",
Header: http.Header{
"foo": []string{"bar", "baz"},
},
},
headers: http.Header{
"password": []string{"my-secret-key"},
"key-with": []string{"multiple", "values"},
},
},
want: &http.Request{
Method: "GET",
Header: http.Header{
"foo": []string{"bar", "baz"},
"Password": []string{"my-secret-key"},
"Key-With": []string{"multiple", "values"},
},
},
},
{
name: "returns nil when input request is nil",
args: args{
req: nil,
headers: http.Header{
"password": []string{"my-secret-key"},
"key-with": []string{"multiple", "values"},
},
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := requestWithHeaders(tt.args.req, tt.args.headers); !reflect.DeepEqual(got, tt.want) {
t.Errorf("requestWithHeaders() = %v, want %v", got, tt.want)
}
})
}
}