Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions pulsaradmin/pkg/admin/brokers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -91,3 +96,55 @@ 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,
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"])
}
14 changes: 14 additions & 0 deletions pulsaradmin/pkg/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down