-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
200 additions
and
7 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
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package client | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
func decorateTransportWithHeadersDecorator(decorated http.RoundTripper, headers map[string]string) (*headersTransportDecorator, error) { | ||
if decorated == nil { | ||
return nil, errors.New("decorated round tripper is nil") | ||
} | ||
|
||
if headers == nil { | ||
return nil, errors.New("headers is nil") | ||
} | ||
|
||
return &headersTransportDecorator{decorated: decorated, headers: headers}, nil | ||
} | ||
|
||
type headersTransportDecorator struct { | ||
decorated http.RoundTripper | ||
headers map[string]string | ||
} | ||
|
||
func (h *headersTransportDecorator) RoundTrip(r *http.Request) (*http.Response, error) { | ||
for key, value := range h.headers { | ||
r.Header.Set(key, value) | ||
} | ||
|
||
return h.decorated.RoundTrip(r) | ||
} |
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,20 @@ | ||
package client | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDecorateTransportHeaderShouldFailWithNilDecorated(t *testing.T) { | ||
transport, err := decorateTransportWithHeadersDecorator(nil, map[string]string{}) | ||
assert.Nil(t, transport) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestDecorateTransportHeaderShouldFailWithNilHeaderMap(t *testing.T) { | ||
transport, err := decorateTransportWithHeadersDecorator(http.DefaultTransport, nil) | ||
assert.Nil(t, transport) | ||
assert.Error(t, err) | ||
} |
File renamed without changes.
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 |
---|---|---|
|
@@ -16,4 +16,5 @@ type Config struct { | |
UserAgent string | ||
UseCookieJar bool | ||
Cookies []*http.Cookie | ||
Headers map[string]string | ||
} |