Skip to content

Commit 2fae696

Browse files
Matthias Gessingercommonsensesoftware
authored andcommitted
Fix failing unit tests
1 parent fb1fdbd commit 2fae696

File tree

9 files changed

+33
-35
lines changed

9 files changed

+33
-35
lines changed

src/Abstractions/src/Asp.Versioning.Abstractions/DeprecationPolicy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ public bool IsEffective( DateTimeOffset? dateTimeOffset )
7373
return true;
7474
}
7575

76-
return date < when;
76+
return date <= when;
7777
}
7878
}

src/AspNet/WebApi/src/Asp.Versioning.WebApi.ApiExplorer/ApiExplorer/VersionedApiExplorer.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,11 @@ protected virtual ApiDescriptionGroupCollection InitializeApiDescriptions()
238238
}
239239

240240
var routes = FlattenRoutes( Configuration.Routes ).ToArray();
241-
var sunsetPolicyManager = Configuration.GetSunsetPolicyManager();
242-
var deprecationPolicyManager = Configuration.GetDeprecationPolicyManager();
243241

244242
foreach ( var apiVersion in FlattenApiVersions( controllerMappings ) )
245243
{
246-
sunsetPolicyManager.TryGetPolicy( apiVersion, out var sunsetPolicy );
247-
deprecationPolicyManager.TryGetPolicy( apiVersion, out var deprecationPolicy );
244+
SunsetPolicyManager.TryGetPolicy( apiVersion, out var sunsetPolicy );
245+
DeprecationPolicyManager.TryGetPolicy( apiVersion, out var deprecationPolicy );
248246

249247
for ( var i = 0; i < routes.Length; i++ )
250248
{

src/AspNet/WebApi/test/Asp.Versioning.WebApi.Tests/DefaultApiVersionReporterTest.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class DefaultApiVersionReporterTest
1313
public void report_should_add_expected_headers()
1414
{
1515
// arrange
16-
var sunsetDate = DateTimeOffset.Now;
17-
var deprecationDate = DateTimeOffset.Now;
16+
var sunsetDate = DateTimeOffset.UtcNow.AddDays( 2 );
17+
var deprecationDate = DateTimeOffset.UtcNow.AddDays( 1 );
1818
var reporter = new DefaultApiVersionReporter( new TestSunsetPolicyManager( sunsetDate ), new TestDeprecationPolicyManager( deprecationDate ) );
1919
var configuration = new HttpConfiguration();
2020
var request = new HttpRequestMessage();
@@ -57,13 +57,11 @@ public void report_should_add_expected_headers()
5757
headers.GetValues( "api-supported-versions" ).Should().Equal( "1.0, 2.0" );
5858
headers.GetValues( "api-deprecated-versions" ).Should().Equal( "0.9" );
5959
headers.GetValues( "Sunset" )
60-
.Single()
6160
.Should()
62-
.Be( sunsetDate.ToString( "r" ) );
61+
.ContainSingle( sunsetDate.ToString( "r" ) );
6362
headers.GetValues( "Deprecation" )
64-
.Single()
6563
.Should()
66-
.Be( $"@{unixTimestamp}" );
64+
.ContainSingle( $"@{unixTimestamp}" );
6765
headers.GetValues( "Link" )
6866
.Should()
6967
.BeEquivalentTo( [

src/AspNetCore/WebApi/test/Asp.Versioning.Http.Tests/DefaultApiVersionReporterTest.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class DefaultApiVersionReporterTest
1212
public void report_should_add_expected_headers()
1313
{
1414
// arrange
15-
var sunsetDate = DateTimeOffset.Now;
16-
var deprecationDate = DateTimeOffset.Now;
15+
var sunsetDate = DateTimeOffset.UtcNow.AddDays( 2 );
16+
var deprecationDate = DateTimeOffset.UtcNow.AddDays( 1 );
1717
var reporter = new DefaultApiVersionReporter( new TestSunsetPolicyManager( sunsetDate ), new TestDeprecationPolicyManager( deprecationDate ) );
1818
var httpContext = new Mock<HttpContext>();
1919
var features = new Mock<IFeatureCollection>();
@@ -65,17 +65,18 @@ public void report_should_add_expected_headers()
6565

6666
headers["api-supported-versions"].Should().Equal( "1.0, 2.0" );
6767
headers["api-deprecated-versions"].Should().Equal( "0.9" );
68-
headers["Sunset"].Single()
69-
.Should()
70-
.Be( sunsetDate.ToString( "r" ) );
71-
headers["Deprecation"].Single()
72-
.Should()
73-
.Be( $"@{unixTimestamp}" );
74-
headers["Link"].Should()
75-
.BeEquivalentTo( [
76-
"<http://docs.api.com/sunset.html>; rel=\"sunset\"",
77-
"<http://docs.api.com/deprecation.html>; rel=\"deprecation\"",
78-
] );
68+
headers["Sunset"]
69+
.Should()
70+
.ContainSingle( sunsetDate.ToString( "r" ) );
71+
headers["Deprecation"]
72+
.Should()
73+
.ContainSingle( $"@{unixTimestamp}" );
74+
headers["Link"]
75+
.Should()
76+
.BeEquivalentTo( [
77+
"<http://docs.api.com/sunset.html>; rel=\"sunset\"",
78+
"<http://docs.api.com/deprecation.html>; rel=\"deprecation\"",
79+
] );
7980
}
8081

8182
private sealed class TestSunsetPolicyManager : IPolicyManager<SunsetPolicy>

src/Client/src/Asp.Versioning.Http.Client/System.Net.Http/HttpResponseMessageExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static class HttpResponseMessageExtensions
1919
private const string Link = nameof( Link );
2020

2121
#if NETSTANDARD1_1
22-
private static readonly DateTime UnixEpoch = new DateTime( 1970, 1, 1 );
22+
private static readonly DateTime UnixEpoch = new DateTime( 1970, 1, 1, 0, 0, 0, DateTimeKind.Utc );
2323
#endif
2424

2525
/// <summary>
@@ -82,7 +82,7 @@ public static SunsetPolicy ReadSunsetPolicy( this HttpResponseMessage response )
8282
public static string ToDeprecationHeaderValue( this DateTimeOffset deprecationDate )
8383
{
8484
var unixTimestamp = deprecationDate.ToUnixTimeSeconds();
85-
return unixTimestamp.ToString( "'@'0", CultureInfo.CurrentCulture );
85+
return unixTimestamp.ToString( "'@'0", CultureInfo.InvariantCulture );
8686
}
8787

8888
/// <summary>
@@ -100,7 +100,7 @@ public static DeprecationPolicy ReadDeprecationPolicy( this HttpResponseMessage
100100

101101
if ( headers.TryGetValues( Deprecation, out var values ) )
102102
{
103-
var culture = CultureInfo.CurrentCulture;
103+
var culture = CultureInfo.InvariantCulture;
104104
var style = NumberStyles.Integer;
105105

106106
foreach ( var value in values )

src/Client/test/Asp.Versioning.Http.Client.Tests/System.Net.Http/HttpResponseMessageExtensionsTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class HttpResponseMessageExtensionsTest
1010
public void read_sunset_policy_should_parse_response()
1111
{
1212
// arrange
13-
var date = DateTimeOffset.Now;
13+
var date = DateTimeOffset.UtcNow;
1414
var request = new HttpRequestMessage( HttpMethod.Get, "http://tempuri.org" );
1515
var response = new HttpResponseMessage() { RequestMessage = request };
1616

@@ -35,7 +35,7 @@ public void read_sunset_policy_should_parse_response()
3535
public void read_sunset_policy_should_use_greatest_date()
3636
{
3737
// arrange
38-
var date = DateTimeOffset.Now;
38+
var date = DateTimeOffset.UtcNow;
3939
var expected = date.AddDays( 14 );
4040
var request = new HttpRequestMessage( HttpMethod.Get, "http://tempuri.org" );
4141
var response = new HttpResponseMessage() { RequestMessage = request };
@@ -91,7 +91,7 @@ public void read_sunset_policy_should_ignore_unrelated_links()
9191
public void read_deprecation_policy_should_parse_response()
9292
{
9393
// arrange
94-
var date = DateTimeOffset.Now;
94+
var date = DateTimeOffset.UtcNow;
9595
var request = new HttpRequestMessage( HttpMethod.Get, "http://tempuri.org" );
9696
var response = new HttpResponseMessage() { RequestMessage = request };
9797

@@ -116,7 +116,7 @@ public void read_deprecation_policy_should_parse_response()
116116
public void read_deprecation_policy_should_use_smallest_date()
117117
{
118118
// arrange
119-
var date = DateTimeOffset.Now;
119+
var date = DateTimeOffset.UtcNow;
120120
var expected = date.Subtract( TimeSpan.FromDays( 14 ) );
121121
var request = new HttpRequestMessage( HttpMethod.Get, "http://tempuri.org" );
122122
var response = new HttpResponseMessage() { RequestMessage = request };

src/Client/test/Asp.Versioning.Http.Client.Tests/net#.0/ApiVersionHandlerLoggerTTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public async Task on_api_deprecated_should_log_message()
2323
var date = DateTimeOffset.Now;
2424
var expected = "API version 1.0 for http://tempuri.org has been deprecated since <unspecified> and will " +
2525
$"sunset on {date.ToUniversalTime()}. Additional information: " +
26-
"API Policy (en): http://tempuri.org/policy/en, " +
27-
"API Política (es): http://tempuri.org/policy/es";
26+
"[API Policy (en): http://tempuri.org/policy/en, " +
27+
"API Política (es): http://tempuri.org/policy/es]";
2828

2929
response.Headers.Add( "sunset", date.ToString( "r" ) );
3030
response.Headers.Add( "link", "<policy/en>; rel=\"sunset\"; type=\"text/html\"; title=\"API Policy\"; hreflang=\"en\"" );

src/Common/src/Common/DefaultApiVersionReporter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public void Report( HttpResponse response, ApiVersionModel apiVersionModel )
104104

105105
if ( deprecationPolicyManager.TryResolvePolicy( name, version, out var deprecationPolicy ) )
106106
{
107+
// Only emit a deprecation header if the deprecation policy becomes effective before the sunset date.
107108
if ( deprecationPolicy.IsEffective( sunsetDate ) )
108109
{
109110
response.WriteDeprecationPolicy( deprecationPolicy );

src/Common/test/Common.Tests/DeprecationPolicyBuilderTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void link_should_should_return_existing_builder()
5151
}
5252

5353
[Fact]
54-
public void build_should_construct_sunset_policy()
54+
public void build_should_construct_deprecation_policy()
5555
{
5656
// arrange
5757
var builder = new DeprecationPolicyBuilder( default, ApiVersion.Default );
@@ -66,6 +66,6 @@ public void build_should_construct_sunset_policy()
6666
policy.Should().BeEquivalentTo(
6767
new DeprecationPolicy(
6868
new DateTimeOffset( new DateTime( 2022, 2, 1 ) ),
69-
new LinkHeaderValue( new Uri( "http://tempuri.org" ), "sunset" ) ) );
69+
new LinkHeaderValue( new Uri( "http://tempuri.org" ), "deprecation" ) ) );
7070
}
7171
}

0 commit comments

Comments
 (0)