Revert use of explicit converters that prevent APIs from returning null.

This commit is contained in:
Chris R 2015-11-04 16:03:36 -08:00
parent bacf76098e
commit f050e09283
14 changed files with 109 additions and 17 deletions

View File

@ -36,7 +36,7 @@ namespace Microsoft.AspNet.Http
/// <returns>the associated values from the collection separated into individual values, or StringValues.Empty if the key is not present.</returns> /// <returns>the associated values from the collection separated into individual values, or StringValues.Empty if the key is not present.</returns>
public static string[] GetCommaSeparatedValues(this IHeaderDictionary headers, string key) public static string[] GetCommaSeparatedValues(this IHeaderDictionary headers, string key)
{ {
return ParsingHelpers.GetHeaderSplit(headers, key).ToArray(); return ParsingHelpers.GetHeaderSplit(headers, key);
} }
/// <summary> /// <summary>

View File

@ -179,7 +179,7 @@ namespace Microsoft.AspNet.Http
if (KnownParsers.TryGetValue(typeof(T), out temp)) if (KnownParsers.TryGetValue(typeof(T), out temp))
{ {
var func = (Func<string, T>)temp; var func = (Func<string, T>)temp;
return func(headers[name].ToString()); return func(headers[name]);
} }
var value = headers[name]; var value = headers[name];
@ -202,7 +202,7 @@ namespace Microsoft.AspNet.Http
if (KnownListParsers.TryGetValue(typeof(T), out temp)) if (KnownListParsers.TryGetValue(typeof(T), out temp))
{ {
var func = (Func<IList<string>, IList<T>>)temp; var func = (Func<IList<string>, IList<T>>)temp;
return func(headers[name].ToArray()); return func(headers[name]);
} }
var values = headers[name]; var values = headers[name];

View File

@ -126,7 +126,7 @@ namespace Microsoft.AspNet.Http.Internal
return; return;
} }
string existing = GetHeader(headers, key).ToString(); string existing = GetHeader(headers, key);
if (existing == null) if (existing == null)
{ {
SetHeaderJoined(headers, key, values); SetHeaderJoined(headers, key, values);

View File

@ -170,7 +170,7 @@ namespace Microsoft.AspNet.Http.Headers
{ {
get get
{ {
return HostString.FromUriComponent(Headers[HeaderNames.Host].ToString()); return HostString.FromUriComponent(Headers[HeaderNames.Host]);
} }
set set
{ {

View File

@ -134,7 +134,7 @@ namespace Microsoft.AspNet.Http.Headers
get get
{ {
Uri uri; Uri uri;
if (Uri.TryCreate(Headers[HeaderNames.Location].ToString(), UriKind.RelativeOrAbsolute, out uri)) if (Uri.TryCreate(Headers[HeaderNames.Location], UriKind.RelativeOrAbsolute, out uri))
{ {
return uri; return uri;
} }

View File

@ -140,7 +140,7 @@ namespace Microsoft.AspNet.Http.Internal
public override HostString Host public override HostString Host
{ {
get { return HostString.FromUriComponent(Headers["Host"].ToString()); } get { return HostString.FromUriComponent(Headers["Host"]); }
set { Headers["Host"] = value.ToUriComponent(); } set { Headers["Host"] = value.ToUriComponent(); }
} }

View File

@ -87,7 +87,7 @@ namespace Microsoft.AspNet.Http.Internal
{ {
get get
{ {
return Headers[HeaderNames.ContentType].ToString(); return Headers[HeaderNames.ContentType];
} }
set set
{ {

View File

@ -55,7 +55,7 @@ namespace Microsoft.AspNet.Http.Internal
{ {
get get
{ {
return ParsingHelpers.GetHeaderSplit(HttpRequestFeature.Headers, HeaderNames.WebSocketSubProtocols).ToArray(); return ParsingHelpers.GetHeaderSplit(HttpRequestFeature.Headers, HeaderNames.WebSocketSubProtocols);
} }
} }

View File

@ -21,13 +21,13 @@ namespace Microsoft.AspNet.Http.Features.Internal
public string ContentDisposition public string ContentDisposition
{ {
get { return Headers["Content-Disposition"].ToString(); } get { return Headers["Content-Disposition"]; }
set { Headers["Content-Disposition"] = value; } set { Headers["Content-Disposition"] = value; }
} }
public string ContentType public string ContentType
{ {
get { return Headers["Content-Type"].ToString(); } get { return Headers["Content-Type"]; }
set { Headers["Content-Type"] = value; } set { Headers["Content-Type"] = value; }
} }

View File

@ -48,7 +48,7 @@ namespace Microsoft.AspNet.Http.Internal
if (Store == null) if (Store == null)
{ {
return string.Empty; return null;
} }
string value; string value;
@ -56,7 +56,7 @@ namespace Microsoft.AspNet.Http.Internal
{ {
return value; return value;
} }
return string.Empty; return null;
} }
} }
@ -126,7 +126,7 @@ namespace Microsoft.AspNet.Http.Internal
{ {
if (Store == null) if (Store == null)
{ {
value = string.Empty; value = null;
return false; return false;
} }
return Store.TryGetValue(key, out value); return Store.TryGetValue(key, out value);

View File

@ -87,7 +87,7 @@ namespace Microsoft.AspNet.WebUtilities
throw new InvalidOperationException("Total header size limit exceeded: " + TotalHeaderSizeLimit.ToString()); throw new InvalidOperationException("Total header size limit exceeded: " + TotalHeaderSizeLimit.ToString());
} }
int splitIndex = line.IndexOf(':'); int splitIndex = line.IndexOf(':');
Debug.Assert(splitIndex > 0, $"Invalid header line: {line.ToString()}"); Debug.Assert(splitIndex > 0, $"Invalid header line: {line}");
if (splitIndex >= 0) if (splitIndex >= 0)
{ {
var name = line.Substring(0, splitIndex); var name = line.Substring(0, splitIndex);

View File

@ -16,7 +16,7 @@ namespace Microsoft.AspNet.WebUtilities
StringValues values; StringValues values;
if (Headers.TryGetValue("Content-Type", out values)) if (Headers.TryGetValue("Content-Type", out values))
{ {
return values.ToString(); return values;
} }
return null; return null;
} }
@ -29,7 +29,7 @@ namespace Microsoft.AspNet.WebUtilities
StringValues values; StringValues values;
if (Headers.TryGetValue("Content-Disposition", out values)) if (Headers.TryGetValue("Content-Disposition", out values))
{ {
return values.ToString(); return values;
} }
return null; return null;
} }

View File

@ -169,6 +169,8 @@ namespace Microsoft.AspNet.Http.Internal
Assert.Equal(0, cookieHeaders.Count); Assert.Equal(0, cookieHeaders.Count);
var cookies0 = request.Cookies; var cookies0 = request.Cookies;
Assert.Equal(0, cookies0.Count); Assert.Equal(0, cookies0.Count);
Assert.Null(cookies0["key0"]);
Assert.False(cookies0.ContainsKey("key0"));
var newCookies = new[] { "name0=value0", "name1=value1" }; var newCookies = new[] { "name0=value0", "name1=value1" };
request.Headers["Cookie"] = newCookies; request.Headers["Cookie"] = newCookies;

View File

@ -0,0 +1,90 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http.Features;
using Microsoft.Extensions.Primitives;
using Xunit;
namespace Microsoft.AspNet.Http.Internal
{
public class DefaultHttpResponseTests
{
[Theory]
[InlineData(0)]
[InlineData(9001)]
[InlineData(65535)]
public void GetContentLength_ReturnsParsedHeader(long value)
{
// Arrange
var response = GetResponseWithContentLength(value.ToString(CultureInfo.InvariantCulture));
// Act and Assert
Assert.Equal(value, response.ContentLength);
}
[Fact]
public void GetContentLength_ReturnsNullIfHeaderDoesNotExist()
{
// Arrange
var response = GetResponseWithContentLength(contentLength: null);
// Act and Assert
Assert.Null(response.ContentLength);
}
[Theory]
[InlineData("cant-parse-this")]
[InlineData("-1000")]
[InlineData("1000.00")]
[InlineData("100/5")]
public void GetContentLength_ReturnsNullIfHeaderCannotBeParsed(string contentLength)
{
// Arrange
var response = GetResponseWithContentLength(contentLength);
// Act and Assert
Assert.Null(response.ContentLength);
}
[Fact]
public void GetContentType_ReturnsNullIfHeaderDoesNotExist()
{
// Arrange
var response = GetResponseWithContentType(contentType: null);
// Act and Assert
Assert.Null(response.ContentType);
}
private static HttpResponse CreateResponse(IHeaderDictionary headers)
{
var context = new DefaultHttpContext();
context.Features.Get<IHttpResponseFeature>().Headers = headers;
return context.Response;
}
private static HttpResponse GetResponseWithContentLength(string contentLength = null)
{
return GetResponseWithHeader("Content-Length", contentLength);
}
private static HttpResponse GetResponseWithContentType(string contentType = null)
{
return GetResponseWithHeader("Content-Type", contentType);
}
private static HttpResponse GetResponseWithHeader(string headerName, string headerValue)
{
var headers = new HeaderDictionary();
if (headerValue != null)
{
headers.Add(headerName, headerValue);
}
return CreateResponse(headers);
}
}
}