-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJWTRequestHandlerTests.cs
More file actions
177 lines (146 loc) · 6.34 KB
/
JWTRequestHandlerTests.cs
File metadata and controls
177 lines (146 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Aspose.BarCode.Cloud.Sdk.Api;
using Aspose.BarCode.Cloud.Sdk.Internal;
using Aspose.BarCode.Cloud.Sdk.Internal.RequestHandlers;
using Moq;
using NUnit.Framework;
namespace Aspose.BarCode.Cloud.Sdk.Tests
{
public interface IWebRequestFactory
{
WebRequest Create(string uri);
}
[TestFixture]
public class JWTRequestHandlerTests : TestsBase
{
[SetUp]
public void Init()
{
_requestFactory = RequestFactoryMock();
}
private Mock<IWebRequestFactory> _requestFactory;
[Test]
public void TestTokenFetched()
{
if (TestConfiguration.AuthType != AuthType.JWT)
{
Assert.Ignore($"Unsupported TestConfiguration.AuthType={TestConfiguration.AuthType}");
}
// arrange
JwtRequestHandler jwtHandler = new JwtRequestHandler(TestConfiguration);
jwtHandler.Preparing();
// act
WebRequest request = _requestFactory.Object.Create("http://some url/");
jwtHandler.BeforeSend(request, new MemoryStream());
// assert
Assert.Contains("Authorization", request.Headers.Keys);
string auth = request.Headers["Authorization"];
Assert.Greater(auth.Length, "Bearer ".Length);
string token = auth.Substring("Bearer ".Length);
AssertTokenIsValid(token);
}
[Test]
public void TestTokenRefresh()
{
if (TestConfiguration.AuthType != AuthType.JWT)
{
Assert.Ignore($"Unsupported TestConfiguration.AuthType={TestConfiguration.AuthType}");
}
// arrange
WebRequest unauthorizedRequest = _requestFactory.Object.Create("http://some url/");
unauthorizedRequest.Method = WebRequestMethods.Http.Get;
HttpWebResponse response401 = (HttpWebResponse)unauthorizedRequest.GetResponse();
Assert.AreEqual(HttpStatusCode.Unauthorized, response401.StatusCode);
JwtRequestHandler jwtHandler = new JwtRequestHandler(TestConfiguration);
// act
Assert.Throws<NeedRepeatRequestException>(
() =>
{
// Should manage 401, fetch new token and store it
// And throw NeedRepeatRequestException
jwtHandler.ProcessResponse(response401, new MemoryStream());
});
WebRequest request2 = _requestFactory.Object.Create("http://some url/");
jwtHandler.BeforeSend(request2, new MemoryStream());
Assert.Contains("Authorization", request2.Headers.Keys);
string auth = request2.Headers["Authorization"];
Assert.Greater(auth.Length, "Bearer ".Length);
string token = auth.Substring("Bearer ".Length);
AssertTokenIsValid(token);
}
[Test]
public async Task TestTokenFetchedAsync()
{
if (TestConfiguration.AuthType != AuthType.JWT)
{
Assert.Ignore($"Unsupported TestConfiguration.AuthType={TestConfiguration.AuthType}");
}
// arrange
JwtRequestHandler jwtHandler = new JwtRequestHandler(TestConfiguration);
await jwtHandler.PreparingAsync();
// act
HttpRequestMessage request = new HttpRequestMessage();
await jwtHandler.BeforeSendAsync(request);
// assert
Assert.IsTrue(request.Headers.Contains("Authorization"));
string auth = request.Headers.Authorization.ToString();
Assert.Greater(auth.Length, "Bearer ".Length);
string token = auth.Substring("Bearer ".Length);
AssertTokenIsValid(token);
}
[Test]
public async Task TestTokenRefreshAsync()
{
if (TestConfiguration.AuthType != AuthType.JWT)
{
Assert.Ignore($"Unsupported TestConfiguration.AuthType={TestConfiguration.AuthType}");
}
// arrange
HttpResponseMessage response401 = new HttpResponseMessage(HttpStatusCode.Unauthorized);
JwtRequestHandler jwtHandler = new JwtRequestHandler(TestConfiguration);
// act
Assert.ThrowsAsync<NeedRepeatRequestException>(
async () =>
{
// Should manage 401, fetch new token and store it
// And throw NeedRepeatRequestException
await jwtHandler.ProcessResponseAsync(response401);
});
// arrange
HttpRequestMessage request2 = new HttpRequestMessage();
await jwtHandler.BeforeSendAsync(request2);
// assert
Assert.IsTrue(request2.Headers.Contains("Authorization"));
string auth = request2.Headers.Authorization.ToString();
Assert.Greater(auth.Length, "Bearer ".Length);
string token = auth.Substring("Bearer ".Length);
AssertTokenIsValid(token);
}
private static Mock<IWebRequestFactory> RequestFactoryMock()
{
Mock<HttpWebResponse> responseMock = new Mock<HttpWebResponse>();
responseMock.Setup(c => c.StatusCode).Returns(HttpStatusCode.Unauthorized);
Mock<WebRequest> requestMock = new Mock<WebRequest>();
requestMock.Setup(c => c.GetResponse()).Returns(responseMock.Object);
requestMock.Setup(c => c.Headers).Returns(new WebHeaderCollection());
Mock<IWebRequestFactory> requestFactory = new Mock<IWebRequestFactory>();
requestFactory.Setup(c => c.Create(It.IsAny<string>()))
.Returns(requestMock.Object);
return requestFactory;
}
private static void AssertTokenIsValid(string token)
{
string firstPartBeforeDot = new string(token.TakeWhile(c => c != '.').ToArray());
byte[] tokenBytes = Convert.FromBase64String(firstPartBeforeDot);
JsonElement tokenHeader = JsonDocument.Parse(Encoding.UTF8.GetString(tokenBytes)).RootElement;
Assert.AreEqual("JWT", tokenHeader.GetProperty("typ").GetString());
}
}
}