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

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

Merged
merged 1 commit into from
Nov 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void AppendCommaSeparatedValues(this IHeaderDictionary headers, st
/// <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)
{
return ParsingHelpers.GetHeaderSplit(headers, key).ToArray();
return ParsingHelpers.GetHeaderSplit(headers, key);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ internal static T Get<T>(this IHeaderDictionary headers, string name)
if (KnownParsers.TryGetValue(typeof(T), out temp))
{
var func = (Func<string, T>)temp;
return func(headers[name].ToString());
return func(headers[name]);
}

var value = headers[name];
Expand All @@ -202,7 +202,7 @@ internal static IList<T> GetList<T>(this IHeaderDictionary headers, string name)
if (KnownListParsers.TryGetValue(typeof(T), out temp))
{
var func = (Func<IList<string>, IList<T>>)temp;
return func(headers[name].ToArray());
return func(headers[name]);
}

var values = headers[name];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static void AppendHeaderJoined(IHeaderDictionary headers, string key, par
return;
}

string existing = GetHeader(headers, key).ToString();
string existing = GetHeader(headers, key);
if (existing == null)
{
SetHeaderJoined(headers, key, values);
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http.Extensions/RequestHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public HostString Host
{
get
{
return HostString.FromUriComponent(Headers[HeaderNames.Host].ToString());
return HostString.FromUriComponent(Headers[HeaderNames.Host]);
}
set
{
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http.Extensions/ResponseHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public Uri Location
get
{
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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http/DefaultHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public override bool IsHttps

public override HostString Host
{
get { return HostString.FromUriComponent(Headers["Host"].ToString()); }
get { return HostString.FromUriComponent(Headers["Host"]); }
set { Headers["Host"] = value.ToUriComponent(); }
}

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http/DefaultHttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public override string ContentType
{
get
{
return Headers[HeaderNames.ContentType].ToString();
return Headers[HeaderNames.ContentType];
}
set
{
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http/DefaultWebSocketManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override IList<string> WebSocketRequestedProtocols
{
get
{
return ParsingHelpers.GetHeaderSplit(HttpRequestFeature.Headers, HeaderNames.WebSocketSubProtocols).ToArray();
return ParsingHelpers.GetHeaderSplit(HttpRequestFeature.Headers, HeaderNames.WebSocketSubProtocols);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.AspNet.Http/Features/FormFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public FormFile(Stream baseStream, long baseStreamOffset, long length)

public string ContentDisposition
{
get { return Headers["Content-Disposition"].ToString(); }
get { return Headers["Content-Disposition"]; }
set { Headers["Content-Disposition"] = value; }
}

public string ContentType
{
get { return Headers["Content-Type"].ToString(); }
get { return Headers["Content-Type"]; }
set { Headers["Content-Type"] = value; }
}

Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.AspNet.Http/RequestCookieCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public string this[string key]

if (Store == null)
{
return string.Empty;
return null;
}

string value;
if (TryGetValue(key, out value))
{
return value;
}
return string.Empty;
return null;
}
}

Expand Down Expand Up @@ -126,7 +126,7 @@ public bool TryGetValue(string key, out string value)
{
if (Store == null)
{
value = string.Empty;
value = null;
return false;
}
return Store.TryGetValue(key, out value);
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.WebUtilities/MultipartReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private async Task<Dictionary<string, StringValues>> ReadHeadersAsync(Cancellati
throw new InvalidOperationException("Total header size limit exceeded: " + TotalHeaderSizeLimit.ToString());
}
int splitIndex = line.IndexOf(':');
Debug.Assert(splitIndex > 0, $"Invalid header line: {line.ToString()}");
Debug.Assert(splitIndex > 0, $"Invalid header line: {line}");
if (splitIndex >= 0)
{
var name = line.Substring(0, splitIndex);
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.AspNet.WebUtilities/MultipartSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public string ContentType
StringValues values;
if (Headers.TryGetValue("Content-Type", out values))
{
return values.ToString();
return values;
}
return null;
}
Expand All @@ -29,7 +29,7 @@ public string ContentDisposition
StringValues values;
if (Headers.TryGetValue("Content-Disposition", out values))
{
return values.ToString();
return values;
}
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions test/Microsoft.AspNet.Http.Tests/DefaultHttpRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public void Cookies_GetAndSet()
Assert.Equal(0, cookieHeaders.Count);
var cookies0 = request.Cookies;
Assert.Equal(0, cookies0.Count);
Assert.Null(cookies0["key0"]);
Assert.False(cookies0.ContainsKey("key0"));

var newCookies = new[] { "name0=value0", "name1=value1" };
request.Headers["Cookie"] = newCookies;
Expand Down
90 changes: 90 additions & 0 deletions test/Microsoft.AspNet.Http.Tests/DefaultHttpResponseTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}