public async Task ExponentialBackoff503TooFewRetriesTest() { // Construct new ExponentialBackOffSettings: var exponentialBackOffSettings = new ExponentialBackOffSettings(2, TimeSpan.FromMilliseconds(250), TimeSpan.FromSeconds(30)); // This needs to be a valid Service Account Credentials File. Can't mock it away: var settings = FileBasedFcmClientSettings.CreateFromFile("project", @"D:\serviceAccountKey.json", exponentialBackOffSettings); // Initialize a new FcmHttpClient to send to localhost: var client = new FcmHttpClient(settings); // Construct a Fake Message: var builder = new HttpRequestMessageBuilder("http://localhost:8081/return503_UntilRequestFour", HttpMethod.Get); CancellationToken longLivingCancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(5)).Token; bool fcmHttpExceptionWasThrown = false; try { await client.SendAsync(builder, longLivingCancellationToken); } catch (FcmHttpException) { fcmHttpExceptionWasThrown = true; } Assert.IsTrue(fcmHttpExceptionWasThrown); }
public async Task ExponentialBackoff503Test() { // This needs to be a valid Service Account Credentials File. Can't mock it away: var settings = FileBasedFcmClientSettings.CreateFromFile("project", @"D:\serviceAccountKey.json"); // Initialize a new FcmHttpClient to send to localhost: var client = new FcmHttpClient(settings); // Construct a Fake Message: var builder = new HttpRequestMessageBuilder("http://localhost:8081/return503_UntilRequestFour", HttpMethod.Get); CancellationToken longLivingCancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(5)).Token; await client.SendAsync(builder, longLivingCancellationToken); }
public void SendFcmMessageUsingProxyTest() { // This needs to be a valid Service Account Credentials File. Can't mock it away: var settings = FileBasedFcmClientSettings.CreateFromFile("your_project_id", @"D:\serviceAccountKey.json"); // Define the Proxy URI to be used: var proxy = new Uri("http://localhost:8888"); // Define the Username and Password ("1", because I am using Fiddler for Testing): var credentials = new NetworkCredential("1", "1"); // Build the HTTP Client Factory: var httpClientFactory = new ProxyHttpClientFactory(proxy, credentials); // Initialize a new FcmHttpClient to send to localhost: var fcmHttpClient = new FcmHttpClient(settings, httpClientFactory); // Construct the Firebase Client: using (var client = new FcmClient(settings, fcmHttpClient)) { // Construct the Notification Payload to send: var notification = new Notification { Title = "Title Text", Body = "Notification Body Text" }; // The Message should be sent to the News Topic: var message = new FcmMessage() { ValidateOnly = false, Message = new Message { Topic = "news", Notification = notification } }; // Finally send the Message and wait for the Result: CancellationTokenSource cts = new CancellationTokenSource(); // Send the Message and wait synchronously: var result = client.SendAsync(message, cts.Token).GetAwaiter().GetResult(); Console.WriteLine(result); } }
public void TestHttpClientWithProxy() { // Settings to be used: IFcmClientSettings settings = new FileBasedFcmClientSettings("/Users/bytefish/api.key"); // The Proxy Address: Uri proxyUri = new Uri(string.Format("{0}:{1}", "<proxy_address>", "<proxy_port>")); // Credentials for the Proxy: ICredentials proxyCredentials = new NetworkCredential( "<proxy_username>", "<proxy_password>" ); // Define the Proxy: IWebProxy proxy = new WebProxy { ProxyUri = proxyUri, Credentials = proxyCredentials }; // Now create a client handler with the Proxy settings: HttpClientHandler httpClientHandler = new HttpClientHandler() { Proxy = proxy, PreAuthenticate = true, UseDefaultCredentials = false, }; // Build the Custom FcmHttpClient: FcmHttpClient fcmHttpClient = new FcmHttpClient(settings, new HttpClient(httpClientHandler), JsonSerializer.Default); // Build the HttpClient: using (var client = new FcmClient(settings, fcmHttpClient)) { CancellationTokenSource cts = new CancellationTokenSource(); // Build the message: var message = new TopicUnicastMessage <int>(new FcmMessageOptionsBuilder().Build(), new Topic("a"), 1); // And send the message: var result = client.SendAsync(message, cts.Token).GetAwaiter().GetResult(); } }