diff options
author | crupest <crupest@outlook.com> | 2019-02-14 23:05:04 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-02-14 23:05:04 +0800 |
commit | 3c140656ebe6ed34dda9356a01dbff205651e641 (patch) | |
tree | 8b8ca7331c9510b897042737a5cbbc0f77b1b736 /Timeline.Tests/Helpers/Authentication | |
parent | de90f0413553a23f8ebba1343c6e96c63e0c9748 (diff) | |
download | timeline-3c140656ebe6ed34dda9356a01dbff205651e641.tar.gz timeline-3c140656ebe6ed34dda9356a01dbff205651e641.tar.bz2 timeline-3c140656ebe6ed34dda9356a01dbff205651e641.zip |
Develop user token interface.
Diffstat (limited to 'Timeline.Tests/Helpers/Authentication')
-rw-r--r-- | Timeline.Tests/Helpers/Authentication/AuthenticationHttpClientExtensions.cs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationHttpClientExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationHttpClientExtensions.cs new file mode 100644 index 00000000..a4cb8c65 --- /dev/null +++ b/Timeline.Tests/Helpers/Authentication/AuthenticationHttpClientExtensions.cs @@ -0,0 +1,41 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using Timeline.Controllers; +using Xunit; + +namespace Timeline.Tests.Helpers.Authentication +{ + public static class AuthenticationHttpClientExtensions + { + private const string CreateTokenUrl = "/api/User/CreateToken"; + + public static async Task<UserController.CreateTokenResult> CreateUserTokenAsync(this HttpClient client, string username, string password) + { + var response = await client.PostAsJsonAsync(CreateTokenUrl, new UserController.UserCredentials { Username = username, Password = password }); + + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + + var result = JsonConvert.DeserializeObject<UserController.CreateTokenResult>(await response.Content.ReadAsStringAsync()); + + return result; + } + + public static async Task<HttpResponseMessage> SendWithAuthenticationAsync(this HttpClient client, string token, string path, Action<HttpRequestMessage> requestBuilder = null) + { + var request = new HttpRequestMessage + { + RequestUri = new Uri(client.BaseAddress, path), + }; + request.Headers.Add("Authorization", "Bearer " + token); + + requestBuilder?.Invoke(request); + + return await client.SendAsync(request); + } + } +} |