Skip to content

Commit

Permalink
fix set var override
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozs committed Dec 6, 2023
1 parent f063a1d commit 238b424
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 35 deletions.
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ module github.com/iclinic/go-httpc
go 1.20

require github.com/golang/mock v1.6.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand All @@ -23,3 +29,6 @@ golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
54 changes: 19 additions & 35 deletions httpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,12 @@ func (c *HttpClient) Head(addrs string) (*http.Response, error) {
func (c *HttpClient) SetHeader(method, key, value string) {
c.Lock()
defer c.Unlock()
v, ok := c.headers[strings.ToUpper(method)]
if ok {
_, ook := v[key]
if !ook {
v[key] = value
}
} else {
c.headers[strings.ToUpper(method)] = make(map[string]string)
c.headers[strings.ToUpper(method)][key] = value
method = strings.ToUpper(method)

if _, exists := c.headers[method]; !exists {
c.headers[method] = make(map[string]string)
}
c.headers[method][key] = value
}

func (c *HttpClient) DeleteHeader(method, key string) {
Expand All @@ -193,16 +189,12 @@ func (c *HttpClient) DeleteHeader(method, key string) {
func (c *HttpClient) SetFormValue(method, key, value string) {
c.Lock()
defer c.Unlock()
v, ok := c.forms[strings.ToUpper(method)]
if ok {
_, ook := v[key]
if !ook {
v[key] = value
}
} else {
c.forms[strings.ToUpper(method)] = make(map[string]string)
c.forms[strings.ToUpper(method)][key] = value
method = strings.ToUpper(method)

if _, exists := c.forms[method]; !exists {
c.forms[method] = make(map[string]string)
}
c.forms[method][key] = value
}

func (c *HttpClient) DeleteFormValue(method, key string) {
Expand All @@ -220,16 +212,12 @@ func (c *HttpClient) GetFormValue(method string) map[string]string {
func (c *HttpClient) SetBasicAuth(method, username, password string) {
c.Lock()
defer c.Unlock()
v, ok := c.basicAuth[strings.ToUpper(method)]
if ok {
_, ook := v[username]
if !ook {
v[username] = password
}
} else {
c.basicAuth[strings.ToUpper(method)] = make(map[string]string)
c.basicAuth[strings.ToUpper(method)][username] = password
method = strings.ToUpper(method)

if _, exists := c.basicAuth[method]; !exists {
c.basicAuth[method] = make(map[string]string)
}
c.basicAuth[method][username] = password
}

func (c *HttpClient) GetBasicAuth(method string) map[string]string {
Expand All @@ -241,16 +229,12 @@ func (c *HttpClient) GetBasicAuth(method string) map[string]string {
func (c *HttpClient) SetPatchHeader(key, value string) {
c.Lock()
defer c.Unlock()
v, ok := c.headers[http.MethodPatch]
if ok {
_, ook := v[key]
if !ook {
v[key] = value
}
} else {

if _, exists := c.headers[http.MethodPatch]; !exists {
c.headers[http.MethodPatch] = make(map[string]string)
c.headers[http.MethodPatch][key] = value
}

c.headers[http.MethodPatch][key] = value
}

func (c *HttpClient) GetHeaders(method string) map[string]string {
Expand Down
97 changes: 97 additions & 0 deletions httpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/stretchr/testify/assert"
)

func TestHttpClient_Get(t *testing.T) {
Expand Down Expand Up @@ -276,3 +279,97 @@ func TestHttpClient_SetPatchHeader(t *testing.T) {
t.Fatalf("expected header %s to be %s, got %s", key, value, headers[key])
}
}

func TestHttpClient_FormPostValueOverride(t *testing.T) {

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
assert.NoError(t, err)

formValues := url.Values{}
for key, values := range r.PostForm {
for _, value := range values {
formValues.Add(key, value)
}
}
w.Write([]byte(formValues.Encode()))
}))
defer ts.Close()

client := NewHttpClient()
// Set initial form value
client.SetFormValue(http.MethodPost, "key", "initialValue")
// Override form value
client.SetFormValue(http.MethodPost, "key", "updatedValue")

resp, err := client.Post(ts.URL, nil)
assert.NoError(t, err)

responseFormValues, err := url.ParseQuery(string(resp))
assert.NoError(t, err)
assert.Equal(t, "updatedValue", responseFormValues.Get("key"))
}

func TestHttpClient_HeaderValueOverride(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
receivedHeaderValue := r.Header.Get("Test-Header")
w.Write([]byte(receivedHeaderValue))
}))
defer ts.Close()

client := NewHttpClient()
// Set initial header value
client.SetHeader(http.MethodGet, "Test-Header", "initialValue")
// Override header value
client.SetHeader(http.MethodGet, "Test-Header", "updatedValue")

resp, err := client.Get(ts.URL)
assert.NoError(t, err)

assert.Equal(t, "updatedValue", string(resp))
}

func TestHttpClient_BasicAuthOverride(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
w.Write([]byte(user + ":" + pass))
}))
defer ts.Close()

client := NewHttpClient()
// Set initial basic auth credentials
client.SetBasicAuth(http.MethodGet, "initialUser", "initialPass")
// Override basic auth credentials
client.SetBasicAuth(http.MethodGet, "updatedUser", "updatedPass")

resp, err := client.Get(ts.URL)
assert.NoError(t, err)

assert.Equal(t, "updatedUser:updatedPass", string(resp))
}

func TestHttpClient_PatchHeaderOverride(t *testing.T) {
// Create a test server that echoes back the received header value
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
receivedHeaderValue := r.Header.Get("Test-Header")
w.Write([]byte(receivedHeaderValue))
}))
defer ts.Close()

client := NewHttpClient()
// Set initial header value
client.SetPatchHeader("Test-Header", "initialValue")
// Override header value
client.SetPatchHeader("Test-Header", "updatedValue")

// Make a PATCH request
resp, err := client.Patch(ts.URL, nil)
assert.NoError(t, err)

// Check if the header value in the response is the updated value
assert.Equal(t, "updatedValue", string(resp))
}

0 comments on commit 238b424

Please sign in to comment.