diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-08 16:54:03 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-08-08 16:54:03 +0800 |
commit | d018e1c318646c6d7fecc8d3824129e9f1d5cf6c (patch) | |
tree | da11e395a3fd1340187fb785fc17e570171b942b /Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs | |
parent | dc1ab11cea249f4ca967f86b115147a63f7c93a5 (diff) | |
parent | e283a3e745bad05a55c572646d7b20fbaaeb522d (diff) | |
download | timeline-d018e1c318646c6d7fecc8d3824129e9f1d5cf6c.tar.gz timeline-d018e1c318646c6d7fecc8d3824129e9f1d5cf6c.tar.bz2 timeline-d018e1c318646c6d7fecc8d3824129e9f1d5cf6c.zip |
Merge branch 'master' into null-request-field
Diffstat (limited to 'Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs')
-rw-r--r-- | Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs index 92617588..de88fd35 100644 --- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs +++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs @@ -1,28 +1,28 @@ -using Microsoft.AspNetCore.Mvc.Testing; -using Newtonsoft.Json; -using System.Net.Http; -using System.Threading.Tasks; -using Timeline.Models.Http; - -namespace Timeline.Tests.Helpers.Authentication -{ - public static class AuthenticationExtensions - { - private const string CreateTokenUrl = "/token/create"; - - public static async Task<CreateTokenResponse> CreateUserTokenAsync(this HttpClient client, string username, string password, int? expireOffset = null) - { - var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = username, Password = password, ExpireOffset = expireOffset }); - var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync()); - return result; - } - - public static async Task<HttpClient> CreateClientWithUser<T>(this WebApplicationFactory<T> factory, string username, string password) where T : class - { - var client = factory.CreateDefaultClient(); - var token = (await client.CreateUserTokenAsync(username, password)).Token; - client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); - return client; - } - } -} +using Microsoft.AspNetCore.Mvc.Testing;
+using Newtonsoft.Json;
+using System.Net.Http;
+using System.Threading.Tasks;
+using Timeline.Models.Http;
+
+namespace Timeline.Tests.Helpers.Authentication
+{
+ public static class AuthenticationExtensions
+ {
+ private const string CreateTokenUrl = "/token/create";
+
+ public static async Task<CreateTokenResponse> CreateUserTokenAsync(this HttpClient client, string username, string password, int? expireOffset = null)
+ {
+ var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = username, Password = password, ExpireOffset = expireOffset });
+ var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync());
+ return result;
+ }
+
+ public static async Task<HttpClient> CreateClientWithUser<T>(this WebApplicationFactory<T> factory, string username, string password) where T : class
+ {
+ var client = factory.CreateDefaultClient();
+ var token = (await client.CreateUserTokenAsync(username, password)).Token;
+ client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
+ return client;
+ }
+ }
+}
|