From 80ffd26465afebfc4eacc68299ff56a5807207b1 Mon Sep 17 00:00:00 2001 From: harshgMSFT Date: Tue, 15 Jul 2014 17:57:44 -0700 Subject: [PATCH] Adding abstractions for request headers viz. Accept, Accept-Charset and Content-Type. --- HttpAbstractions.sln | 2 +- src/Microsoft.AspNet.Http/HttpRequest.cs | 12 +++- .../DefaultHttpRequest.cs | 18 ++++++ .../Infrastructure/Constants.cs | 1 + .../DefaultHttpRequestTests.cs | 56 ++++++++++++++++++- 5 files changed, 83 insertions(+), 6 deletions(-) diff --git a/HttpAbstractions.sln b/HttpAbstractions.sln index 5158aff09e..46b9267189 100644 --- a/HttpAbstractions.sln +++ b/HttpAbstractions.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.21813.0 +VisualStudioVersion = 14.0.21806.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A5A15F1C-885A-452A-A731-B0173DDBD913}" EndProject diff --git a/src/Microsoft.AspNet.Http/HttpRequest.cs b/src/Microsoft.AspNet.Http/HttpRequest.cs index 4d1aa1a9db..be5e1e14b1 100644 --- a/src/Microsoft.AspNet.Http/HttpRequest.cs +++ b/src/Microsoft.AspNet.Http/HttpRequest.cs @@ -2,6 +2,8 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections; +using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; @@ -95,7 +97,7 @@ namespace Microsoft.AspNet.Http /// Gets or sets the Content-Type header. /// /// The Content-Type header. - // (TODO header conventions?) public abstract string ContentType { get; set; } + public abstract string ContentType { get; set; } /// /// Gets or sets the Cache-Control header. @@ -113,7 +115,13 @@ namespace Microsoft.AspNet.Http /// Gets or set the Accept header. /// /// The Accept header. - // (TODO header conventions?) public abstract string Accept { get; set; } + public abstract string Accept { get; set; } + + /// + /// Gets or set the Accept-Charset header. + /// + /// The Accept-Charset header. + public abstract string AcceptCharset { get; set; } /// /// Gets or set the owin.RequestBody Stream. diff --git a/src/Microsoft.AspNet.PipelineCore/DefaultHttpRequest.cs b/src/Microsoft.AspNet.PipelineCore/DefaultHttpRequest.cs index efdda560d1..eac94536d6 100644 --- a/src/Microsoft.AspNet.PipelineCore/DefaultHttpRequest.cs +++ b/src/Microsoft.AspNet.PipelineCore/DefaultHttpRequest.cs @@ -149,5 +149,23 @@ namespace Microsoft.AspNet.PipelineCore { get { return RequestCookiesFeature.Cookies; } } + + public override string ContentType + { + get { return Headers[Constants.Headers.ContentType]; } + set { Headers[Constants.Headers.ContentType] = value; } + } + + public override string Accept + { + get { return Headers[Constants.Headers.Accept]; } + set { Headers[Constants.Headers.Accept] = value; } + } + + public override string AcceptCharset + { + get { return Headers[Constants.Headers.AcceptCharset]; } + set { Headers[Constants.Headers.AcceptCharset] = value; } + } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.PipelineCore/Infrastructure/Constants.cs b/src/Microsoft.AspNet.PipelineCore/Infrastructure/Constants.cs index 1f440b0bf1..9404c79c70 100644 --- a/src/Microsoft.AspNet.PipelineCore/Infrastructure/Constants.cs +++ b/src/Microsoft.AspNet.PipelineCore/Infrastructure/Constants.cs @@ -15,6 +15,7 @@ namespace Microsoft.AspNet.Http.Infrastructure internal const string CacheControl = "Cache-Control"; internal const string MediaType = "Media-Type"; internal const string Accept = "Accept"; + internal const string AcceptCharset = "Accept-Charset"; internal const string Host = "Host"; internal const string ETag = "ETag"; internal const string Location = "Location"; diff --git a/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpRequestTests.cs b/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpRequestTests.cs index baa1c6c4ed..c82bfd67b7 100644 --- a/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpRequestTests.cs +++ b/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpRequestTests.cs @@ -49,6 +49,36 @@ namespace Microsoft.AspNet.PipelineCore.Tests Assert.Null(request.ContentLength); } + [Fact] + public void GetContentType_ReturnsNullIfHeaderDoesNotExist() + { + // Arrange + var request = GetRequestWithContentType(contentType: null); + + // Act and Assert + Assert.Null(request.ContentType); + } + + [Fact] + public void GetAcceptHeader_ReturnsNullIfHeaderDoesNotExist() + { + // Arrange + var request = GetRequestWithAcceptHeader(acceptHeader: null); + + // Act and Assert + Assert.Null(request.Accept); + } + + [Fact] + public void GetAcceptCharsetHeader_ReturnsNullIfHeaderDoesNotExist() + { + // Arrange + var request = GetRequestWithAcceptCharsetHeader(acceptCharset: null); + + // Act and Assert + Assert.Null(request.AcceptCharset); + } + [Fact] public void Host_GetsHostFromHeaders() { @@ -114,14 +144,34 @@ namespace Microsoft.AspNet.PipelineCore.Tests } private static HttpRequest GetRequestWithContentLength(string contentLength = null) + { + return GetRequestWithHeader("Content-Length", contentLength); + } + + private static HttpRequest GetRequestWithContentType(string contentType = null) + { + return GetRequestWithHeader("Content-Type", contentType); + } + + private static HttpRequest GetRequestWithAcceptHeader(string acceptHeader = null) + { + return GetRequestWithHeader("Accept", acceptHeader); + } + + private static HttpRequest GetRequestWithAcceptCharsetHeader(string acceptCharset = null) + { + return GetRequestWithHeader("Accept-Charset", acceptCharset); + } + + private static HttpRequest GetRequestWithHeader(string headerName, string headerValue) { var headers = new Dictionary(StringComparer.OrdinalIgnoreCase); - if (contentLength != null) + if (headerValue != null) { - headers.Add("Content-Length", new[] { contentLength }); + headers.Add(headerName, new[] { headerValue }); } - return CreateRequest(headers); + return CreateRequest(headers); } } }