diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-09 15:39:58 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-08-09 15:39:58 +0800 |
commit | 58986da4a5bfe519af44e5834edfbe8d4651b51c (patch) | |
tree | 26432b04bb7b2b2f59b3e8170f2e37cd5ce08c57 /Timeline.Tests/Helpers | |
parent | 6c50630c4d6576446fc2338714feaefbf28c4ed1 (diff) | |
download | timeline-58986da4a5bfe519af44e5834edfbe8d4651b51c.tar.gz timeline-58986da4a5bfe519af44e5834edfbe8d4651b51c.tar.bz2 timeline-58986da4a5bfe519af44e5834edfbe8d4651b51c.zip |
Add UserController unit tests.
Diffstat (limited to 'Timeline.Tests/Helpers')
4 files changed, 71 insertions, 0 deletions
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs index e31bd51c..8a44c852 100644 --- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs +++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs @@ -14,6 +14,7 @@ namespace Timeline.Tests.Helpers.Authentication 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 });
+ response.AssertOk();
var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync());
return result;
}
diff --git a/Timeline.Tests/Helpers/HttpClientExtensions.cs b/Timeline.Tests/Helpers/HttpClientExtensions.cs new file mode 100644 index 00000000..cd40d91e --- /dev/null +++ b/Timeline.Tests/Helpers/HttpClientExtensions.cs @@ -0,0 +1,15 @@ +using Newtonsoft.Json;
+using System.Net.Http;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Timeline.Tests.Helpers
+{
+ public static class HttpClientExtensions
+ {
+ public static Task<HttpResponseMessage> PatchAsJsonAsync<T>(this HttpClient client, string url, T body)
+ {
+ return client.PatchAsync(url, new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"));
+ }
+ }
+}
diff --git a/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs b/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs index 51919021..1c079d0e 100644 --- a/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs +++ b/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs @@ -15,5 +15,13 @@ namespace Timeline.Tests.Helpers var responseBody = await response.ReadBodyAsJson<CommonResponse>();
Assert.Equal(CommonResponse.ErrorCodes.InvalidModel, responseBody.Code);
}
+
+ public static async Task TestPutInvalidModel<T>(HttpClient client, string url, T body)
+ {
+ var response = await client.PutAsJsonAsync(url, body);
+ Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
+ var responseBody = await response.ReadBodyAsJson<CommonResponse>();
+ Assert.Equal(CommonResponse.ErrorCodes.InvalidModel, responseBody.Code);
+ }
}
}
diff --git a/Timeline.Tests/Helpers/ResponseExtensions.cs b/Timeline.Tests/Helpers/ResponseExtensions.cs index 155836fb..46c9e81d 100644 --- a/Timeline.Tests/Helpers/ResponseExtensions.cs +++ b/Timeline.Tests/Helpers/ResponseExtensions.cs @@ -1,11 +1,58 @@ using Newtonsoft.Json;
+using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
+using Timeline.Models.Http;
+using Xunit;
namespace Timeline.Tests.Helpers
{
public static class ResponseExtensions
{
+ public static void AssertOk(this HttpResponseMessage response)
+ {
+ Assert.Equal(HttpStatusCode.OK, response.StatusCode);
+ }
+
+ public static void AssertNotFound(this HttpResponseMessage response)
+ {
+ Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
+ }
+
+ public static void AssertBadRequest(this HttpResponseMessage response)
+ {
+ Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
+ }
+
+ public static async Task AssertIsPutCreated(this HttpResponseMessage response)
+ {
+ Assert.Equal(HttpStatusCode.Created, response.StatusCode);
+ var body = await response.ReadBodyAsJson<CommonResponse>();
+ Assert.Equal(CommonPutResponse.CreatedCode, body.Code);
+ }
+
+ public static async Task AssertIsPutModified(this HttpResponseMessage response)
+ {
+ response.AssertOk();
+ var body = await response.ReadBodyAsJson<CommonResponse>();
+ Assert.Equal(CommonPutResponse.ModifiedCode, body.Code);
+ }
+
+
+ public static async Task AssertIsDeleteDeleted(this HttpResponseMessage response)
+ {
+ response.AssertOk();
+ var body = await response.ReadBodyAsJson<CommonResponse>();
+ Assert.Equal(CommonDeleteResponse.DeletedCode, body.Code);
+ }
+
+ public static async Task AssertIsDeleteNotExist(this HttpResponseMessage response)
+ {
+ response.AssertOk();
+ var body = await response.ReadBodyAsJson<CommonResponse>();
+ Assert.Equal(CommonDeleteResponse.NotExistsCode, body.Code);
+ }
+
public static async Task<T> ReadBodyAsJson<T>(this HttpResponseMessage response)
{
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
|