Skip to content

Commit

Permalink
Updated App::default_base_url from static var to method
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wilkerson-Barker committed Mar 28, 2024
1 parent 734cf63 commit 4bc23b2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/realm/object-store/c_api/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ static inline bson::BsonArray parse_ejson_array(const char* serialized)

RLM_API const char* realm_app_get_default_base_url(void) noexcept
{
return app::App::default_base_url.data();
return app::App::default_base_url().data();
}

RLM_API realm_app_credentials_t* realm_app_credentials_new_anonymous(bool reuse_credentials) noexcept
Expand Down
13 changes: 8 additions & 5 deletions src/realm/object-store/sync/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ std::mutex s_apps_mutex;
namespace realm {
namespace app {

std::string_view App::default_base_url = "https://services.cloud.mongodb.com";
std::string_view App::default_base_url()
{
return "https://services.cloud.mongodb.com";
}

App::Config::DeviceInfo::DeviceInfo()
: platform(util::get_library_platform())
Expand Down Expand Up @@ -216,7 +219,7 @@ SharedApp App::get_app(CacheMode mode, const Config& config,
{
if (mode == CacheMode::Enabled) {
std::lock_guard<std::mutex> lock(s_apps_mutex);
auto& app = s_apps_cache[config.app_id][config.base_url.value_or(std::string(default_base_url))];
auto& app = s_apps_cache[config.app_id][config.base_url.value_or(std::string(App::default_base_url()))];
if (!app) {
app = std::make_shared<App>(Private(), config);
app->configure(sync_client_config);
Expand Down Expand Up @@ -262,7 +265,7 @@ void App::close_all_sync_sessions()

App::App(Private, const Config& config)
: m_config(config)
, m_base_url(m_config.base_url.value_or(std::string(default_base_url)))
, m_base_url(m_config.base_url.value_or(std::string(App::default_base_url())))
, m_location_updated(false)
, m_request_timeout_ms(m_config.default_request_timeout_ms.value_or(s_default_timeout_ms))
{
Expand Down Expand Up @@ -653,11 +656,11 @@ std::string App::get_base_url() const

void App::update_base_url(std::optional<std::string> base_url, UniqueFunction<void(Optional<AppError>)>&& completion)
{
std::string new_base_url = base_url.value_or(std::string(default_base_url));
std::string new_base_url = base_url.value_or(std::string(App::default_base_url()));

if (new_base_url.empty()) {
// Treat an empty string the same as requesting the default base url
new_base_url = default_base_url;
new_base_url = std::string(App::default_base_url());
log_debug("App::update_base_url: empty => %1", new_base_url);
}
else {
Expand Down
3 changes: 2 additions & 1 deletion src/realm/object-store/sync/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ class App : public std::enable_shared_from_this<App>,
DeviceInfo device_info;
};

static std::string_view default_base_url;
// Returns the default base_url for SDKs to use instead of defining their own
static std::string_view default_base_url();

// `enable_shared_from_this` is unsafe with public constructors;
// use `App::get_app()` instead
Expand Down
6 changes: 3 additions & 3 deletions test/object-store/c_api/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ TEST_CASE("C API (non-database)", "[c_api]") {
CHECK(app_config->app_id == "app_id_123");
CHECK(app_config->transport == transport);

CHECK(realm_app_get_default_base_url() == app::App::default_base_url);
CHECK(realm_app_get_default_base_url() == app::App::default_base_url());

CHECK(!app_config->base_url);
realm_app_config_set_base_url(app_config.get(), base_url.c_str());
Expand Down Expand Up @@ -695,13 +695,13 @@ TEST_CASE("C API (non-database)", "[c_api]") {
check_base_url(base_url);

// Reset to the default base url using nullptr
update_and_check_base_url(nullptr, app::App::default_base_url);
update_and_check_base_url(nullptr, app::App::default_base_url());

// Set to some other base url
update_and_check_base_url(base_url2.c_str(), base_url2);

// Reset to default base url using empty string
update_and_check_base_url("", app::App::default_base_url);
update_and_check_base_url("", app::App::default_base_url());

realm_release(sync_user);
realm_release(token);
Expand Down
34 changes: 17 additions & 17 deletions test/object-store/sync/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3992,21 +3992,21 @@ TEST_CASE("app: base_url", "[sync][app][base_url]") {

SECTION("Test app config baseurl") {
{
redir_transport->reset(App::default_base_url);
redir_transport->reset(App::default_base_url());

// First time through, base_url is empty; https://services.cloud.mongodb.com is expected
auto app = app::App::get_app(app::App::CacheMode::Disabled, app_config, sc_config);
// Location is not requested until first app services request
CHECK(!redir_transport->location_requested);
// Initial hostname and ws hostname use base url, but aren't used until location is updated
CHECK(app->get_host_url() == App::default_base_url);
CHECK(app->get_ws_host_url() == App::create_ws_host_url(App::default_base_url));
CHECK(app->get_host_url() == App::default_base_url());
CHECK(app->get_ws_host_url() == App::create_ws_host_url(App::default_base_url()));

do_login(app);
CHECK(redir_transport->location_requested);
CHECK(app->get_base_url() == App::default_base_url);
CHECK(app->get_host_url() == App::default_base_url);
CHECK(app->get_ws_host_url() == App::create_ws_host_url(App::default_base_url));
CHECK(app->get_base_url() == App::default_base_url());
CHECK(app->get_host_url() == App::default_base_url());
CHECK(app->get_ws_host_url() == App::create_ws_host_url(App::default_base_url()));
}
{
// Second time through, base_url is set to https://alternate.someurl.fake is expected
Expand All @@ -4030,8 +4030,8 @@ TEST_CASE("app: base_url", "[sync][app][base_url]") {
// Third time through, base_url is not set, expect https://services.cloud.mongodb.com,
// since metadata is no longer used
app_config.base_url = util::none;
std::string expected_url = std::string(App::default_base_url);
std::string expected_wsurl = App::create_ws_host_url(App::default_base_url);
std::string expected_url = std::string(App::default_base_url());
std::string expected_wsurl = App::create_ws_host_url(App::default_base_url());
redir_transport->reset(expected_url);

auto app = app::App::get_app(app::App::CacheMode::Disabled, app_config, sc_config);
Expand Down Expand Up @@ -4084,16 +4084,16 @@ TEST_CASE("app: base_url", "[sync][app][base_url]") {
CHECK(app->get_host_url() == "https://alternate.someurl.fake");
CHECK(app->get_ws_host_url() == "wss://alternate.someurl.fake");

redir_transport->reset(App::default_base_url);
redir_transport->reset(App::default_base_url());

// Revert the base URL to the default URL value using std::nullopt
app->update_base_url(std::nullopt, [](util::Optional<app::AppError> error) {
CHECK(!error);
});
CHECK(redir_transport->location_requested);
CHECK(app->get_base_url() == App::default_base_url);
CHECK(app->get_host_url() == App::default_base_url);
CHECK(app->get_ws_host_url() == App::create_ws_host_url(App::default_base_url));
CHECK(app->get_base_url() == App::default_base_url());
CHECK(app->get_host_url() == App::default_base_url());
CHECK(app->get_ws_host_url() == App::create_ws_host_url(App::default_base_url()));
// Expected URL is still App::default_base_url
do_login(app);

Expand All @@ -4108,16 +4108,16 @@ TEST_CASE("app: base_url", "[sync][app][base_url]") {
// Expected URL is still "http://some-other.url.fake"
do_login(app);

redir_transport->reset(App::default_base_url);
redir_transport->reset(App::default_base_url());

// Revert the base URL to the default URL value using the empty string
app->update_base_url("", [](util::Optional<app::AppError> error) {
CHECK(!error);
});
CHECK(redir_transport->location_requested);
CHECK(app->get_base_url() == App::default_base_url);
CHECK(app->get_host_url() == App::default_base_url);
CHECK(app->get_ws_host_url() == App::create_ws_host_url(App::default_base_url));
CHECK(app->get_base_url() == App::default_base_url());
CHECK(app->get_host_url() == App::default_base_url());
CHECK(app->get_ws_host_url() == App::create_ws_host_url(App::default_base_url()));
// Expected URL is still App::default_base_url
do_login(app);
}
Expand Down Expand Up @@ -5806,7 +5806,7 @@ TEST_CASE("app: shared instances", "[sync][app]") {

auto config2 = base_config;
config2.app_id = "app1";
config2.base_url = std::string(App::default_base_url);
config2.base_url = std::string(App::default_base_url());

auto config3 = base_config;
config3.app_id = "app2";
Expand Down

0 comments on commit 4bc23b2

Please sign in to comment.