Skip to content

Commit cba2c1b

Browse files
committed
Updated tests
1 parent c1f329d commit cba2c1b

File tree

9 files changed

+307
-70
lines changed

9 files changed

+307
-70
lines changed

src/Microsoft.Graph.Core/Helpers/ContentHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Net.Http;
4-
using System.Text;
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
// ------------------------------------------------------------------------------
54

65
namespace Microsoft.Graph.Core.Helpers
76
{
7+
using System.Net.Http;
88
internal static class ContentHelper
99
{
1010
/// <summary>

src/Microsoft.Graph.Core/Requests/AuthenticationHandler.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
33
// ------------------------------------------------------------------------------
44

5-
using System.Net.Http;
6-
using System.Threading;
7-
using System.Threading.Tasks;
8-
using System.Net;
9-
using Microsoft.Graph.Core.Helpers;
10-
115
namespace Microsoft.Graph
126
{
7+
using System.Net.Http;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using System.Net;
11+
using Microsoft.Graph.Core.Helpers;
1312
/// <summary>
1413
/// An <see cref="DelegatingHandler"/> implementation using standard .NET libraries.
1514
/// </summary>
@@ -33,6 +32,15 @@ public AuthenticationHandler()
3332

3433
}
3534

35+
/// <summary>
36+
/// Construct a new <see cref="AuthenticationHandler"/>
37+
/// <param name="authenticationProvider">An authentication provider to pass to <see cref="AuthenticationHandler"/> for authenticating requests.</param>
38+
/// </summary>
39+
public AuthenticationHandler(IAuthenticationProvider authenticationProvider)
40+
{
41+
AuthenticationProvider = authenticationProvider;
42+
}
43+
3644
/// <summary>
3745
/// Construct a new <see cref="AuthenticationHandler"/>
3846
/// </summary>
@@ -92,11 +100,15 @@ private async Task<HttpResponseMessage> SendRetryAsync(HttpResponseMessage httpR
92100
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken)
93101
{
94102
// Authenticate request using AuthenticationProvider
95-
await AuthenticationProvider.AuthenticateRequestAsync(httpRequest);
103+
if (AuthenticationProvider != null)
104+
{
105+
await AuthenticationProvider.AuthenticateRequestAsync(httpRequest);
106+
}
107+
96108
HttpResponseMessage response = await base.SendAsync(httpRequest, cancellationToken);
97109

98110
// Chcek if response is a 401 & is not a streamed body (is buffered)
99-
if (IsUnauthorized(response) && ContentHelper.IsBuffered(httpRequest))
111+
if (IsUnauthorized(response) && ContentHelper.IsBuffered(httpRequest) && (AuthenticationProvider != null))
100112
{
101113
// re-issue the request to get a new access token
102114
response = await SendRetryAsync(response, cancellationToken);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
// ------------------------------------------------------------------------------
4+
5+
namespace Microsoft.Graph.Core.Test.Helpers
6+
{
7+
using Microsoft.Graph.Core.Helpers;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
using System.Net.Http;
10+
11+
[TestClass]
12+
public class ContentHelperTests
13+
{
14+
[TestMethod]
15+
public void IsBuffered_Get()
16+
{
17+
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, "http://example.com");
18+
var response = ContentHelper.IsBuffered(httpRequest);
19+
20+
Assert.IsTrue(response, "Unexpected content type");
21+
}
22+
[TestMethod]
23+
public void IsBuffered_PostWithNoContent()
24+
{
25+
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, "http://example.com");
26+
var response = ContentHelper.IsBuffered(httpRequest);
27+
28+
Assert.IsTrue(response, "Unexpected content type");
29+
}
30+
[TestMethod]
31+
public void IsBuffered_PostWithBufferStringContent()
32+
{
33+
byte[] data = new byte[] { 1, 2, 3, 4, 5 };
34+
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, "http://example.com");
35+
httpRequest.Content = new ByteArrayContent(data);
36+
var response = ContentHelper.IsBuffered(httpRequest);
37+
38+
Assert.IsTrue(response, "Unexpected content type");
39+
}
40+
41+
[TestMethod]
42+
public void IsBuffered_PutWithStreamStringContent()
43+
{
44+
var stringContent = new StringContent("Hello World");
45+
var byteArrayContent = new ByteArrayContent(new byte[] { 1, 2, 3, 4, 5 });
46+
var mutliformDataContent = new MultipartFormDataContent();
47+
mutliformDataContent.Add(stringContent);
48+
mutliformDataContent.Add(byteArrayContent);
49+
50+
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Put, "http://example.com");
51+
httpRequest.Content = mutliformDataContent;
52+
httpRequest.Content.Headers.ContentLength = -1;
53+
var response = ContentHelper.IsBuffered(httpRequest);
54+
55+
Assert.IsFalse(response, "Unexpected content type");
56+
}
57+
58+
[TestMethod]
59+
public void IsBuffered_PatchWithStreamStringContent()
60+
{
61+
HttpRequestMessage httpRequest = new HttpRequestMessage(new HttpMethod("PATCH"), "http://example.com");
62+
httpRequest.Content = new StringContent("Hello World");
63+
httpRequest.Content.Headers.ContentLength = null;
64+
var response = ContentHelper.IsBuffered(httpRequest);
65+
66+
Assert.IsFalse(response, "Unexpected content type");
67+
}
68+
}
69+
}

tests/Microsoft.Graph.Core.Test/Microsoft.Graph.Core.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<Compile Include="Authentication\DelegateAuthenticationProviderTests.cs" />
8080
<Compile Include="Exceptions\ErrorTests.cs" />
8181
<Compile Include="Exceptions\ServiceExceptionTests.cs" />
82+
<Compile Include="Helpers\ContentHelperTests.cs" />
8283
<Compile Include="Helpers\ExtractSelectHelperTest.cs" />
8384
<Compile Include="Helpers\StringHelperTests.cs" />
8485
<Compile Include="Helpers\UrlHelperTests.cs" />

tests/Microsoft.Graph.Core.Test/Requests/AuthenticationHandlerTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
33
// ------------------------------------------------------------------------------
44

5-
using Microsoft.Graph.Core.Test.Mocks;
6-
using Microsoft.VisualStudio.TestTools.UnitTesting;
7-
using System.Net;
8-
using System.Net.Http;
9-
using System.Threading.Tasks;
10-
using System.Threading;
11-
125
namespace Microsoft.Graph.Core.Test.Requests
136
{
7+
using Microsoft.Graph.Core.Test.Mocks;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
using System.Net;
10+
using System.Net.Http;
11+
using System.Threading.Tasks;
12+
using System.Threading;
13+
1414
[TestClass]
1515
public class AuthenticationHandlerTests
1616
{

tests/Microsoft.Graph.Core.Test/Requests/GraphClientFactoryTests.cs

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Microsoft.Graph.Core.Test.Requests
1919
[TestClass]
2020
public class GraphClientFactoryTests
2121
{
22-
private DelegatingHandler[] handlers = new DelegatingHandler[2];
22+
private DelegatingHandler[] handlers = new DelegatingHandler[3];
2323
private MockRedirectHandler testHttpMessageHandler;
2424

2525

@@ -29,7 +29,7 @@ public void Setup()
2929
this.testHttpMessageHandler = new MockRedirectHandler();
3030
handlers[0] = new RetryHandler();
3131
handlers[1] = new RedirectHandler();
32-
32+
handlers[2] = new AuthenticationHandler();
3333
}
3434

3535
[TestCleanup]
@@ -41,15 +41,18 @@ public void Teardown()
4141
[TestMethod]
4242
public void CreatePipelineWithoutHttpMessageHandlerInput()
4343
{
44-
using (RetryHandler handler = (RetryHandler)GraphClientFactory.CreatePipeline(null, handlers))
45-
using (RedirectHandler inner = (RedirectHandler)handler.InnerHandler)
46-
using (HttpMessageHandler innerMost = inner.InnerHandler)
44+
using (RetryHandler retryHandler = (RetryHandler)GraphClientFactory.CreatePipeline(null, handlers))
45+
using (RedirectHandler redirectHandler = (RedirectHandler)retryHandler.InnerHandler)
46+
using (AuthenticationHandler authenticationHandler = (AuthenticationHandler) redirectHandler.InnerHandler)
47+
using (HttpMessageHandler innerMost = authenticationHandler.InnerHandler)
4748
{
48-
Assert.IsNotNull(handler, "Create a middleware pipeline failed.");
49-
Assert.IsNotNull(inner, "Create a middleware pipeline failed.");
49+
Assert.IsNotNull(retryHandler, "Create a middleware pipeline failed.");
50+
Assert.IsNotNull(redirectHandler, "Create a middleware pipeline failed.");
51+
Assert.IsNotNull(authenticationHandler, "Create a middleware pipeline failed");
5052
Assert.IsNotNull(innerMost, "Create inner most HttpMessageHandler failed.");
51-
Assert.IsInstanceOfType(handler, typeof(RetryHandler), "Pass pipeline failed in first level.");
52-
Assert.IsInstanceOfType(inner, typeof(RedirectHandler), "Pass pipeline failed in seconde level.");
53+
Assert.IsInstanceOfType(retryHandler, typeof(RetryHandler), "Pass pipeline failed in first level.");
54+
Assert.IsInstanceOfType(redirectHandler, typeof(RedirectHandler), "Pass pipeline failed in seconde level.");
55+
Assert.IsInstanceOfType(authenticationHandler, typeof(AuthenticationHandler), "Pass pipeline failed in third level.");
5356
Assert.IsInstanceOfType(innerMost, typeof(HttpMessageHandler), "Inner most HttpMessageHandler class error.");
5457
}
5558

@@ -58,18 +61,20 @@ public void CreatePipelineWithoutHttpMessageHandlerInput()
5861
[TestMethod]
5962
public void CreatePipelineWithHttpMessageHandlerInput()
6063
{
61-
using (RetryHandler handler = (RetryHandler)GraphClientFactory.CreatePipeline(this.testHttpMessageHandler, handlers))
62-
using (RedirectHandler inner = (RedirectHandler)handler.InnerHandler)
63-
using (MockRedirectHandler innerMost = (MockRedirectHandler)inner.InnerHandler)
64+
using (RetryHandler retryHandler = (RetryHandler)GraphClientFactory.CreatePipeline(this.testHttpMessageHandler, handlers))
65+
using (RedirectHandler redirectHandler = (RedirectHandler)retryHandler.InnerHandler)
66+
using (AuthenticationHandler authenticationHandler = (AuthenticationHandler)redirectHandler.InnerHandler)
67+
using (MockRedirectHandler innerMost = (MockRedirectHandler)authenticationHandler.InnerHandler)
6468
{
65-
Assert.IsNotNull(handler, "Create a middleware pipeline failed.");
66-
Assert.IsNotNull(inner, "Create a middleware pipeline failed.");
69+
Assert.IsNotNull(retryHandler, "Create a middleware pipeline failed.");
70+
Assert.IsNotNull(redirectHandler, "Create a middleware pipeline failed.");
71+
Assert.IsNotNull(authenticationHandler, "Create a middleware pipeline failed.");
6772
Assert.IsNotNull(innerMost, "Create inner most HttpMessageHandler failed.");
68-
Assert.IsInstanceOfType(handler, typeof(RetryHandler), "Pass pipeline failed in first level.");
69-
Assert.IsInstanceOfType(inner, typeof(RedirectHandler), "Pass pipeline failed in seconde level.");
73+
Assert.IsInstanceOfType(retryHandler, typeof(RetryHandler), "Pass pipeline failed in first level.");
74+
Assert.IsInstanceOfType(redirectHandler, typeof(RedirectHandler), "Pass pipeline failed in seconde level.");
75+
Assert.IsInstanceOfType(authenticationHandler, typeof(AuthenticationHandler), "Pass pipeline failed in third level.");
7076
Assert.IsInstanceOfType(innerMost, typeof(MockRedirectHandler), "Inner most HttpMessageHandler class error.");
7177
}
72-
7378
}
7479

7580

@@ -98,7 +103,6 @@ public void CreateClient_CustomHttpHandlingBehaviors()
98103
Assert.IsFalse(client.DefaultRequestHeaders.CacheControl.NoCache, "NoCache true.");
99104
Assert.IsFalse(client.DefaultRequestHeaders.CacheControl.NoStore, "NoStore true.");
100105
Assert.AreEqual(client.BaseAddress, baseAddress, "Unexpected default baseAddress set.");
101-
102106
}
103107
}
104108

@@ -204,13 +208,52 @@ public async Task SendRequest_Retry()
204208
Assert.AreEqual(values.Count(), 1);
205209
Assert.AreEqual(values.First(), 1.ToString());
206210
}
211+
}
212+
213+
[TestMethod]
214+
public async Task SendRequest_UnauthorizedWithNoAuthenticationProvider()
215+
{
216+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, "https://example.com/bar");
217+
httpRequestMessage.Content = new StringContent("Hello World");
218+
219+
var unauthorizedResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized);
220+
var okResponse = new HttpResponseMessage(HttpStatusCode.OK);
221+
222+
testHttpMessageHandler.SetHttpResponse(unauthorizedResponse, okResponse);
223+
224+
using (HttpClient client = GraphClientFactory.CreateClient(testHttpMessageHandler, handlers))
225+
{
226+
var response = await client.SendAsync(httpRequestMessage, new CancellationToken());
227+
Assert.AreSame(response, unauthorizedResponse);
228+
Assert.AreSame(response.RequestMessage, httpRequestMessage);
229+
}
230+
}
231+
232+
[TestMethod]
233+
public async Task SendRequest_UnauthorizedWithAuthenticationProvider()
234+
{
235+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, "https://example.com/bar");
236+
httpRequestMessage.Content = new StringContent("Hello World");
207237

238+
var unauthorizedResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized);
239+
var okResponse = new HttpResponseMessage(HttpStatusCode.OK);
240+
241+
testHttpMessageHandler.SetHttpResponse(unauthorizedResponse, okResponse);
242+
243+
handlers[2] = new AuthenticationHandler(new MockAuthenticationProvider().Object);
244+
245+
using (HttpClient client = GraphClientFactory.CreateClient(testHttpMessageHandler, handlers))
246+
{
247+
var response = await client.SendAsync(httpRequestMessage, new CancellationToken());
248+
Assert.AreSame(response, okResponse);
249+
Assert.AreSame(response.RequestMessage, httpRequestMessage);
250+
}
208251
}
209252

210253
[TestMethod]
211254
public void CreateClient_WithHandlersHasExceptions()
212255
{
213-
handlers[1] = null;
256+
handlers[handlers.Length - 1] = null;
214257
try
215258
{
216259
HttpClient client = GraphClientFactory.CreateClient(handlers);
@@ -220,16 +263,18 @@ public void CreateClient_WithHandlersHasExceptions()
220263
Assert.IsInstanceOfType(exception, typeof(ArgumentNullException), "Exception is not the right type");
221264
Assert.AreEqual(exception.ParamName, "handlers", "ParamName not right.");
222265
}
223-
224-
handlers[1] = new RetryHandler(this.testHttpMessageHandler);
266+
handlers[handlers.Length - 1] = new RetryHandler(this.testHttpMessageHandler);
225267
try
226268
{
227269
HttpClient client = GraphClientFactory.CreateClient(handlers);
228270
}
229271
catch (ArgumentException exception)
230272
{
231273
Assert.IsInstanceOfType(exception, typeof(ArgumentException), "Exception is not the right type");
232-
Assert.AreEqual(exception.Message, String.Format("DelegatingHandler array has unexpected InnerHandler. {0} has unexpected InnerHandler.", handlers[1]));
274+
Assert.AreEqual(
275+
exception.Message,
276+
String.Format("DelegatingHandler array has unexpected InnerHandler. {0} has unexpected InnerHandler.",
277+
handlers[handlers.Length - 1]));
233278

234279
}
235280

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
// ------------------------------------------------------------------------------
4+
5+
namespace Microsoft.Graph.DotnetCore.Core.Test.Helpers
6+
{
7+
using Microsoft.Graph.Core.Helpers;
8+
using System.Net.Http;
9+
using Xunit;
10+
public class ContentHelperTests
11+
{
12+
[Fact]
13+
public void IsBuffered_Get()
14+
{
15+
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, "http://example.com");
16+
var response = ContentHelper.IsBuffered(httpRequest);
17+
18+
Assert.True(response, "Unexpected content type");
19+
}
20+
[Fact]
21+
public void IsBuffered_PostWithNoContent()
22+
{
23+
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, "http://example.com");
24+
var response = ContentHelper.IsBuffered(httpRequest);
25+
26+
Assert.True(response, "Unexpected content type");
27+
}
28+
[Fact]
29+
public void IsBuffered_PostWithBufferStringContent()
30+
{
31+
byte[] data = new byte[] { 1, 2, 3, 4, 5 };
32+
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, "http://example.com");
33+
httpRequest.Content = new ByteArrayContent(data);
34+
var response = ContentHelper.IsBuffered(httpRequest);
35+
36+
Assert.True(response, "Unexpected content type");
37+
}
38+
39+
[Fact]
40+
public void IsBuffered_PutWithStreamStringContent()
41+
{
42+
var stringContent = new StringContent("Hello World");
43+
var byteArrayContent = new ByteArrayContent(new byte[] { 1, 2, 3, 4, 5 });
44+
var mutliformDataContent = new MultipartFormDataContent();
45+
mutliformDataContent.Add(stringContent);
46+
mutliformDataContent.Add(byteArrayContent);
47+
48+
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Put, "http://example.com");
49+
httpRequest.Content = mutliformDataContent;
50+
httpRequest.Content.Headers.ContentLength = -1;
51+
var response = ContentHelper.IsBuffered(httpRequest);
52+
53+
Assert.False(response, "Unexpected content type");
54+
}
55+
56+
[Fact]
57+
public void IsBuffered_PatchWithStreamStringContent()
58+
{
59+
HttpRequestMessage httpRequest = new HttpRequestMessage(new HttpMethod("PATCH"), "http://example.com");
60+
httpRequest.Content = new StringContent("Hello World");
61+
httpRequest.Content.Headers.ContentLength = null;
62+
var response = ContentHelper.IsBuffered(httpRequest);
63+
64+
Assert.False(response, "Unexpected content type");
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)