-
Notifications
You must be signed in to change notification settings - Fork 8
✨ http
Add support for HTTP client with headers
#623
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1373e6c
:sparkles: `http` Add support for HTTP client with headers
joshjennings98 3980d3e
create headers.HeaderContentType
joshjennings98 a5f1a96
another newsfile
joshjennings98 913649b
add missing headers to call
joshjennings98 8909ad9
mocks and review
joshjennings98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
:sparkles: `http` Add support for HTTP client with headers |
This file contains hidden or 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,138 @@ | ||
package http | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/ARM-software/golang-utils/utils/commonerrors" | ||
"github.com/ARM-software/golang-utils/utils/http/headers" | ||
) | ||
|
||
type ClientWithHeaders struct { | ||
client IClient | ||
headers headers.Headers | ||
} | ||
|
||
func newClientWithHeaders(underlyingClient IClient, headerValues ...string) (c *ClientWithHeaders, err error) { | ||
c = &ClientWithHeaders{ | ||
headers: make(headers.Headers), | ||
} | ||
|
||
if underlyingClient == nil { | ||
c.client = NewPlainHTTPClient() | ||
} else { | ||
c.client = underlyingClient | ||
} | ||
|
||
for header := range slices.Chunk(headerValues, 2) { | ||
if len(header) != 2 { | ||
err = commonerrors.New(commonerrors.ErrInvalid, "headers must be supplied in key-value pairs") | ||
return | ||
} | ||
|
||
c.headers.AppendHeader(header[0], header[1]) | ||
} | ||
|
||
return | ||
} | ||
|
||
func NewHTTPClientWithHeaders(headers ...string) (clientWithHeaders IClientWithHeaders, err error) { | ||
return newClientWithHeaders(nil) | ||
} | ||
|
||
func NewHTTPClientWithEmptyHeaders() (c IClientWithHeaders, err error) { | ||
return NewHTTPClientWithHeaders() | ||
} | ||
|
||
func NewHTTPClientWithUnderlyingClientWithHeaders(underlyingClient IClient, headers ...string) (c IClientWithHeaders, err error) { | ||
return newClientWithHeaders(underlyingClient, headers...) | ||
} | ||
|
||
func NewHTTPClientWithUnderlyingClientWithEmptyHeaders(underlyingClient IClient) (c IClientWithHeaders, err error) { | ||
return newClientWithHeaders(underlyingClient) | ||
} | ||
|
||
func (c *ClientWithHeaders) do(method string, url string, body io.Reader) (*http.Response, error) { | ||
req, err := http.NewRequest(method, url, body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return c.Do(req) | ||
} | ||
|
||
func (c *ClientWithHeaders) Head(url string) (*http.Response, error) { | ||
return c.do(http.MethodHead, url, nil) | ||
} | ||
|
||
func (c *ClientWithHeaders) Post(url, contentType string, rawBody interface{}) (*http.Response, error) { | ||
b, err := determineBodyReader(rawBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req, err := http.NewRequest(http.MethodPost, url, b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Content-Type", contentType) // make sure to overrwrite any in the headers | ||
joshjennings98 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return c.client.Do(req) | ||
} | ||
|
||
func (c *ClientWithHeaders) PostForm(url string, data url.Values) (*http.Response, error) { | ||
rawBody := strings.NewReader(data.Encode()) | ||
return c.Post(url, headers.HeaderXWWWFormURLEncoded, rawBody) | ||
} | ||
|
||
func (c *ClientWithHeaders) StandardClient() *http.Client { | ||
return c.client.StandardClient() | ||
} | ||
|
||
func (c *ClientWithHeaders) Get(url string) (*http.Response, error) { | ||
return c.do(http.MethodGet, url, nil) | ||
} | ||
|
||
func (c *ClientWithHeaders) Do(req *http.Request) (*http.Response, error) { | ||
c.headers.AppendToRequest(req) | ||
return c.client.Do(req) | ||
} | ||
|
||
func (c *ClientWithHeaders) Delete(url string) (*http.Response, error) { | ||
return c.do(http.MethodDelete, url, nil) | ||
} | ||
|
||
func (c *ClientWithHeaders) Put(url string, rawBody interface{}) (*http.Response, error) { | ||
b, err := determineBodyReader(rawBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return c.do(http.MethodPut, url, b) | ||
} | ||
|
||
func (c *ClientWithHeaders) Options(url string) (*http.Response, error) { | ||
return c.do(http.MethodOptions, url, nil) | ||
} | ||
|
||
func (c *ClientWithHeaders) Close() error { | ||
c.client.StandardClient().CloseIdleConnections() | ||
return nil | ||
} | ||
|
||
func (c *ClientWithHeaders) AppendHeader(key, value string) { | ||
if c.headers == nil { | ||
c.headers = make(headers.Headers) | ||
} | ||
c.headers.AppendHeader(key, value) | ||
} | ||
|
||
func (c *ClientWithHeaders) RemoveHeader(key string) { | ||
if c.headers == nil { | ||
return | ||
} | ||
delete(c.headers, key) | ||
} | ||
|
||
func (c *ClientWithHeaders) ClearHeaders() { | ||
c.headers = make(headers.Headers) | ||
} |
This file contains hidden or 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,234 @@ | ||
package http | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/goleak" | ||
|
||
"github.com/ARM-software/golang-utils/utils/http/headers" | ||
"github.com/ARM-software/golang-utils/utils/http/httptest" | ||
) | ||
|
||
func TestClientWithHeadersWithDifferentBodies(t *testing.T) { | ||
clientsToTest := []struct { | ||
clientName string | ||
client func() IClient | ||
}{ | ||
{ | ||
clientName: "default plain client", | ||
client: NewPlainHTTPClient, | ||
}, | ||
{ | ||
clientName: "fast client", | ||
client: NewFastPooledClient, | ||
}, | ||
{ | ||
clientName: "default pooled client", | ||
client: NewDefaultPooledClient, | ||
}, | ||
{ | ||
clientName: "default retryable client", | ||
client: func() IClient { | ||
return NewRetryableClient() | ||
}, | ||
}, | ||
{ | ||
clientName: "client with no retry", | ||
client: func() IClient { | ||
return NewConfigurableRetryableClient(DefaultHTTPClientConfiguration()) | ||
}, | ||
}, | ||
{ | ||
clientName: "client with basic retry", | ||
client: func() IClient { | ||
return NewConfigurableRetryableClient(DefaultRobustHTTPClientConfiguration()) | ||
}, | ||
}, | ||
{ | ||
clientName: "client with exponential backoff", | ||
client: func() IClient { | ||
return NewConfigurableRetryableClient(DefaultRobustHTTPClientConfigurationWithExponentialBackOff()) | ||
}, | ||
}, | ||
{ | ||
clientName: "client with linear backoff", | ||
client: func() IClient { | ||
return NewConfigurableRetryableClient(DefaultRobustHTTPClientConfigurationWithLinearBackOff()) | ||
}, | ||
}, | ||
{ | ||
clientName: "custom oauth client with retry after but no backoff using oauth2.Token (using custom client function with client == nil)", | ||
client: func() IClient { | ||
return NewConfigurableRetryableOauthClientWithLoggerAndCustomClient(DefaultRobustHTTPClientConfigurationWithRetryAfter(), nil, logr.Discard(), "test-token") | ||
}, | ||
}, | ||
{ | ||
clientName: "custom oauth client with retry after but no backoff using oauth2.Token (using custom client function with client == NewPlainHTTPClient())", | ||
client: func() IClient { | ||
return NewConfigurableRetryableOauthClientWithLoggerAndCustomClient(DefaultRobustHTTPClientConfigurationWithRetryAfter(), NewPlainHTTPClient().StandardClient(), logr.Discard(), "test-token") | ||
}, | ||
}, | ||
{ | ||
clientName: "nil", | ||
client: func() IClient { | ||
return nil | ||
}, | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
bodyType string | ||
uri string | ||
headers map[string]string | ||
body interface{} | ||
}{ | ||
{ | ||
bodyType: "nil", | ||
uri: "/foo/bar", | ||
headers: nil, | ||
body: nil, | ||
}, | ||
{ | ||
bodyType: "string", | ||
uri: "/foo/bar", | ||
headers: nil, | ||
body: "some kind of string body", | ||
}, | ||
{ | ||
bodyType: "string reader", | ||
uri: "/foo/bar", | ||
headers: nil, | ||
body: strings.NewReader("some kind of string body"), | ||
}, | ||
{ | ||
bodyType: "bytes", | ||
uri: "/foo/bar", | ||
headers: nil, | ||
body: []byte("some kind of byte body"), | ||
}, | ||
{ | ||
bodyType: "byte buffer", | ||
uri: "/foo/bar", | ||
headers: nil, | ||
body: bytes.NewBuffer([]byte("some kind of byte body")), | ||
}, | ||
{ | ||
bodyType: "byte reader", | ||
uri: "/foo/bar", | ||
headers: nil, | ||
body: bytes.NewReader([]byte("some kind of byte body")), | ||
}, | ||
{ | ||
bodyType: "nil + single Host", | ||
uri: "/foo/bar", | ||
headers: map[string]string{ | ||
headers.HeaderHost: "example.com", | ||
}, | ||
body: nil, | ||
}, | ||
{ | ||
bodyType: "string + WebSocket headers", | ||
uri: "/foo/bar", | ||
headers: map[string]string{ | ||
headers.HeaderConnection: "Upgrade", | ||
headers.HeaderWebsocketVersion: "13", | ||
headers.HeaderWebsocketKey: "dGhlIHNhbXBsZSBub25jZQ==", | ||
headers.HeaderWebsocketProtocol: "chat, superchat", | ||
headers.HeaderWebsocketExtensions: "permessage-deflate; client_max_window_bits", | ||
}, | ||
body: "hello websocket", | ||
}, | ||
{ | ||
bodyType: "bytes + Sunset/Deprecation", | ||
uri: "/foo/bar", | ||
headers: map[string]string{ | ||
headers.HeaderSunset: "2025-12-31T23:59:59Z", | ||
headers.HeaderDeprecation: "Tue, 01 Dec 2026 00:00:00 GMT", | ||
}, | ||
body: []byte("payload with deprecation headers"), | ||
}, | ||
{ | ||
bodyType: "byte buffer + Link", | ||
uri: "/foo/bar", | ||
headers: map[string]string{ | ||
headers.HeaderLink: `<https://api.example.com/page2>; rel="next", <https://api.example.com/help>; rel="help"`, | ||
}, | ||
body: bytes.NewBuffer([]byte("link header test")), | ||
}, | ||
{ | ||
bodyType: "reader + TUS upload headers", | ||
uri: "/foo/bar", | ||
headers: map[string]string{ | ||
headers.HeaderTusVersion: "1.0.0", | ||
headers.HeaderUploadOffset: "1024", | ||
headers.HeaderUploadLength: "2048", | ||
headers.HeaderTusResumable: "1.0.0", | ||
}, | ||
body: strings.NewReader("resumable upload content"), | ||
}, | ||
} | ||
|
||
for i := range tests { | ||
test := tests[i] | ||
for j := range clientsToTest { | ||
defer goleak.VerifyNone(t) | ||
rawClient := clientsToTest[j] | ||
var headersSlice []string | ||
for k, v := range tests[i].headers { | ||
headersSlice = append(headersSlice, k, v) | ||
} | ||
client, err := NewHTTPClientWithUnderlyingClientWithHeaders(rawClient.client(), headersSlice...) | ||
require.NoError(t, err) | ||
defer func() { _ = client.Close() }() | ||
require.NotEmpty(t, client.StandardClient()) | ||
t.Run(fmt.Sprintf("local host/Client %v/Body %v", rawClient.clientName, test.bodyType), func(t *testing.T) { | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
port := "28934" | ||
|
||
// Mock server which always responds 201. | ||
httptest.NewTestServer(t, ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
}), port) | ||
time.Sleep(100 * time.Millisecond) | ||
url := fmt.Sprintf("http://127.0.0.1:%v/%v", port, test.uri) | ||
resp, err := client.Put(url, test.body) | ||
require.NoError(t, err) | ||
_ = resp.Body.Close() | ||
bodyReader, err := determineBodyReader(test.body) | ||
require.NoError(t, err) | ||
req, err := http.NewRequest("POST", url, bodyReader) | ||
require.NoError(t, err) | ||
resp, err = client.Do(req) | ||
require.NoError(t, err) | ||
_ = resp.Body.Close() | ||
cancel() | ||
time.Sleep(100 * time.Millisecond) | ||
}) | ||
clientStruct, ok := client.(*ClientWithHeaders) | ||
require.True(t, ok) | ||
|
||
clientStruct.ClearHeaders() | ||
assert.Empty(t, clientStruct.headers) | ||
|
||
clientStruct.AppendHeader("hello", "world") | ||
require.NotEmpty(t, clientStruct.headers) | ||
assert.Equal(t, headers.Header{Key: "hello", Value: "world"}, clientStruct.headers["hello"]) | ||
|
||
clientStruct.RemoveHeader("hello") | ||
assert.Empty(t, clientStruct.headers) | ||
|
||
_ = client.Close() | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.