Skip to content

Commit 43bdc3c

Browse files
authored
Merge pull request #34 from nixj14/master
Proxy Support
2 parents 1a44ca1 + d9520c1 commit 43bdc3c

File tree

8 files changed

+95
-9
lines changed

8 files changed

+95
-9
lines changed

FirebaseAdmin/FirebaseAdmin.Tests/FirebaseAppTest.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Threading.Tasks;
1717
using FirebaseAdmin;
1818
using Google.Apis.Auth.OAuth2;
19+
using Google.Apis.Http;
1920
using Xunit;
2021

2122
namespace FirebaseAdmin.Tests
@@ -93,6 +94,7 @@ public void CreateAppOptions()
9394
Credential = credential,
9495
ProjectId = "test-project",
9596
ServiceAccountId = "[email protected]",
97+
HttpClientFactory = new MockHttpClientFactory(new MockMessageHandler()),
9698
};
9799
var app = FirebaseApp.Create(options);
98100
Assert.Equal("[DEFAULT]", app.Name);
@@ -102,6 +104,42 @@ public void CreateAppOptions()
102104
Assert.Same(credential, copy.Credential);
103105
Assert.Equal("test-project", copy.ProjectId);
104106
Assert.Equal("[email protected]", copy.ServiceAccountId);
107+
Assert.Same(typeof(MockHttpClientFactory), copy.HttpClientFactory.GetType());
108+
}
109+
110+
[Fact]
111+
public void CreateAppOptionsNoClientFactory()
112+
{
113+
var credential = GoogleCredential.FromAccessToken("token");
114+
var options = new AppOptions()
115+
{
116+
Credential = credential,
117+
ProjectId = "test-project",
118+
ServiceAccountId = "[email protected]",
119+
};
120+
var app = FirebaseApp.Create(options);
121+
Assert.Equal("[DEFAULT]", app.Name);
122+
123+
var copy = app.Options;
124+
Assert.NotSame(options, copy);
125+
Assert.Same(credential, copy.Credential);
126+
Assert.Equal("test-project", copy.ProjectId);
127+
Assert.Equal("[email protected]", copy.ServiceAccountId);
128+
Assert.Same(typeof(HttpClientFactory), copy.HttpClientFactory.GetType());
129+
}
130+
131+
[Fact]
132+
public void NoHttpClientFactory()
133+
{
134+
var credential = GoogleCredential.FromAccessToken("token");
135+
var options = new AppOptions()
136+
{
137+
Credential = credential,
138+
ProjectId = "test-project",
139+
ServiceAccountId = "[email protected]",
140+
HttpClientFactory = null,
141+
};
142+
Assert.Throws<ArgumentNullException>(() => FirebaseApp.Create(options));
105143
}
106144

107145
[Fact]

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/FirebaseMessagingTest.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Threading.Tasks;
1818
using FirebaseAdmin.Tests;
1919
using Google.Apis.Auth.OAuth2;
20+
using Google.Apis.Http;
2021
using Xunit;
2122

2223
namespace FirebaseAdmin.Messaging.Tests
@@ -54,6 +55,28 @@ public void GetMessaging()
5455
Assert.Throws<InvalidOperationException>(() => FirebaseMessaging.GetMessaging(app));
5556
}
5657

58+
[Fact]
59+
public async Task GetMessagingWithClientFactory()
60+
{
61+
var handler = new MockMessageHandler()
62+
{
63+
Response = new FirebaseMessagingClient.SendResponse()
64+
{
65+
Name = "test-response",
66+
},
67+
};
68+
var factory = new MockHttpClientFactory(handler);
69+
70+
var app = FirebaseApp.Create(new AppOptions() { Credential = GoogleCredential.FromAccessToken("test-token"), HttpClientFactory = factory, ProjectId = "test-project" });
71+
FirebaseMessaging messaging = FirebaseMessaging.GetMessaging(app);
72+
Assert.NotNull(messaging);
73+
Assert.Same(messaging, FirebaseMessaging.GetMessaging(app));
74+
75+
var response = await messaging.SendAsync(new Message() { Topic = "test-topic" });
76+
Assert.Equal("test-response", response);
77+
app.Delete();
78+
}
79+
5780
[Fact]
5881
public async Task UseAfterDelete()
5982
{
@@ -67,8 +90,18 @@ await Assert.ThrowsAsync<ObjectDisposedException>(
6790
[Fact]
6891
public async Task SendMessageCancel()
6992
{
70-
var cred = GoogleCredential.FromFile("./resources/service_account.json");
71-
FirebaseApp.Create(new AppOptions() { Credential = cred });
93+
FirebaseApp.Create(new AppOptions() { Credential = MockCredential });
94+
var canceller = new CancellationTokenSource();
95+
canceller.Cancel();
96+
await Assert.ThrowsAsync<OperationCanceledException>(
97+
async () => await FirebaseMessaging.DefaultInstance.SendAsync(
98+
new Message() { Topic = "test-topic" }, canceller.Token));
99+
}
100+
101+
[Fact]
102+
public async Task SendMessageCancelWithClientFactory()
103+
{
104+
FirebaseApp.Create(new AppOptions() { Credential = MockCredential, HttpClientFactory = new HttpClientFactory() });
72105
var canceller = new CancellationTokenSource();
73106
canceller.Cancel();
74107
await Assert.ThrowsAsync<OperationCanceledException>(

FirebaseAdmin/FirebaseAdmin/AppOptions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System;
1616
using System.Collections.Generic;
1717
using Google.Apis.Auth.OAuth2;
18+
using Google.Apis.Http;
1819

1920
namespace FirebaseAdmin
2021
{
@@ -28,13 +29,17 @@ public sealed class AppOptions
2829
/// <summary>
2930
/// Initializes a new instance of the <see cref="AppOptions"/> class.
3031
/// </summary>
31-
public AppOptions() { }
32+
public AppOptions()
33+
{
34+
this.HttpClientFactory = new HttpClientFactory();
35+
}
3236

3337
internal AppOptions(AppOptions options)
3438
{
3539
this.Credential = options.Credential;
3640
this.ProjectId = options.ProjectId;
3741
this.ServiceAccountId = options.ServiceAccountId;
42+
this.HttpClientFactory = options.HttpClientFactory;
3843
}
3944

4045
/// <summary>
@@ -58,5 +63,10 @@ internal AppOptions(AppOptions options)
5863
/// JSON.</para>
5964
/// </summary>
6065
public string ServiceAccountId { get; set; }
66+
67+
/// <summary>
68+
/// Gets or sets the HttpClientFactory to use when making Firebase requests.
69+
/// </summary>
70+
public HttpClientFactory HttpClientFactory { get; set; }
6171
}
6272
}

FirebaseAdmin/FirebaseAdmin/Auth/FirebaseTokenFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ public static FirebaseTokenFactory Create(FirebaseApp app)
8585
{
8686
// If no service account ID is specified, attempt to discover one and invoke the
8787
// IAM service with it.
88-
signer = new IAMSigner(new HttpClientFactory(), app.Options.Credential);
88+
signer = new IAMSigner(app.Options.HttpClientFactory, app.Options.Credential);
8989
}
9090
else
9191
{
9292
// If a service account ID is specified, invoke the IAM service with it.
9393
signer = new FixedAccountIAMSigner(
94-
new HttpClientFactory(), app.Options.Credential, app.Options.ServiceAccountId);
94+
app.Options.HttpClientFactory, app.Options.Credential, app.Options.ServiceAccountId);
9595
}
9696

9797
return new FirebaseTokenFactory(signer, SystemClock.Default);

FirebaseAdmin/FirebaseAdmin/Auth/FirebaseTokenVerifier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ internal static FirebaseTokenVerifier CreateIDTokenVerifier(FirebaseApp app)
8585
}
8686

8787
var keySource = new HttpPublicKeySource(
88-
IdTokenCertUrl, SystemClock.Default, new HttpClientFactory());
88+
IdTokenCertUrl, SystemClock.Default, app.Options.HttpClientFactory);
8989
var args = new FirebaseTokenVerifierArgs()
9090
{
9191
ProjectId = projectId,

FirebaseAdmin/FirebaseAdmin/Auth/FirebaseUserManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019, Google Inc. All rights reserved.
1+
// Copyright 2019, Google Inc. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -53,7 +53,7 @@ public static FirebaseUserManager Create(FirebaseApp app)
5353
{
5454
var args = new FirebaseUserManagerArgs
5555
{
56-
ClientFactory = new HttpClientFactory(),
56+
ClientFactory = app.Options.HttpClientFactory,
5757
Credential = app.Options.Credential,
5858
ProjectId = app.GetProjectId(),
5959
};

FirebaseAdmin/FirebaseAdmin/FirebaseApp.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ private FirebaseApp(AppOptions options, string name)
7979
this.options.Credential = this.options.Credential.CreateScoped(DefaultScopes);
8080
}
8181

82+
if (this.options.HttpClientFactory == null)
83+
{
84+
throw new ArgumentNullException("HttpClientFactory must be set");
85+
}
86+
8287
this.Name = name;
8388
}
8489

FirebaseAdmin/FirebaseAdmin/Messaging/FirebaseMessaging.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public sealed class FirebaseMessaging : IFirebaseService
3131
private FirebaseMessaging(FirebaseApp app)
3232
{
3333
this.messagingClient = new FirebaseMessagingClient(
34-
new HttpClientFactory(), app.Options.Credential, app.GetProjectId());
34+
app.Options.HttpClientFactory, app.Options.Credential, app.GetProjectId());
3535
}
3636

3737
/// <summary>

0 commit comments

Comments
 (0)