|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using NUnit.Framework; |
| 4 | +using Realms; |
| 5 | +using Realms.Sync; |
| 6 | +using Realms.Sync.Exceptions; |
| 7 | +using Realms.Sync.Testing; |
| 8 | +using Realms.Logging; |
| 9 | +using System.Threading; |
| 10 | + |
| 11 | +//:snippet-start: experimental-import |
| 12 | +using System.Diagnostics.CodeAnalysis; |
| 13 | +//:snippet-end: |
| 14 | + |
| 15 | +namespace Examples |
| 16 | +{ |
| 17 | + public class BaseURLChange |
| 18 | + { |
| 19 | + |
| 20 | + [Test] |
| 21 | + |
| 22 | + public async Task testEdgeAppWithCustomBaseURL() |
| 23 | + { |
| 24 | + var YOUR_APP_ID = "sync-edge-server-cskhoow"; |
| 25 | + |
| 26 | + // :snippet-start: custom-base-url |
| 27 | + // Specify a base URL to connect to a server other than the default. |
| 28 | + var appConfig = new AppConfiguration(YOUR_APP_ID); |
| 29 | + appConfig.BaseUri = new Uri("http://localhost:80"); |
| 30 | + |
| 31 | + var app = App.Create(appConfig); |
| 32 | + // :snippet-end: |
| 33 | + |
| 34 | + try { |
| 35 | + var user = await app.LogInAsync(Credentials.Anonymous()); |
| 36 | + Assert.AreEqual(UserState.LoggedIn, user.State); |
| 37 | + await user.LogOutAsync(); |
| 38 | + } |
| 39 | + catch (Exception e) { |
| 40 | + Console.WriteLine(e.Message); |
| 41 | + Assert.AreEqual(e.Message, "Could not connect to the server."); |
| 42 | + } |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + [Test] |
| 47 | + |
| 48 | + public async Task testChangeBaseURL() |
| 49 | + { |
| 50 | + var YOUR_APP_ID = "sync-edge-server-cskhoow"; |
| 51 | + |
| 52 | + // :snippet-start: update-base-url |
| 53 | + // Specify a baseURL to connect to a server other than the default. |
| 54 | + // In this case, an Edge Server instance running on the device |
| 55 | + var appConfig = new AppConfiguration(YOUR_APP_ID); |
| 56 | + appConfig.BaseUri = new Uri("http://localhost:80"); |
| 57 | + |
| 58 | + var app = App.Create(appConfig); |
| 59 | + |
| 60 | + // ... log in a user and use the app ... |
| 61 | + |
| 62 | + // Update the base URL back to the default. |
| 63 | + #pragma warning disable Rlm001 // suppress the warning for the experimental method |
| 64 | + |
| 65 | + await app.UpdateBaseUriAsync(new Uri("https://services.cloud.mongodb.com")); |
| 66 | + |
| 67 | + #pragma warning restore Rlm001 |
| 68 | + // :snippet-end: |
| 69 | + |
| 70 | + try { |
| 71 | + var user = await app.LogInAsync(Credentials.Anonymous()); |
| 72 | + Assert.AreEqual(UserState.LoggedIn, user.State); |
| 73 | + |
| 74 | + await user.LogOutAsync(); |
| 75 | + } |
| 76 | + catch (Exception e) { |
| 77 | + Console.WriteLine(e.Message); |
| 78 | + Assert.AreEqual(e.Message, "With a base URL pointing to the cloud, logging in should not fail."); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
0 commit comments