Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 80ffd26

Browse files
committed
Adding abstractions for request headers
viz. Accept, Accept-Charset and Content-Type.
1 parent 384d545 commit 80ffd26

File tree

5 files changed

+83
-6
lines changed

5 files changed

+83
-6
lines changed

HttpAbstractions.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.21813.0
4+
VisualStudioVersion = 14.0.21806.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A5A15F1C-885A-452A-A731-B0173DDBD913}"
77
EndProject

src/Microsoft.AspNet.Http/HttpRequest.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections;
6+
using System.Collections.Generic;
57
using System.IO;
68
using System.Threading;
79
using System.Threading.Tasks;
@@ -95,7 +97,7 @@ public abstract class HttpRequest
9597
/// Gets or sets the Content-Type header.
9698
/// </summary>
9799
/// <returns>The Content-Type header.</returns>
98-
// (TODO header conventions?) public abstract string ContentType { get; set; }
100+
public abstract string ContentType { get; set; }
99101

100102
/// <summary>
101103
/// Gets or sets the Cache-Control header.
@@ -113,7 +115,13 @@ public abstract class HttpRequest
113115
/// Gets or set the Accept header.
114116
/// </summary>
115117
/// <returns>The Accept header.</returns>
116-
// (TODO header conventions?) public abstract string Accept { get; set; }
118+
public abstract string Accept { get; set; }
119+
120+
/// <summary>
121+
/// Gets or set the Accept-Charset header.
122+
/// </summary>
123+
/// <returns>The Accept-Charset header.</returns>
124+
public abstract string AcceptCharset { get; set; }
117125

118126
/// <summary>
119127
/// Gets or set the owin.RequestBody Stream.

src/Microsoft.AspNet.PipelineCore/DefaultHttpRequest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,23 @@ public override IReadableStringCollection Cookies
149149
{
150150
get { return RequestCookiesFeature.Cookies; }
151151
}
152+
153+
public override string ContentType
154+
{
155+
get { return Headers[Constants.Headers.ContentType]; }
156+
set { Headers[Constants.Headers.ContentType] = value; }
157+
}
158+
159+
public override string Accept
160+
{
161+
get { return Headers[Constants.Headers.Accept]; }
162+
set { Headers[Constants.Headers.Accept] = value; }
163+
}
164+
165+
public override string AcceptCharset
166+
{
167+
get { return Headers[Constants.Headers.AcceptCharset]; }
168+
set { Headers[Constants.Headers.AcceptCharset] = value; }
169+
}
152170
}
153171
}

src/Microsoft.AspNet.PipelineCore/Infrastructure/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal static class Headers
1515
internal const string CacheControl = "Cache-Control";
1616
internal const string MediaType = "Media-Type";
1717
internal const string Accept = "Accept";
18+
internal const string AcceptCharset = "Accept-Charset";
1819
internal const string Host = "Host";
1920
internal const string ETag = "ETag";
2021
internal const string Location = "Location";

test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpRequestTests.cs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,36 @@ public void GetContentLength_ReturnsNullIfHeaderCannotBeParsed(string contentLen
4949
Assert.Null(request.ContentLength);
5050
}
5151

52+
[Fact]
53+
public void GetContentType_ReturnsNullIfHeaderDoesNotExist()
54+
{
55+
// Arrange
56+
var request = GetRequestWithContentType(contentType: null);
57+
58+
// Act and Assert
59+
Assert.Null(request.ContentType);
60+
}
61+
62+
[Fact]
63+
public void GetAcceptHeader_ReturnsNullIfHeaderDoesNotExist()
64+
{
65+
// Arrange
66+
var request = GetRequestWithAcceptHeader(acceptHeader: null);
67+
68+
// Act and Assert
69+
Assert.Null(request.Accept);
70+
}
71+
72+
[Fact]
73+
public void GetAcceptCharsetHeader_ReturnsNullIfHeaderDoesNotExist()
74+
{
75+
// Arrange
76+
var request = GetRequestWithAcceptCharsetHeader(acceptCharset: null);
77+
78+
// Act and Assert
79+
Assert.Null(request.AcceptCharset);
80+
}
81+
5282
[Fact]
5383
public void Host_GetsHostFromHeaders()
5484
{
@@ -114,14 +144,34 @@ private static HttpRequest CreateRequest(IDictionary<string, string[]> headers)
114144
}
115145

116146
private static HttpRequest GetRequestWithContentLength(string contentLength = null)
147+
{
148+
return GetRequestWithHeader("Content-Length", contentLength);
149+
}
150+
151+
private static HttpRequest GetRequestWithContentType(string contentType = null)
152+
{
153+
return GetRequestWithHeader("Content-Type", contentType);
154+
}
155+
156+
private static HttpRequest GetRequestWithAcceptHeader(string acceptHeader = null)
157+
{
158+
return GetRequestWithHeader("Accept", acceptHeader);
159+
}
160+
161+
private static HttpRequest GetRequestWithAcceptCharsetHeader(string acceptCharset = null)
162+
{
163+
return GetRequestWithHeader("Accept-Charset", acceptCharset);
164+
}
165+
166+
private static HttpRequest GetRequestWithHeader(string headerName, string headerValue)
117167
{
118168
var headers = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
119-
if (contentLength != null)
169+
if (headerValue != null)
120170
{
121-
headers.Add("Content-Length", new[] { contentLength });
171+
headers.Add(headerName, new[] { headerValue });
122172
}
123173

124-
return CreateRequest(headers);
174+
return CreateRequest(headers);
125175
}
126176
}
127177
}

0 commit comments

Comments
 (0)