-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support passing custom headers to the provider (#78)
Fixes #73
- Loading branch information
Showing
7 changed files
with
144 additions
and
9 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package transport | ||
|
||
import "net/http" | ||
|
||
// HeadersTransport is a http.RoundTripper that supports adding custom HTTP headers to requests. | ||
type HeadersTransport struct { | ||
// Headers is a set of headers to add to each request. | ||
Headers map[string]string | ||
|
||
// BaseTransport is the underlying HTTP transport to use when making requests. It will default to http.DefaultTransport if nil. | ||
BaseTransport http.RoundTripper | ||
} | ||
|
||
// RoundTrip implements the RoundTripper interface. | ||
func (t *HeadersTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
req = cloneRequest(req) | ||
|
||
for k, v := range t.Headers { | ||
req.Header.Add(k, v) | ||
} | ||
|
||
return t.transport().RoundTrip(req) | ||
} | ||
|
||
// Client returns an *http.Client that makes requests that include additional headers. | ||
func (t *HeadersTransport) Client() *http.Client { | ||
return &http.Client{Transport: t} | ||
} | ||
|
||
// transport returns the underlying HTTP transport. If none is set, http.DefaultTransport is used. | ||
func (t *HeadersTransport) transport() http.RoundTripper { | ||
if t.BaseTransport != nil { | ||
return t.BaseTransport | ||
} | ||
|
||
return http.DefaultTransport | ||
} | ||
|
||
// cloneRequest returns a clone of the provided *http.Request. The clone is a shallow copy of the struct and its headers map. | ||
func cloneRequest(r *http.Request) *http.Request { | ||
r2 := new(http.Request) | ||
*r2 = *r | ||
r2.Header = make(http.Header, len(r.Header)) | ||
|
||
for k, s := range r.Header { | ||
r2.Header[k] = append([]string(nil), s...) | ||
} | ||
|
||
return r2 | ||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package transport | ||
|
||
import ( | ||
"context" | ||
"github.com/h2non/gock" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/tdabasinskas/go-backstage/v2/backstage" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestHeadersTransport_HeadersAdded(t *testing.T) { | ||
const baseURL = "http://localhost:7007" | ||
|
||
headers := map[string]string{ | ||
"test-header-1": "test-value-1", | ||
"test-header-2": "test-value-2", | ||
} | ||
|
||
defer gock.Off() | ||
gock.New(baseURL). | ||
MatchHeaders(headers). | ||
Reply(http.StatusOK) | ||
|
||
client, err := backstage.NewClient(baseURL, "default", &http.Client{ | ||
Transport: &HeadersTransport{ | ||
Headers: headers, | ||
}, | ||
}) | ||
|
||
assert.NoErrorf(t, err, "NewClient should not return an error") | ||
_, _, err = client.Catalog.Entities.List(context.Background(), &backstage.ListEntityOptions{}) | ||
|
||
assert.NoErrorf(t, err, "ListEntities should not return an error") | ||
} |