From c11a1b7be5d41bb1825a7190c708fdb04923a4fd Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Sun, 27 Oct 2019 22:52:21 +0800 Subject: Add error code tests. --- Timeline.Tests/ErrorCodeTest.cs | 52 ++++++++++++++++++++++++ Timeline.Tests/IntegratedTests/UserAvatarTest.cs | 9 ++-- 2 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 Timeline.Tests/ErrorCodeTest.cs (limited to 'Timeline.Tests') diff --git a/Timeline.Tests/ErrorCodeTest.cs b/Timeline.Tests/ErrorCodeTest.cs new file mode 100644 index 00000000..78a58131 --- /dev/null +++ b/Timeline.Tests/ErrorCodeTest.cs @@ -0,0 +1,52 @@ +using FluentAssertions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Xunit; +using Xunit.Abstractions; + +namespace Timeline.Tests +{ + public class ErrorCodeTest + { + private readonly ITestOutputHelper _output; + + public ErrorCodeTest(ITestOutputHelper output) + { + _output = output; + } + + [Fact] + public void ShouldWork() + { + var errorCodes = new Dictionary(); + + void RecursiveCheckErrorCode(Type type) + { + foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) + .Where(fi => fi.IsLiteral && !fi.IsInitOnly && fi.FieldType == typeof(int))) + { + var name = type.FullName + "." + field.Name; + var value = (int)field.GetRawConstantValue(); + _output.WriteLine($"Find error code {name} , value is {value}."); + + value.Should().BeInRange(1000_0000, 9999_9999, "Error code should have exactly 8 digits."); + + errorCodes.Should().NotContainKey(value, + "identical error codes are found and conflict paths are {0} and {1}", + name, errorCodes.GetValueOrDefault(value)); + + errorCodes.Add(value, name); + } + + foreach (var nestedType in type.GetNestedTypes()) + { + RecursiveCheckErrorCode(nestedType); + } + } + + RecursiveCheckErrorCode(typeof(ErrorCodes)); + } + } +} diff --git a/Timeline.Tests/IntegratedTests/UserAvatarTest.cs b/Timeline.Tests/IntegratedTests/UserAvatarTest.cs index ce389046..ad2e11df 100644 --- a/Timeline.Tests/IntegratedTests/UserAvatarTest.cs +++ b/Timeline.Tests/IntegratedTests/UserAvatarTest.cs @@ -13,7 +13,6 @@ using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; -using Timeline.Controllers; using Timeline.Services; using Timeline.Tests.Helpers; using Timeline.Tests.Helpers.Authentication; @@ -95,7 +94,7 @@ namespace Timeline.Tests.IntegratedTests request.Headers.TryAddWithoutValidation("If-None-Match", "\"dsdfd"); var res = await client.SendAsync(request); res.Should().HaveStatusCode(HttpStatusCode.BadRequest) - .And.Should().HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Common.Header.BadFormat_IfNonMatch); + .And.Should().HaveCommonBody().Which.Code.Should().Be(Header.IfNonMatch.BadFormat); } { @@ -125,7 +124,7 @@ namespace Timeline.Tests.IntegratedTests content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); var res = await client.PutAsync("users/user/avatar", content); res.Should().HaveStatusCode(HttpStatusCode.BadRequest) - .And.Should().HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Common.Header.Missing_ContentLength); + .And.Should().HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Filter.Header.ContentLength.Missing); ; } { @@ -133,7 +132,7 @@ namespace Timeline.Tests.IntegratedTests content.Headers.ContentLength = 1; var res = await client.PutAsync("users/user/avatar", content); res.Should().HaveStatusCode(HttpStatusCode.BadRequest) - .And.Should().HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Common.Header.Missing_ContentType); + .And.Should().HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Filter.Header.ContentType.Missing); } { @@ -142,7 +141,7 @@ namespace Timeline.Tests.IntegratedTests content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); var res = await client.PutAsync("users/user/avatar", content); res.Should().HaveStatusCode(HttpStatusCode.BadRequest) - .And.Should().HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Common.Header.Zero_ContentLength); + .And.Should().HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Filter.Header.ContentLength.Zero); } { -- cgit v1.2.3