-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into eric/ics-compat
- Loading branch information
Showing
8 changed files
with
305 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// RoundTripperWithHeaders is a round tripper that adds headers to the request. | ||
type RoundTripperWithHeaders struct { | ||
// Headers is the map of headers to add to the request. | ||
headers map[string]string | ||
|
||
// Next is the next round tripper in the chain. | ||
next http.RoundTripper | ||
} | ||
|
||
// NewRoundTripperWithHeaders creates a new RoundTripperWithHeaders. | ||
func NewRoundTripperWithHeaders(headers map[string]string, next http.RoundTripper) *RoundTripperWithHeaders { | ||
return &RoundTripperWithHeaders{ | ||
headers: headers, | ||
next: next, | ||
} | ||
} | ||
|
||
// RoundTrip updates the Requests' headers with the headers specified in the constructor, and calls the underlying RoundTripper. | ||
func (r *RoundTripperWithHeaders) RoundTrip(req *http.Request) (*http.Response, error) { | ||
for k, v := range r.headers { | ||
req.Header.Set(k, v) | ||
} | ||
|
||
return r.next.RoundTrip(req) | ||
} |
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,47 @@ | ||
package http_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
slinkyhttp "github.com/skip-mev/slinky/pkg/http" | ||
) | ||
|
||
func TestRoundTripperWithHeaders(t *testing.T) { | ||
expectedHeaderFields := map[string]string{ | ||
"X-Api-Key": "test", | ||
} | ||
|
||
rt := &customRoundTripper{ | ||
expectedHeaderFields: expectedHeaderFields, | ||
} | ||
|
||
rtWithHeaders := slinkyhttp.NewRoundTripperWithHeaders(expectedHeaderFields, rt) | ||
|
||
client := &http.Client{ | ||
Transport: rtWithHeaders, | ||
} | ||
|
||
req, err := http.NewRequest(http.MethodGet, "http://test.com", nil) | ||
require.NoError(t, err) | ||
|
||
// Make the request | ||
_, err = client.Do(req) | ||
require.NoError(t, err) | ||
} | ||
|
||
type customRoundTripper struct { | ||
expectedHeaderFields map[string]string | ||
} | ||
|
||
func (c *customRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
for k, v := range c.expectedHeaderFields { | ||
if req.Header.Get(k) != v { | ||
return nil, fmt.Errorf("expected header %s to be %s, got %s", k, v, req.Header.Get(k)) | ||
} | ||
} | ||
return &http.Response{}, nil | ||
} |
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