diff --git a/pulsaradmin/pkg/admin/brokers_test.go b/pulsaradmin/pkg/admin/brokers_test.go index 5be90f6c34..45c1dce231 100644 --- a/pulsaradmin/pkg/admin/brokers_test.go +++ b/pulsaradmin/pkg/admin/brokers_test.go @@ -18,10 +18,15 @@ package admin import ( + "encoding/json" + "net/http" + "net/url" "os" "testing" + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/auth" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config" + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/rest" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" "github.com/stretchr/testify/assert" ) @@ -91,3 +96,60 @@ func TestUpdateDynamicConfiguration(t *testing.T) { assert.NoError(t, err) assert.NotEmpty(t, configurations) } + +func TestUpdateDynamicConfigurationWithCustomURL(t *testing.T) { + readFile, err := os.ReadFile("../../../integration-tests/tokens/admin-token") + assert.NoError(t, err) + cfg := &config.Config{ + WebServiceURL: DefaultWebServiceURL, + Token: string(readFile), + } + + authProvider, err := auth.GetAuthProvider(cfg) + assert.NoError(t, err) + + client := rest.Client{ + ServiceURL: cfg.WebServiceURL, + VersionInfo: ReleaseVersion, + HTTPClient: &http.Client{ + Timeout: DefaultHTTPTimeOutDuration, + Transport: authProvider, + }, + } + u, err := url.Parse(cfg.WebServiceURL) + assert.NoError(t, err) + + // example config value with '/' + value := `{"key/123":"https://example.com/"}` + encoded := url.QueryEscape(value) + + resp, err := client.MakeRequestWithURL(http.MethodPost, &url.URL{ + Scheme: u.Scheme, + User: u.User, + Host: u.Host, + // use this config to test, will restore it later + Path: "/admin/v2/brokers/configuration/allowAutoSubscriptionCreation/" + value, + RawPath: "/admin/v2/brokers/configuration/allowAutoSubscriptionCreation/" + encoded, + }) + assert.NoError(t, err) + defer resp.Body.Close() + assert.Equal(t, http.StatusOK, resp.StatusCode) + + // get the config, check if it's updated + admin, err := New(cfg) + assert.NoError(t, err) + assert.NotNil(t, admin) + + configurations, err := admin.Brokers().GetAllDynamicConfigurations() + assert.NoError(t, err) + assert.NotEmpty(t, configurations) + + var m map[string]interface{} + err = json.Unmarshal([]byte(configurations["allowAutoSubscriptionCreation"]), &m) + assert.NoError(t, err) + assert.Equal(t, "https://example.com/", m["key/123"]) + + // restore the config + err = admin.Brokers().UpdateDynamicConfiguration("allowAutoSubscriptionCreation", "true") + assert.NoError(t, err) +} diff --git a/pulsaradmin/pkg/rest/client.go b/pulsaradmin/pkg/rest/client.go index e6b603f5f8..9878ec41f4 100644 --- a/pulsaradmin/pkg/rest/client.go +++ b/pulsaradmin/pkg/rest/client.go @@ -104,6 +104,20 @@ func (c *Client) MakeRequest(method, endpoint string) (*http.Response, error) { return resp, nil } +func (c *Client) MakeRequestWithURL(method string, urlOpt *url.URL) (*http.Response, error) { + req := &request{ + method: method, + url: urlOpt, + params: make(url.Values), + } + resp, err := checkSuccessful(c.doRequest(req)) + if err != nil { + return nil, err + } + + return resp, nil +} + func (c *Client) Get(endpoint string, obj interface{}) error { _, err := c.GetWithQueryParams(endpoint, obj, nil, true) return err