Skip to content

Commit 8e9308d

Browse files
committed
add url
1 parent aa090be commit 8e9308d

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

pulsaradmin/pkg/admin/brokers_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@
1818
package admin
1919

2020
import (
21+
"encoding/json"
22+
"net/http"
23+
"net/url"
2124
"os"
2225
"testing"
2326

27+
"github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/auth"
2428
"github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config"
29+
"github.com/apache/pulsar-client-go/pulsaradmin/pkg/rest"
2530
"github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils"
2631
"github.com/stretchr/testify/assert"
2732
)
@@ -91,3 +96,53 @@ func TestUpdateDynamicConfiguration(t *testing.T) {
9196
assert.NoError(t, err)
9297
assert.NotEmpty(t, configurations)
9398
}
99+
100+
func TestUpdateDynamicConfigurationWithCustomURL(t *testing.T) {
101+
readFile, err := os.ReadFile("../../../integration-tests/tokens/admin-token")
102+
assert.NoError(t, err)
103+
cfg := &config.Config{
104+
Token: string(readFile),
105+
}
106+
107+
authProvider, err := auth.GetAuthProvider(cfg)
108+
assert.NoError(t, err)
109+
110+
client := rest.Client{
111+
ServiceURL: cfg.WebServiceURL,
112+
VersionInfo: ReleaseVersion,
113+
HTTPClient: &http.Client{
114+
Timeout: DefaultHTTPTimeOutDuration,
115+
Transport: authProvider,
116+
},
117+
}
118+
u, err := url.Parse(cfg.WebServiceURL)
119+
assert.NoError(t, err)
120+
121+
// example config value with '/'
122+
value := `{"key/123":"https://example.com/"}`
123+
encoded := url.QueryEscape(value)
124+
125+
resp, err := client.MakeRequestWithURL(http.MethodPost, &url.URL{
126+
Scheme: u.Scheme,
127+
User: u.User,
128+
Host: u.Host,
129+
Path: "/admin/v2/brokers/configuration/allowAutoSubscriptionCreation/" + value,
130+
RawPath: "/admin/v2/brokers/configuration/allowAutoSubscriptionCreation/" + encoded,
131+
})
132+
assert.NoError(t, err)
133+
assert.Equal(t, http.StatusOK, resp.StatusCode)
134+
135+
// get the config, check if it's updated
136+
admin, err := New(cfg)
137+
assert.NoError(t, err)
138+
assert.NotNil(t, admin)
139+
140+
configurations, err := admin.Brokers().GetAllDynamicConfigurations()
141+
assert.NoError(t, err)
142+
assert.NotEmpty(t, configurations)
143+
144+
var m map[string]interface{}
145+
err = json.Unmarshal([]byte(configurations["allowAutoSubscriptionCreation"]), &m)
146+
assert.NoError(t, err)
147+
assert.Equal(t, "https://example.com/", m["key/123"])
148+
}

pulsaradmin/pkg/rest/client.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@ func (c *Client) newRequest(method, path string) (*request, error) {
5454
return nil, err
5555
}
5656

57+
urlOpt := &url.URL{
58+
Scheme: base.Scheme,
59+
User: base.User,
60+
Host: base.Host,
61+
Path: endpoint(base.Path, u.Path),
62+
}
63+
64+
return c.newRequestWithURL(method, urlOpt)
65+
}
66+
67+
func (c *Client) newRequestWithURL(method string, urlOpt *url.URL) (*request, error) {
5768
req := &request{
5869
method: method,
59-
url: &url.URL{
60-
Scheme: base.Scheme,
61-
User: base.User,
62-
Host: base.Host,
63-
Path: endpoint(base.Path, u.Path),
64-
},
70+
url: urlOpt,
6571
params: make(url.Values),
6672
}
6773
return req, nil
@@ -104,6 +110,20 @@ func (c *Client) MakeRequest(method, endpoint string) (*http.Response, error) {
104110
return resp, nil
105111
}
106112

113+
func (c *Client) MakeRequestWithURL(method string, urlOpt *url.URL) (*http.Response, error) {
114+
req, err := c.newRequestWithURL(method, urlOpt)
115+
if err != nil {
116+
return nil, err
117+
}
118+
119+
resp, err := checkSuccessful(c.doRequest(req))
120+
if err != nil {
121+
return nil, err
122+
}
123+
124+
return resp, nil
125+
}
126+
107127
func (c *Client) Get(endpoint string, obj interface{}) error {
108128
_, err := c.GetWithQueryParams(endpoint, obj, nil, true)
109129
return err

0 commit comments

Comments
 (0)