This is an automated email from the ASF dual-hosted git repository. guangning pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/pulsar-client-go.git
The following commit(s) were added to refs/heads/master by this push: new 552b541f Fix custom value with `/` (#1229) 552b541f is described below commit 552b541fa7e3de339739a262e63e3d7ee6d554ee Author: Donglai Fu <labulad...@foxmail.com> AuthorDate: Wed Jun 26 14:40:51 2024 +0800 Fix custom value with `/` (#1229) --- pulsaradmin/pkg/admin/brokers_test.go | 62 +++++++++++++++++++++++++++++++++++ pulsaradmin/pkg/rest/client.go | 14 ++++++++ 2 files changed, 76 insertions(+) diff --git a/pulsaradmin/pkg/admin/brokers_test.go b/pulsaradmin/pkg/admin/brokers_test.go index 5be90f6c..45c1dce2 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 e6b603f5..9878ec41 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