|
| 1 | +using FluentAssertions.Execution; |
| 2 | +using FluentAssertions.Primitives; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using System; |
| 5 | +using Microsoft.Net.Http.Headers; |
| 6 | + |
| 7 | +namespace FluentAssertions.AspNetCore.Mvc |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Base class for <see cref="FileResultAssertions"/>, <see cref="FileContentResultAssertions"/>, |
| 11 | + /// <see cref="PhysicalFileResultAssertions"/>, <see cref="VirtualFileResultAssertions"/> and |
| 12 | + /// <see cref="FileStreamResultAssertions"/>. |
| 13 | + /// </summary> |
| 14 | + /// <typeparam name="TFileResult">The type of the testet FileResult variant.</typeparam> |
| 15 | + /// <typeparam name="TFileResultAssertions">The subclass of this class.</typeparam> |
| 16 | + public class FileResultAssertionsBase<TFileResult, TFileResultAssertions> : ObjectAssertions |
| 17 | + where TFileResult : FileResult |
| 18 | + where TFileResultAssertions : FileResultAssertionsBase<TFileResult, TFileResultAssertions> |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// Creates an instance of this class. |
| 22 | + /// </summary> |
| 23 | + /// <param name="fileResult">The testet instance.</param> |
| 24 | + protected FileResultAssertionsBase(TFileResult fileResult) |
| 25 | + : base(fileResult) |
| 26 | + { |
| 27 | + Identifier = typeof(TFileResult).Name; |
| 28 | + } |
| 29 | + |
| 30 | + #region Private Properties |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The <see cref="FileResult"/> under test. |
| 34 | + /// </summary> |
| 35 | + protected TFileResult FileResultSubject => (TFileResult)Subject; |
| 36 | + |
| 37 | + #endregion Private Properties |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Returns the type of the subject the assertion applies on. |
| 41 | + /// </summary> |
| 42 | + protected override string Identifier { get; } |
| 43 | + |
| 44 | + #region Public Properties |
| 45 | + /// <summary> |
| 46 | + /// The <see cref="FileResult.ContentType">ContentType</see> on the <see cref="FileResult"/>. |
| 47 | + /// </summary> |
| 48 | + public string ContentType => FileResultSubject.ContentType; |
| 49 | + /// <summary> |
| 50 | + /// The <see cref="FileResult.FileDownloadName">ContentType</see> on the <see cref="FileResult"/>. |
| 51 | + /// </summary> |
| 52 | + public string FileDownloadName => FileResultSubject.FileDownloadName; |
| 53 | + /// <summary> |
| 54 | + /// The <see cref="FileResult.LastModified">ContentType</see> on the <see cref="FileResult"/>. |
| 55 | + /// </summary> |
| 56 | + public DateTimeOffset? LastModified => FileResultSubject.LastModified; |
| 57 | + /// <summary> |
| 58 | + /// The <see cref="FileResult.EntityTag">ContentType</see> on the <see cref="FileResult"/>. |
| 59 | + /// </summary> |
| 60 | + public EntityTagHeaderValue EntityTag => FileResultSubject.EntityTag; |
| 61 | + |
| 62 | + #endregion |
| 63 | + |
| 64 | + #region Public Methods |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Asserts that the content type is the expected string. |
| 68 | + /// </summary> |
| 69 | + /// <param name="expectedContentType">The expected content type.</param> |
| 70 | + /// <param name="reason"> |
| 71 | + /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion |
| 72 | + /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. |
| 73 | + /// </param> |
| 74 | + /// <param name="reasonArgs"> |
| 75 | + /// Zero or more objects to format using the placeholders in <paramref name="reason"/>. |
| 76 | + /// </param> |
| 77 | + public TFileResultAssertions WithContentType(string expectedContentType, string reason = "", |
| 78 | + params object[] reasonArgs) |
| 79 | + { |
| 80 | + var actualContentType = FileResultSubject.ContentType; |
| 81 | + |
| 82 | + Execute.Assertion |
| 83 | + .ForCondition(string.Equals(expectedContentType, actualContentType, StringComparison.OrdinalIgnoreCase)) |
| 84 | + .BecauseOf(reason, reasonArgs) |
| 85 | + .WithDefaultIdentifier($"{Identifier}.ContentType") |
| 86 | + .FailWith(FailureMessages.CommonFailMessage, expectedContentType, actualContentType); |
| 87 | + return (TFileResultAssertions)this; |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Asserts that the entity tag is the expected value. |
| 92 | + /// </summary> |
| 93 | + /// <param name="expectedEntityTag">The expected entity tag value.</param> |
| 94 | + /// <param name="reason"> |
| 95 | + /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion |
| 96 | + /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. |
| 97 | + /// </param> |
| 98 | + /// <param name="reasonArgs"> |
| 99 | + /// Zero or more objects to format using the placeholders in <paramref name="reason"/>. |
| 100 | + /// </param> |
| 101 | + public TFileResultAssertions WithEntityTag(EntityTagHeaderValue expectedEntityTag, string reason = "", |
| 102 | + params object[] reasonArgs) |
| 103 | + { |
| 104 | + var actualEntityTag = FileResultSubject.EntityTag; |
| 105 | + |
| 106 | + Execute.Assertion |
| 107 | + .ForCondition(Equals(expectedEntityTag, actualEntityTag)) |
| 108 | + .BecauseOf(reason, reasonArgs) |
| 109 | + .WithDefaultIdentifier($"{Identifier}.EntityTag") |
| 110 | + .FailWith(FailureMessages.CommonFailMessage, expectedEntityTag, actualEntityTag); |
| 111 | + return (TFileResultAssertions)this; |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Asserts that the file download name is the expected string. |
| 116 | + /// </summary> |
| 117 | + /// <param name="expectedFileDownloadName">The expected file download name.</param> |
| 118 | + /// <param name="reason"> |
| 119 | + /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion |
| 120 | + /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. |
| 121 | + /// </param> |
| 122 | + /// <param name="reasonArgs"> |
| 123 | + /// Zero or more objects to format using the placeholders in <paramref name="reason"/>. |
| 124 | + /// </param> |
| 125 | + public TFileResultAssertions WithFileDownloadName(string expectedFileDownloadName, string reason = "", |
| 126 | + params object[] reasonArgs) |
| 127 | + { |
| 128 | + var actualFileDownloadName = FileResultSubject.FileDownloadName; |
| 129 | + |
| 130 | + Execute.Assertion |
| 131 | + .ForCondition(string.Equals(expectedFileDownloadName, actualFileDownloadName, StringComparison.OrdinalIgnoreCase)) |
| 132 | + .BecauseOf(reason, reasonArgs) |
| 133 | + .WithDefaultIdentifier($"{Identifier}.FileDownloadName") |
| 134 | + .FailWith(FailureMessages.CommonFailMessage, expectedFileDownloadName, actualFileDownloadName); |
| 135 | + return (TFileResultAssertions)this; |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Asserts that the last modified is the expected DateTimeOffset. |
| 140 | + /// </summary> |
| 141 | + /// <param name="expectedLastModified">The expected last modified value.</param> |
| 142 | + /// <param name="reason"> |
| 143 | + /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion |
| 144 | + /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. |
| 145 | + /// </param> |
| 146 | + /// <param name="reasonArgs"> |
| 147 | + /// Zero or more objects to format using the placeholders in <paramref name="reason"/>. |
| 148 | + /// </param> |
| 149 | + public TFileResultAssertions WithLastModified(DateTimeOffset? expectedLastModified, string reason = "", |
| 150 | + params object[] reasonArgs) |
| 151 | + { |
| 152 | + var actualLastModified = FileResultSubject.LastModified; |
| 153 | + |
| 154 | + Execute.Assertion |
| 155 | + .ForCondition(expectedLastModified == actualLastModified) |
| 156 | + .BecauseOf(reason, reasonArgs) |
| 157 | + .WithDefaultIdentifier($"{Identifier}.LastModified") |
| 158 | + .FailWith(FailureMessages.CommonFailMessage, expectedLastModified, actualLastModified); |
| 159 | + return (TFileResultAssertions)this; |
| 160 | + } |
| 161 | + |
| 162 | + #endregion |
| 163 | + } |
| 164 | +} |
0 commit comments