Skip to content

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 5 commits into from
May 30, 2025
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
1 change: 1 addition & 0 deletions changes/20250530152641.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkles: `http` Add support for HTTP client with headers
1 change: 1 addition & 0 deletions changes/20250530153502.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkles: `http` Add utilities for headers
5 changes: 4 additions & 1 deletion utils/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,7 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

tool github.com/dmarkham/enumer
tool (
github.com/dmarkham/enumer
go.uber.org/mock/mockgen
)
138 changes: 138 additions & 0 deletions utils/http/header_client.go
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, headers...)
}

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(headers.HeaderContentType, contentType) // make sure to overrwrite any in the headers
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)
}
234 changes: 234 additions & 0 deletions utils/http/header_client_test.go
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()
}
}
}
Loading
Loading