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

Do not check if key is present before removing item in AuthenticationProperties #1064

Merged
merged 1 commit into from
Nov 14, 2018
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 @@ -122,7 +122,7 @@ public void SetString(string key, string value)
{
Items[key] = value;
}
else if (Items.ContainsKey(key))
else
{
Items.Remove(key);
}
Expand Down Expand Up @@ -169,9 +169,9 @@ protected void SetBool(string key, bool? value)
{
if (value.HasValue)
{
Items[key] = value.Value.ToString();
Items[key] = value.GetValueOrDefault().ToString();
}
else if (Items.ContainsKey(key))
else
{
Items.Remove(key);
}
Expand Down Expand Up @@ -201,9 +201,9 @@ protected void SetDateTimeOffset(string key, DateTimeOffset? value)
{
if (value.HasValue)
{
Items[key] = value.Value.ToString(UtcDateTimeFormat, CultureInfo.InvariantCulture);
Items[key] = value.GetValueOrDefault().ToString(UtcDateTimeFormat, CultureInfo.InvariantCulture);
}
else if (Items.ContainsKey(key))
else
{
Items.Remove(key);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Xunit;

Expand Down Expand Up @@ -73,6 +74,10 @@ public void GetSetString()
props.SetString("foo", null);
Assert.Null(props.GetString("foo"));
Assert.Equal(1, props.Items.Count);

props.SetString("doesntexist", null);
Assert.False(props.Items.ContainsKey("doesntexist"));
Assert.Equal(1, props.Items.Count);
}

[Fact]
Expand Down Expand Up @@ -203,5 +208,94 @@ public void AllowRefresh_Test()
props.Items.Clear();
Assert.Null(props.AllowRefresh);
}

[Fact]
public void SetDateTimeOffset()
{
var props = new MyAuthenticationProperties();

props.SetDateTimeOffset("foo", new DateTimeOffset(new DateTime(2018, 03, 19, 12, 34, 56, DateTimeKind.Utc)));
Assert.Equal("Mon, 19 Mar 2018 12:34:56 GMT", props.Items["foo"]);

props.SetDateTimeOffset("foo", null);
Assert.False(props.Items.ContainsKey("foo"));

props.SetDateTimeOffset("doesnotexist", null);
Assert.False(props.Items.ContainsKey("doesnotexist"));
}

[Fact]
public void GetDateTimeOffset()
{
var props = new MyAuthenticationProperties();
var dateTimeOffset = new DateTimeOffset(new DateTime(2018, 03, 19, 12, 34, 56, DateTimeKind.Utc));

props.Items["foo"] = dateTimeOffset.ToString("r", CultureInfo.InvariantCulture);
Assert.Equal(dateTimeOffset, props.GetDateTimeOffset("foo"));

props.Items.Remove("foo");
Assert.Null(props.GetDateTimeOffset("foo"));

props.Items["foo"] = "BAR";
Assert.Null(props.GetDateTimeOffset("foo"));
Assert.Equal("BAR", props.Items["foo"]);
}

[Fact]
public void SetBool()
{
var props = new MyAuthenticationProperties();

props.SetBool("foo", true);
Assert.Equal(true.ToString(), props.Items["foo"]);

props.SetBool("foo", false);
Assert.Equal(false.ToString(), props.Items["foo"]);

props.SetBool("foo", null);
Assert.False(props.Items.ContainsKey("foo"));
}

[Fact]
public void GetBool()
{
var props = new MyAuthenticationProperties();

props.Items["foo"] = true.ToString();
Assert.True(props.GetBool("foo"));

props.Items["foo"] = false.ToString();
Assert.False(props.GetBool("foo"));

props.Items["foo"] = null;
Assert.Null(props.GetBool("foo"));

props.Items["foo"] = "BAR";
Assert.Null(props.GetBool("foo"));
Assert.Equal("BAR", props.Items["foo"]);
}

public class MyAuthenticationProperties : AuthenticationProperties
{
public new DateTimeOffset? GetDateTimeOffset(string key)
{
return base.GetDateTimeOffset(key);
}

public new void SetDateTimeOffset(string key, DateTimeOffset? value)
{
base.SetDateTimeOffset(key, value);
}

public new void SetBool(string key, bool? value)
{
base.SetBool(key, value);
}

public new bool? GetBool(string key)
{
return base.GetBool(key);
}
}
}
}