Skip to content

Commit

Permalink
Fix SetRequestCallbacks/SetResponseCallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
averche committed Oct 18, 2023
1 parent fd98162 commit 6d3a86b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
6 changes: 3 additions & 3 deletions client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package vault

import (
"fmt"
"net/http"
"testing"
"time"

Expand All @@ -27,6 +28,12 @@ func Test_Client_Clone(t *testing.T) {

require.NoError(t, client.SetToken("test-token"))
require.NoError(t, client.SetNamespace("test-namespace"))
require.NoError(t, client.SetRequestCallbacks(func(req *http.Request) {
t.Log(req)
}))
require.NoError(t, client.SetResponseCallbacks(func(req *http.Request, resp *http.Response) {
t.Log(req, resp)
}))

clone := client.Clone()

Expand Down
6 changes: 3 additions & 3 deletions generate/templates/client.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ func (c *Client) cloneClientRequestModifiers() requestModifiers {

var clone requestModifiers

copy(clone.requestCallbacks, c.clientRequestModifiers.requestCallbacks)
copy(clone.responseCallbacks, c.clientRequestModifiers.responseCallbacks)
clone.requestCallbacks = append(clone.requestCallbacks, c.clientRequestModifiers.requestCallbacks...)
clone.responseCallbacks = append(clone.responseCallbacks, c.clientRequestModifiers.responseCallbacks...)

clone.headers = requestHeaders{
userAgent: c.clientRequestModifiers.headers.userAgent,
Expand All @@ -192,7 +192,7 @@ func (c *Client) cloneClientRequestModifiers() requestModifiers {
customHeaders: c.clientRequestModifiers.headers.customHeaders.Clone(),
}

copy(clone.headers.mfaCredentials, c.clientRequestModifiers.headers.mfaCredentials)
clone.headers.mfaCredentials = append(clone.headers.mfaCredentials, c.clientRequestModifiers.headers.mfaCredentials...)

return clone
}
Expand Down
2 changes: 1 addition & 1 deletion replication_consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (c *replicationStateCache) clone() replicationStateCache {
/* */ c.statesLock.RLock()
defer c.statesLock.RUnlock()

var cloned []string
cloned := make([]string, len(c.states))
copy(cloned, c.states)

return replicationStateCache{
Expand Down
2 changes: 2 additions & 0 deletions request_modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (c *Client) ClearCustomHeaders() {
// SetRequestCallbacks sets callbacks which will be invoked before each request.
func (c *Client) SetRequestCallbacks(callbacks ...RequestCallback) error {
c.clientRequestModifiersLock.Lock()
c.clientRequestModifiers.requestCallbacks = make([]RequestCallback, len(callbacks))
copy(c.clientRequestModifiers.requestCallbacks, callbacks)
c.clientRequestModifiersLock.Unlock()

Expand All @@ -189,6 +190,7 @@ func (c *Client) ClearRequestCallbacks() {
// successful response.
func (c *Client) SetResponseCallbacks(callbacks ...ResponseCallback) error {
c.clientRequestModifiersLock.Lock()
c.clientRequestModifiers.responseCallbacks = make([]ResponseCallback, len(callbacks))
copy(c.clientRequestModifiers.responseCallbacks, callbacks)
c.clientRequestModifiersLock.Unlock()

Expand Down

0 comments on commit 6d3a86b

Please sign in to comment.