aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline.Tests/Helpers
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-11-14 01:58:38 +0800
committercrupest <crupest@outlook.com>2020-11-14 01:58:38 +0800
commit9d287c9bb2dbcd0347d9a9e8e937022de185ec04 (patch)
tree0bab4ac7cc55f1bab71963f677667a58a5faa037 /BackEnd/Timeline.Tests/Helpers
parentbd2462f2bc964839e9b64f167dfa47abb3f9afe6 (diff)
downloadtimeline-9d287c9bb2dbcd0347d9a9e8e937022de185ec04.tar.gz
timeline-9d287c9bb2dbcd0347d9a9e8e937022de185ec04.tar.bz2
timeline-9d287c9bb2dbcd0347d9a9e8e937022de185ec04.zip
...
Diffstat (limited to 'BackEnd/Timeline.Tests/Helpers')
-rw-r--r--BackEnd/Timeline.Tests/Helpers/CacheTestHelper.cs66
-rw-r--r--BackEnd/Timeline.Tests/Helpers/HttpClientExtensions.cs36
-rw-r--r--BackEnd/Timeline.Tests/Helpers/HttpResponseExtensions.cs30
-rw-r--r--BackEnd/Timeline.Tests/Helpers/ResponseAssertions.cs128
4 files changed, 0 insertions, 260 deletions
diff --git a/BackEnd/Timeline.Tests/Helpers/CacheTestHelper.cs b/BackEnd/Timeline.Tests/Helpers/CacheTestHelper.cs
deleted file mode 100644
index ef230cb0..00000000
--- a/BackEnd/Timeline.Tests/Helpers/CacheTestHelper.cs
+++ /dev/null
@@ -1,66 +0,0 @@
-using FluentAssertions;
-using System;
-using System.Net;
-using System.Net.Http;
-using System.Net.Http.Headers;
-using System.Threading.Tasks;
-using Timeline.Models.Http;
-
-namespace Timeline.Tests.Helpers
-{
- public static class CacheTestHelper
- {
- public static async Task TestCache(HttpClient client, string getUrl)
- {
- EntityTagHeaderValue eTag;
- {
- var res = await client.GetAsync(getUrl);
- res.Should().HaveStatusCode(200);
- var cacheControlHeader = res.Headers.CacheControl;
- cacheControlHeader.Should().NotBeNull();
- cacheControlHeader!.NoCache.Should().BeTrue();
- cacheControlHeader.NoStore.Should().BeFalse();
- cacheControlHeader.Private.Should().BeTrue();
- cacheControlHeader.Public.Should().BeFalse();
- cacheControlHeader.MustRevalidate.Should().BeTrue();
- cacheControlHeader.MaxAge.Should().NotBeNull().And.Be(TimeSpan.FromDays(14));
- res.Headers.ETag.Should().NotBeNull();
- eTag = res.Headers.ETag!;
- }
-
- {
- using var request = new HttpRequestMessage()
- {
- RequestUri = new Uri(client.BaseAddress!, getUrl),
- Method = HttpMethod.Get,
- };
- request.Headers.TryAddWithoutValidation("If-None-Match", "\"dsdfd");
- var res = await client.SendAsync(request);
- await res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
- .And.HaveCommonBodyWithCodeAsync(ErrorCodes.Common.Header.IfNonMatch_BadFormat);
- }
-
- {
- using var request = new HttpRequestMessage()
- {
- RequestUri = new Uri(client.BaseAddress!, getUrl),
- Method = HttpMethod.Get,
- };
- request.Headers.TryAddWithoutValidation("If-None-Match", "\"aaa\"");
- var res = await client.SendAsync(request);
- res.Should().HaveStatusCode(HttpStatusCode.OK);
- }
-
- {
- using var request = new HttpRequestMessage()
- {
- RequestUri = new Uri(client.BaseAddress!, getUrl),
- Method = HttpMethod.Get,
- };
- request.Headers.Add("If-None-Match", eTag.ToString());
- var res = await client.SendAsync(request);
- res.Should().HaveStatusCode(HttpStatusCode.NotModified);
- }
- }
- }
-}
diff --git a/BackEnd/Timeline.Tests/Helpers/HttpClientExtensions.cs b/BackEnd/Timeline.Tests/Helpers/HttpClientExtensions.cs
deleted file mode 100644
index 47335a22..00000000
--- a/BackEnd/Timeline.Tests/Helpers/HttpClientExtensions.cs
+++ /dev/null
@@ -1,36 +0,0 @@
-using System;
-using System.Net.Http;
-using System.Net.Http.Headers;
-using System.Net.Mime;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Timeline.Tests.Helpers
-{
- public static class HttpClientExtensions
- {
- public static Task<HttpResponseMessage> PutByteArrayAsync(this HttpClient client, string url, byte[] body, string mimeType)
- {
- return client.PutByteArrayAsync(new Uri(url, UriKind.RelativeOrAbsolute), body, mimeType);
- }
-
- public static Task<HttpResponseMessage> PutByteArrayAsync(this HttpClient client, Uri url, byte[] body, string mimeType)
- {
- using var content = new ByteArrayContent(body);
- content.Headers.ContentLength = body.Length;
- content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);
- return client.PutAsync(url, content);
- }
-
- public static Task<HttpResponseMessage> PutStringAsync(this HttpClient client, string url, string body, string? mimeType = null)
- {
- return client.PutStringAsync(new Uri(url, UriKind.RelativeOrAbsolute), body, mimeType);
- }
-
- public static Task<HttpResponseMessage> PutStringAsync(this HttpClient client, Uri url, string body, string? mimeType = null)
- {
- using var content = new StringContent(body, Encoding.UTF8, mimeType ?? MediaTypeNames.Text.Plain);
- return client.PutAsync(url, content);
- }
- }
-}
diff --git a/BackEnd/Timeline.Tests/Helpers/HttpResponseExtensions.cs b/BackEnd/Timeline.Tests/Helpers/HttpResponseExtensions.cs
deleted file mode 100644
index 01497434..00000000
--- a/BackEnd/Timeline.Tests/Helpers/HttpResponseExtensions.cs
+++ /dev/null
@@ -1,30 +0,0 @@
-using System.Net.Http;
-using System.Text.Json;
-using System.Text.Json.Serialization;
-using System.Threading.Tasks;
-using Timeline.Models.Converters;
-using Timeline.Models.Http;
-
-namespace Timeline.Tests.Helpers
-{
- public static class HttpResponseExtensions
- {
- public static JsonSerializerOptions JsonSerializerOptions { get; }
-
- static HttpResponseExtensions()
- {
- JsonSerializerOptions = new JsonSerializerOptions
- {
- PropertyNamingPolicy = JsonNamingPolicy.CamelCase
- };
- JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
- JsonSerializerOptions.Converters.Add(new JsonDateTimeConverter());
- }
-
- public static async Task<T?> ReadBodyAsJsonAsync<T>(this HttpResponseMessage response)
- {
- var stream = await response.Content.ReadAsStreamAsync();
- return await JsonSerializer.DeserializeAsync<T>(stream, JsonSerializerOptions);
- }
- }
-}
diff --git a/BackEnd/Timeline.Tests/Helpers/ResponseAssertions.cs b/BackEnd/Timeline.Tests/Helpers/ResponseAssertions.cs
deleted file mode 100644
index 95df3eda..00000000
--- a/BackEnd/Timeline.Tests/Helpers/ResponseAssertions.cs
+++ /dev/null
@@ -1,128 +0,0 @@
-using FluentAssertions;
-using FluentAssertions.Execution;
-using FluentAssertions.Formatting;
-using FluentAssertions.Primitives;
-using System;
-using System.Globalization;
-using System.Net;
-using System.Net.Http;
-using System.Net.Http.Json;
-using System.Text;
-using System.Text.Json;
-using System.Text.Json.Serialization;
-using System.Threading.Tasks;
-using Timeline.Models.Converters;
-using Timeline.Models.Http;
-
-namespace Timeline.Tests.Helpers
-{
- public class HttpResponseMessageValueFormatter : IValueFormatter
- {
- public bool CanHandle(object value)
- {
- return value is HttpResponseMessage;
- }
-
- public string Format(object value, FormattingContext context, FormatChild formatChild)
- {
- string newline = context.UseLineBreaks ? Environment.NewLine : "";
- string padding = new string('\t', context.Depth);
-
- var res = (HttpResponseMessage)value;
- return $"{newline}{padding} Status Code: {res.StatusCode}";
- }
- }
-
- public class HttpResponseMessageAssertions
- : ReferenceTypeAssertions<HttpResponseMessage, HttpResponseMessageAssertions>
- {
- static HttpResponseMessageAssertions()
- {
- Formatter.AddFormatter(new HttpResponseMessageValueFormatter());
- }
-
- public HttpResponseMessageAssertions(HttpResponseMessage instance)
- {
- Subject = instance;
- }
-
- protected override string Identifier => "HttpResponseMessage";
-
- public AndConstraint<HttpResponseMessageAssertions> HaveStatusCode(int expected, string because = "", params object[] becauseArgs)
- {
- return HaveStatusCode((HttpStatusCode)expected, because, becauseArgs);
- }
-
- public AndConstraint<HttpResponseMessageAssertions> HaveStatusCode(HttpStatusCode expected, string because = "", params object[] becauseArgs)
- {
- Execute.Assertion.BecauseOf(because, becauseArgs)
- .ForCondition(Subject.StatusCode == expected)
- .FailWith("Expected status code of {context:HttpResponseMessage} to be {0}{reason}, but found {1}.", expected, Subject.StatusCode);
- return new AndConstraint<HttpResponseMessageAssertions>(this);
- }
-
- public async Task<T> HaveAndGetJsonBodyAsync<T>(string because = "", params object[] becauseArgs)
- {
- var a = Execute.Assertion.BecauseOf(because, becauseArgs);
-
- var body = await Subject.ReadBodyAsJsonAsync<T>();
- if (body == null)
- {
- a.FailWith("Expected response body of {context:HttpResponseMessage} to be json string of type {0}{reason}, but failed to read it or it was not a valid json string.", typeof(T).FullName);
- return default!;
- }
- return body;
- }
- }
-
- public static class AssertionResponseExtensions
- {
- public static HttpResponseMessageAssertions Should(this HttpResponseMessage instance)
- {
- return new HttpResponseMessageAssertions(instance);
- }
-
- public static Task<CommonResponse> HaveAndGetCommonBodyAsync(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
- {
- return assertions.HaveAndGetJsonBodyAsync<CommonResponse>(because, becauseArgs);
- }
-
- public static async Task HaveCommonBodyWithCodeAsync(this HttpResponseMessageAssertions assertions, int code, string? message = null, params object[] messageArgs)
- {
- message = string.IsNullOrEmpty(message) ? "" : ", " + string.Format(CultureInfo.CurrentCulture, message, messageArgs);
- var body = await assertions.HaveAndGetCommonBodyAsync("Response body should be CommonResponse{0}", message);
- body.Code.Should().Be(code, "Response body code is not the specified one{0}", message);
- }
-
- public static Task<CommonDataResponse<TData>> HaveAndGetCommonDataBodyAsync<TData>(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
- {
- return assertions.HaveAndGetJsonBodyAsync<CommonDataResponse<TData>>(because, becauseArgs);
- }
-
- public static async Task BePutAsync(this HttpResponseMessageAssertions assertions, bool create, string because = "", params object[] becauseArgs)
- {
- var body = await assertions.HaveStatusCode(create ? 201 : 200, because, becauseArgs)
- .And.HaveAndGetJsonBodyAsync<CommonPutResponse>(because, becauseArgs);
- body.Code.Should().Be(0);
- body.Data.Create.Should().Be(create);
- }
-
- public static async Task BeDeleteAsync(this HttpResponseMessageAssertions assertions, bool delete, string because = "", params object[] becauseArgs)
- {
- var body = await assertions.HaveStatusCode(200, because, becauseArgs)
- .And.HaveAndGetJsonBodyAsync<CommonDeleteResponse>(because, becauseArgs);
- body.Code.Should().Be(0);
- body.Data.Delete.Should().Be(delete);
- }
-
- public static async Task BeInvalidModelAsync(this HttpResponseMessageAssertions assertions, string message = null)
- {
- message = string.IsNullOrEmpty(message) ? "" : ", " + message;
- var body = await assertions.HaveStatusCode(400, "Invalid Model Error must have 400 status code{0}", message)
- .And.HaveAndGetCommonBodyAsync("Invalid Model Error must have CommonResponse body{0}", message);
- body.Code.Should().Be(ErrorCodes.Common.InvalidModel,
- "Invalid Model Error must have code {0} in body{1}",
- ErrorCodes.Common.InvalidModel, message);
- }
- }
-}