Skip to content

Commit 75b86e1

Browse files
authored
Merge pull request #76 from firebase/hkj-auth-test
Cleaning up the test utils
2 parents a1ee19c + 2bdd302 commit 75b86e1

File tree

7 files changed

+208
-191
lines changed

7 files changed

+208
-191
lines changed

FirebaseAdmin/FirebaseAdmin.Tests/Auth/FirebaseUserManagerTest.cs

Lines changed: 55 additions & 91 deletions
Large diffs are not rendered by default.

FirebaseAdmin/FirebaseAdmin.Tests/Auth/HttpPublicKeySourceTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public async Task GetPublicKeysWithCaching()
5858
var handler = new MockMessageHandler()
5959
{
6060
Response = File.ReadAllBytes("./resources/public_keys.json"),
61-
ApplyHeaders = (header) => header.CacheControl = cacheControl,
61+
ApplyHeaders = (header, _) => header.CacheControl = cacheControl,
6262
};
6363
var clientFactory = new MockHttpClientFactory(handler);
6464
var keyManager = new HttpPublicKeySource(

FirebaseAdmin/FirebaseAdmin.Tests/Auth/IAMSignerTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public async Task Signer()
5454
byte[] signature = await signer.SignDataAsync(data);
5555
Assert.Equal(bytes, signature);
5656
var req = NewtonsoftJsonSerializer.Instance.Deserialize<IAMSigner.SignBlobRequest>(
57-
handler.Request);
57+
handler.LastRequestBody);
5858
Assert.Equal(Convert.ToBase64String(data), req.BytesToSign);
5959
Assert.Equal(2, handler.Calls);
6060
}
@@ -99,7 +99,7 @@ public async Task Signer()
9999
byte[] signature = await signer.SignDataAsync(data);
100100
Assert.Equal(bytes, signature);
101101
var req = NewtonsoftJsonSerializer.Instance.Deserialize<IAMSigner.SignBlobRequest>(
102-
handler.Request);
102+
handler.LastRequestBody);
103103
Assert.Equal(Convert.ToBase64String(data), req.BytesToSign);
104104
Assert.Equal(1, handler.Calls);
105105
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2019, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System.Net.Http;
16+
using System.Threading;
17+
using System.Threading.Tasks;
18+
19+
namespace FirebaseAdmin.Tests
20+
{
21+
/// <summary>
22+
/// An <see cref="HttpMessageHandler"/> implementation that counts the number of requests
23+
/// processed.
24+
/// </summary>
25+
internal abstract class CountingMessageHandler : HttpMessageHandler
26+
{
27+
private int calls;
28+
29+
public int Calls
30+
{
31+
get => this.calls;
32+
}
33+
34+
protected sealed override Task<HttpResponseMessage> SendAsync(
35+
HttpRequestMessage request, CancellationToken cancellationToken)
36+
{
37+
var count = Interlocked.Increment(ref this.calls);
38+
return this.DoSendAsync(request, count, cancellationToken);
39+
}
40+
41+
protected abstract Task<HttpResponseMessage> DoSendAsync(
42+
HttpRequestMessage request, int count, CancellationToken cancellationToken);
43+
}
44+
}

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/FirebaseMessagingClientTest.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ public async Task SendAsync()
7575
var response = await client.SendAsync(message);
7676

7777
Assert.Equal("test-response", response);
78-
var req = JsonConvert.DeserializeObject<FirebaseMessagingClient.SendRequest>(handler.Request);
78+
var req = JsonConvert.DeserializeObject<FirebaseMessagingClient.SendRequest>(handler.LastRequestBody);
7979
Assert.Equal("test-topic", req.Message.Topic);
8080
Assert.False(req.ValidateOnly);
8181
Assert.Equal(1, handler.Calls);
82-
var versionHeader = handler.RequestHeaders.GetValues("X-Firebase-Client").First();
82+
var versionHeader = handler.LastRequestHeaders.GetValues("X-Firebase-Client").First();
8383
Assert.Equal(FirebaseMessagingClient.ClientVersion, versionHeader);
8484
}
8585

@@ -103,11 +103,11 @@ public async Task SendDryRunAsync()
103103
var response = await client.SendAsync(message, dryRun: true);
104104

105105
Assert.Equal("test-response", response);
106-
var req = JsonConvert.DeserializeObject<FirebaseMessagingClient.SendRequest>(handler.Request);
106+
var req = JsonConvert.DeserializeObject<FirebaseMessagingClient.SendRequest>(handler.LastRequestBody);
107107
Assert.Equal("test-topic", req.Message.Topic);
108108
Assert.True(req.ValidateOnly);
109109
Assert.Equal(1, handler.Calls);
110-
var versionHeader = handler.RequestHeaders.GetValues("X-Firebase-Client").First();
110+
var versionHeader = handler.LastRequestHeaders.GetValues("X-Firebase-Client").First();
111111
Assert.Equal(FirebaseMessagingClient.ClientVersion, versionHeader);
112112
}
113113

@@ -148,7 +148,7 @@ public async Task SendAllAsync()
148148
var handler = new MockMessageHandler()
149149
{
150150
Response = rawResponse,
151-
ApplyContentHeaders = (headers) =>
151+
ApplyHeaders = (_, headers) =>
152152
{
153153
headers.Remove("Content-Type");
154154
headers.TryAddWithoutValidation("Content-Type", "multipart/mixed; boundary=batch_test-boundary");
@@ -172,7 +172,7 @@ public async Task SendAllAsync()
172172
Assert.Equal("projects/fir-adminintegrationtests/messages/5903525881088369386", response.Responses[1].MessageId);
173173
Assert.Equal(1, handler.Calls);
174174
var versionHeader = $"X-Firebase-Client: {FirebaseMessagingClient.ClientVersion}";
175-
Assert.Equal(2, this.CountLinesWithPrefix(handler.Request, versionHeader));
175+
Assert.Equal(2, this.CountLinesWithPrefix(handler.LastRequestBody, versionHeader));
176176
}
177177

178178
[Fact]
@@ -223,7 +223,7 @@ public async Task SendAllAsyncWithError()
223223
var handler = new MockMessageHandler()
224224
{
225225
Response = rawResponse,
226-
ApplyContentHeaders = (headers) =>
226+
ApplyHeaders = (_, headers) =>
227227
{
228228
headers.Remove("Content-Type");
229229
headers.TryAddWithoutValidation("Content-Type", "multipart/mixed; boundary=batch_test-boundary");
@@ -248,7 +248,7 @@ public async Task SendAllAsyncWithError()
248248
Assert.NotNull(response.Responses[1].Exception);
249249
Assert.Equal(1, handler.Calls);
250250
var versionHeader = $"X-Firebase-Client: {FirebaseMessagingClient.ClientVersion}";
251-
Assert.Equal(2, this.CountLinesWithPrefix(handler.Request, versionHeader));
251+
Assert.Equal(2, this.CountLinesWithPrefix(handler.LastRequestBody, versionHeader));
252252
}
253253

254254
[Fact]
@@ -295,7 +295,7 @@ public async Task HttpErrorAsync()
295295
async () => await client.SendAsync(message));
296296
Assert.Contains("not json", ex.Message);
297297
var req = JsonConvert.DeserializeObject<FirebaseMessagingClient.SendRequest>(
298-
handler.Request);
298+
handler.LastRequestBody);
299299
Assert.Equal("test-topic", req.Message.Topic);
300300
Assert.False(req.ValidateOnly);
301301
Assert.Equal(1, handler.Calls);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2019, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System.Net.Http;
16+
using Google.Apis.Http;
17+
18+
namespace FirebaseAdmin.Tests
19+
{
20+
internal sealed class MockHttpClientFactory : HttpClientFactory
21+
{
22+
private readonly HttpMessageHandler handler;
23+
24+
public MockHttpClientFactory(HttpMessageHandler handler)
25+
{
26+
this.handler = handler;
27+
}
28+
29+
protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
30+
{
31+
return this.handler;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)