From 03f8a7a21723c6169003d8cf0ecd78530e9c1cb2 Mon Sep 17 00:00:00 2001 From: Kristian Hellang Date: Thu, 6 Oct 2016 20:56:49 +0200 Subject: [PATCH] Use HeaderUtilities.FormatDate in DateHeaderValueManager (#1132) --- .../Internal/Http/DateHeaderValueManager.cs | 4 ++-- .../Internal/Infrastructure/Constants.cs | 6 ----- .../DateHeaderValueManagerTests.cs | 23 ++++++++++++------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/DateHeaderValueManager.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/DateHeaderValueManager.cs index b5fc6730e6..b824fb606d 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/DateHeaderValueManager.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/DateHeaderValueManager.cs @@ -5,6 +5,7 @@ using System; using System.Text; using System.Threading; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; +using Microsoft.Net.Http.Headers; namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http { @@ -170,8 +171,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http /// A DateTimeOffset value private void SetDateValues(DateTimeOffset value) { - // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.18 for required format of Date header - var dateValue = value.ToString(Constants.RFC1123DateFormat); + var dateValue = HeaderUtilities.FormatDate(value); var dateBytes = new byte[_datePreambleBytes.Length + dateValue.Length]; Buffer.BlockCopy(_datePreambleBytes, 0, dateBytes, 0, _datePreambleBytes.Length); Encoding.ASCII.GetBytes(dateValue, 0, dateValue.Length, dateBytes, _datePreambleBytes.Length); diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/Constants.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/Constants.cs index 90f228de31..ddc9f767b9 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/Constants.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/Constants.cs @@ -17,12 +17,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure /// public const string UnixPipeHostPrefix = "unix:/"; - /// - /// DateTime format string for RFC1123. See https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#RFC1123 - /// for info on the format. - /// - public const string RFC1123DateFormat = "r"; - public const string ServerName = "Kestrel"; private static int? GetEADDRINUSE() diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/DateHeaderValueManagerTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/DateHeaderValueManagerTests.cs index ddfce0d3a6..8e9802600b 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/DateHeaderValueManagerTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/DateHeaderValueManagerTests.cs @@ -4,7 +4,6 @@ using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; -using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; using Microsoft.AspNetCore.Testing; using Xunit; @@ -12,6 +11,14 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { public class DateHeaderValueManagerTests { + /// + /// DateTime format string for RFC1123. + /// + /// + /// See https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#RFC1123 for info on the format. + /// + private const string Rfc1123DateFormat = "r"; + [Fact] public void GetDateHeaderValue_ReturnsDateValueInRFC1123Format() { @@ -34,7 +41,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests dateHeaderValueManager.Dispose(); } - Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result); + Assert.Equal(now.ToString(Rfc1123DateFormat), result); } [Fact] @@ -63,8 +70,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests dateHeaderValueManager.Dispose(); } - Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result1); - Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result2); + Assert.Equal(now.ToString(Rfc1123DateFormat), result1); + Assert.Equal(now.ToString(Rfc1123DateFormat), result2); Assert.Equal(1, systemClock.UtcNowCalled); } @@ -96,8 +103,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests dateHeaderValueManager.Dispose(); } - Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result1); - Assert.Equal(future.ToString(Constants.RFC1123DateFormat), result2); + Assert.Equal(now.ToString(Rfc1123DateFormat), result1); + Assert.Equal(future.ToString(Rfc1123DateFormat), result2); Assert.True(systemClock.UtcNowCalled >= 2); } @@ -119,8 +126,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests systemClock.UtcNow = future; var result2 = dateHeaderValueManager.GetDateHeaderValues().String; - Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result1); - Assert.Equal(future.ToString(Constants.RFC1123DateFormat), result2); + Assert.Equal(now.ToString(Rfc1123DateFormat), result1); + Assert.Equal(future.ToString(Rfc1123DateFormat), result2); } } }