Adding abstractions for request headers

viz. Accept, Accept-Charset and Content-Type.
This commit is contained in:
harshgMSFT 2014-07-15 17:57:44 -07:00
parent 384d54577e
commit 80ffd26465
5 changed files with 83 additions and 6 deletions

View File

@ -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

View File

@ -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.
/// </summary>
/// <returns>The Content-Type header.</returns>
// (TODO header conventions?) public abstract string ContentType { get; set; }
public abstract string ContentType { get; set; }
/// <summary>
/// Gets or sets the Cache-Control header.
@ -113,7 +115,13 @@ namespace Microsoft.AspNet.Http
/// Gets or set the Accept header.
/// </summary>
/// <returns>The Accept header.</returns>
// (TODO header conventions?) public abstract string Accept { get; set; }
public abstract string Accept { get; set; }
/// <summary>
/// Gets or set the Accept-Charset header.
/// </summary>
/// <returns>The Accept-Charset header.</returns>
public abstract string AcceptCharset { get; set; }
/// <summary>
/// Gets or set the owin.RequestBody Stream.

View File

@ -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; }
}
}
}

View File

@ -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";

View File

@ -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<string, string[]>(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);
}
}
}