Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev committed Apr 29, 2024
1 parent 627bae4 commit 9d52c14
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Realm/Realm/Handles/AppHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,13 @@ public Uri GetBaseUri()
return new Uri(uriString);
}

public async Task UpdateBaseUriAsync(Uri newUri)
public async Task UpdateBaseUriAsync(Uri? newUri)
{
var tcs = new TaskCompletionSource();
var tcsHandle = GCHandle.Alloc(tcs);
try
{
var url = newUri.ToString();
var url = newUri?.ToString().TrimEnd('/') ?? string.Empty;
NativeMethods.update_base_uri(this, url, (IntPtr)url.Length, GCHandle.ToIntPtr(tcsHandle), out var ex);
ex.ThrowIfNecessary();
await tcs.Task;
Expand Down
7 changes: 5 additions & 2 deletions Realm/Realm/Sync/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,10 @@ public Task DeleteUserFromServerAsync(User user)
/// Temporarily overrides the <see cref="AppConfiguration.BaseUri"/> value from <see cref="AppConfiguration"/>
/// with a new <paramref name="newUri"/> value used for communicating with the server.
/// </summary>
/// <param name="newUri">The new uri that will be used for communicating with the server.</param>
/// <param name="newUri">
/// The new uri that will be used for communicating with the server. If set to <c>null</c>, the base uri will
/// be reset to its default value.
/// </param>
/// <returns>An awaitable <see cref="Task"/> that represents the asynchronous operation.</returns>
/// <remarks>
/// The App will revert to using the value in [AppConfiguration] when it is restarted.
Expand All @@ -287,7 +290,7 @@ public Task DeleteUserFromServerAsync(User user)
/// This API is experimental and subject to change without a major version increase.
/// </remarks>
[Experimental("Rlm001", UrlFormat = "www.mongodb.com/docs/atlas/app-services/edge-server/connect/#roaming-between-edge-servers")]
public Task UpdateBaseUriAsync(Uri newUri) => Handle.UpdateBaseUriAsync(newUri);
public Task UpdateBaseUriAsync(Uri? newUri) => Handle.UpdateBaseUriAsync(newUri);

/// <inheritdoc />
public override bool Equals(object? obj)
Expand Down
26 changes: 23 additions & 3 deletions Tests/Realm.Tests/Sync/AppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public void RealmConfigurationBaseUrl_ReturnsExpectedValue()
}

[Test]
public void App_ChangeBaseUri_UpdatesBaseUri()
public void App_UpdateBaseUri_UpdatesBaseUri()
{
SyncTestHelpers.RunBaasTestAsync(async () =>
{
Expand All @@ -411,12 +411,32 @@ public void App_ChangeBaseUri_UpdatesBaseUri()
Assert.That(app.BaseUri, Is.EqualTo(new Uri("https://services.mongodb.com")));
#pragma warning disable RLM001
#pragma warning disable Rlm001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
await app.UpdateBaseUriAsync(SyncTestHelpers.BaasUri!);
#pragma warning restore RLM001
#pragma warning restore Rlm001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
Assert.That(app.BaseUri, Is.EqualTo(SyncTestHelpers.BaasUri));
});
}

[Test]
public void App_UpdateBaseUri_WhenUnreachable_Throws()
{
SyncTestHelpers.RunBaasTestAsync(async () =>
{
var appConfig = SyncTestHelpers.GetAppConfig(AppConfigType.FlexibleSync);
appConfig.BaseUri = new Uri("https://services.mongodb.com");
var app = App.Create(appConfig);
Assert.That(app.BaseUri, Is.EqualTo(new Uri("https://services.mongodb.com")));
#pragma warning disable Rlm001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
var ex = await TestHelpers.AssertThrows<AppException>(() => app.UpdateBaseUriAsync(new Uri("https://google.com")));
#pragma warning restore Rlm001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
Assert.That(ex.Message, Does.Contain("404"));
Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
});
}
}
}
19 changes: 19 additions & 0 deletions wrappers/src/app_cs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,25 @@ extern "C" {
});
}

REALM_EXPORT void shared_app_update_base_url(SharedApp& app, uint16_t* url_buf, size_t url_len, void* tcs_ptr, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
std::string url(Utf16StringAccessor(url_buf, url_len));

app->update_base_url(url, [tcs_ptr](util::Optional<AppError> err) {
if (err) {
auto& err_copy = *err;
MarshaledAppError app_error(err_copy);

s_void_callback(tcs_ptr, app_error);
}
else {
s_void_callback(tcs_ptr, MarshaledAppError());
}
});
});
}

#pragma region EmailPassword

REALM_EXPORT void shared_app_email_register_user(SharedApp& app, uint16_t* username_buf, size_t username_len, uint16_t* password_buf, size_t password_len, void* tcs_ptr, NativeException::Marshallable& ex)
Expand Down

0 comments on commit 9d52c14

Please sign in to comment.