From 636cf3839d92e884987e4e3aec7f23953d02fe37 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 13 Mar 2020 17:58:32 +0800 Subject: Add cache for timeline post data. --- Timeline.Tests/Helpers/CacheTestHelper.cs | 64 ++++++++++++++++++++++++ Timeline.Tests/IntegratedTests/TimelineTest.cs | 4 ++ Timeline.Tests/IntegratedTests/UserAvatarTest.cs | 45 ++--------------- 3 files changed, 71 insertions(+), 42 deletions(-) create mode 100644 Timeline.Tests/Helpers/CacheTestHelper.cs (limited to 'Timeline.Tests') diff --git a/Timeline.Tests/Helpers/CacheTestHelper.cs b/Timeline.Tests/Helpers/CacheTestHelper.cs new file mode 100644 index 00000000..b3709a28 --- /dev/null +++ b/Timeline.Tests/Helpers/CacheTestHelper.cs @@ -0,0 +1,64 @@ +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.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)); + 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); + res.Should().HaveStatusCode(HttpStatusCode.BadRequest) + .And.HaveCommonBody(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/Timeline.Tests/IntegratedTests/TimelineTest.cs b/Timeline.Tests/IntegratedTests/TimelineTest.cs index 682cfd7c..845208e8 100644 --- a/Timeline.Tests/IntegratedTests/TimelineTest.cs +++ b/Timeline.Tests/IntegratedTests/TimelineTest.cs @@ -1090,6 +1090,10 @@ namespace Timeline.Tests.IntegratedTests format.Name.Should().Be(PngFormat.Instance.Name); } + { + await CacheTestHelper.TestCache(client, generator(1, $"posts/{postId}/data")); + } + { var res = await client.DeleteAsync(generator(1, $"posts/{postId}")); res.Should().BeDelete(true); diff --git a/Timeline.Tests/IntegratedTests/UserAvatarTest.cs b/Timeline.Tests/IntegratedTests/UserAvatarTest.cs index fa0120f1..fbf34490 100644 --- a/Timeline.Tests/IntegratedTests/UserAvatarTest.cs +++ b/Timeline.Tests/IntegratedTests/UserAvatarTest.cs @@ -6,7 +6,6 @@ using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Gif; using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Png; -using System; using System.Collections.Generic; using System.IO; using System.Net; @@ -58,55 +57,17 @@ namespace Timeline.Tests.IntegratedTests body.Should().Equal(defaultAvatarData); } - EntityTagHeaderValue eTag; { - var res = await client.GetAsync($"users/user1/avatar"); + var res = await client.GetAsync("users/user1/avatar"); res.Should().HaveStatusCode(200); res.Content.Headers.ContentType.MediaType.Should().Be("image/png"); var body = await res.Content.ReadAsByteArrayAsync(); body.Should().Equal(defaultAvatarData); - var cacheControl = res.Headers.CacheControl; - cacheControl.NoCache.Should().BeTrue(); - cacheControl.NoStore.Should().BeFalse(); - cacheControl.MaxAge.Should().NotBeNull().And.Be(TimeSpan.Zero); - eTag = res.Headers.ETag; } - await GetReturnDefault("admin"); - - { - using var request = new HttpRequestMessage() - { - RequestUri = new Uri(client.BaseAddress, "users/user1/avatar"), - Method = HttpMethod.Get, - }; - request.Headers.TryAddWithoutValidation("If-None-Match", "\"dsdfd"); - var res = await client.SendAsync(request); - res.Should().HaveStatusCode(HttpStatusCode.BadRequest) - .And.HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Common.Header.IfNonMatch_BadFormat); - } - - { - using var request = new HttpRequestMessage() - { - RequestUri = new Uri(client.BaseAddress, "users/user1/avatar"), - Method = HttpMethod.Get, - }; - request.Headers.TryAddWithoutValidation("If-None-Match", "\"aaa\""); - var res = await client.SendAsync(request); - res.Should().HaveStatusCode(HttpStatusCode.OK); - } + await CacheTestHelper.TestCache(client, "users/user1/avatar"); - { - using var request = new HttpRequestMessage() - { - RequestUri = new Uri(client.BaseAddress, "users/user1/avatar"), - Method = HttpMethod.Get, - }; - request.Headers.Add("If-None-Match", eTag.ToString()); - var res = await client.SendAsync(request); - res.Should().HaveStatusCode(HttpStatusCode.NotModified); - } + await GetReturnDefault("admin"); { using var content = new ByteArrayContent(new[] { (byte)0x00 }); -- cgit v1.2.3 From 5edffd0242a16e9866a1f4e9f1e1d2ff4e549d2c Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 13 Mar 2020 18:03:29 +0800 Subject: Clean code. --- Timeline.Tests/PasswordGenerator.cs | 3 +-- Timeline/Controllers/TimelineController.cs | 1 - Timeline/Controllers/UserAvatarController.cs | 2 -- Timeline/Helpers/Log.cs | 1 - Timeline/Models/Http/UserController.cs | 1 - Timeline/Models/Validation/Validator.cs | 5 +---- Timeline/Services/Clock.cs | 3 --- Timeline/Services/PathProvider.cs | 4 ---- Timeline/Startup.cs | 2 +- 9 files changed, 3 insertions(+), 19 deletions(-) (limited to 'Timeline.Tests') diff --git a/Timeline.Tests/PasswordGenerator.cs b/Timeline.Tests/PasswordGenerator.cs index 6c07836b..863439b5 100644 --- a/Timeline.Tests/PasswordGenerator.cs +++ b/Timeline.Tests/PasswordGenerator.cs @@ -1,5 +1,4 @@ -using System; -using Timeline.Services; +using Timeline.Services; using Xunit; using Xunit.Abstractions; diff --git a/Timeline/Controllers/TimelineController.cs b/Timeline/Controllers/TimelineController.cs index 8bc0345f..f1781ff3 100644 --- a/Timeline/Controllers/TimelineController.cs +++ b/Timeline/Controllers/TimelineController.cs @@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using Microsoft.Net.Http.Headers; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; diff --git a/Timeline/Controllers/UserAvatarController.cs b/Timeline/Controllers/UserAvatarController.cs index b5f4be1e..4062837b 100644 --- a/Timeline/Controllers/UserAvatarController.cs +++ b/Timeline/Controllers/UserAvatarController.cs @@ -2,9 +2,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using Microsoft.Net.Http.Headers; using System; -using System.Linq; using System.Threading.Tasks; using Timeline.Auth; using Timeline.Filters; diff --git a/Timeline/Helpers/Log.cs b/Timeline/Helpers/Log.cs index 68c975fa..af0b7e13 100644 --- a/Timeline/Helpers/Log.cs +++ b/Timeline/Helpers/Log.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Text; namespace Timeline.Helpers diff --git a/Timeline/Models/Http/UserController.cs b/Timeline/Models/Http/UserController.cs index e4c95cbd..5ee02a95 100644 --- a/Timeline/Models/Http/UserController.cs +++ b/Timeline/Models/Http/UserController.cs @@ -1,7 +1,6 @@ using AutoMapper; using System.ComponentModel.DataAnnotations; using Timeline.Models.Validation; -using Timeline.Services; namespace Timeline.Models.Http { diff --git a/Timeline/Models/Validation/Validator.cs b/Timeline/Models/Validation/Validator.cs index ead7dbef..db139448 100644 --- a/Timeline/Models/Validation/Validator.cs +++ b/Timeline/Models/Validation/Validator.cs @@ -1,8 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Localization; -using System; +using System; using System.ComponentModel.DataAnnotations; -using Timeline.Helpers; using static Timeline.Resources.Models.Validation.Validator; namespace Timeline.Models.Validation diff --git a/Timeline/Services/Clock.cs b/Timeline/Services/Clock.cs index 0499c0c6..040f9304 100644 --- a/Timeline/Services/Clock.cs +++ b/Timeline/Services/Clock.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace Timeline.Services { diff --git a/Timeline/Services/PathProvider.cs b/Timeline/Services/PathProvider.cs index 15e66972..1f6b629a 100644 --- a/Timeline/Services/PathProvider.cs +++ b/Timeline/Services/PathProvider.cs @@ -1,9 +1,5 @@ using Microsoft.Extensions.Configuration; -using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Threading.Tasks; namespace Timeline.Services { diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 38bb3164..f5220446 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -108,7 +108,7 @@ namespace Timeline services.TryAddSingleton(); - services.AddDbContext((services, options )=> + services.AddDbContext((services, options) => { var pathProvider = services.GetRequiredService(); options.UseSqlite($"Data Source={pathProvider.GetDatabaseFilePath()}"); -- cgit v1.2.3