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

Set default path=/ when removing cookie #476

Merged
merged 1 commit into from
Nov 16, 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
31 changes: 1 addition & 30 deletions src/Microsoft.AspNet.Http/ResponseCookies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,36 +80,7 @@ public void Append(string key, string value, CookieOptions options)
/// <param name="key"></param>
public void Delete(string key)
{
var encodedKeyPlusEquals = UrlEncoder.Default.Encode(key) + "=";
Func<string, string, bool> predicate = (value, encKeyPlusEquals) => value.StartsWith(encKeyPlusEquals, StringComparison.OrdinalIgnoreCase);

StringValues deleteCookies = $"{encodedKeyPlusEquals}; expires=Thu, 01-Jan-1970 00:00:00 GMT";
var existingValues = Headers[HeaderNames.SetCookie];
if (StringValues.IsNullOrEmpty(existingValues))
{
Headers[HeaderNames.SetCookie] = deleteCookies;
}
else
{
var values = existingValues.ToArray();
var newValues = new List<string>();

for (var i = 0; i < values.Length; i++)
{
if (!predicate(values[i], encodedKeyPlusEquals))
{
newValues.Add(values[i]);
}
}

values = deleteCookies.ToArray();
for (var i = 0; i < values.Length; i++)
{
newValues.Add(values[i]);
}

Headers[HeaderNames.SetCookie] = new StringValues(newValues.ToArray());
}
Delete(key, new CookieOptions() { Path = "/" });
}

/// <summary>
Expand Down
46 changes: 46 additions & 0 deletions test/Microsoft.AspNet.Http.Tests/ResponseCookiesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 Xunit;
using Microsoft.Net.Http.Headers;
using Microsoft.AspNet.Http.Internal;

namespace Microsoft.AspNet.Http.Tests
{
public class ResponseCookiesTest
{
[Fact]
public void DeleteCookieShouldSetDefaultPath()
{
var headers = new HeaderDictionary();
var cookies = new ResponseCookies(headers);
var testcookie = "TestCookie";

cookies.Delete(testcookie);

var cookieHeaderValues = headers[HeaderNames.SetCookie];
Assert.Equal(1, cookieHeaderValues.Count);
Assert.StartsWith(testcookie, cookieHeaderValues[0]);
Assert.Contains("path=/", cookieHeaderValues[0]);
Assert.Contains("expires=Thu, 01 Jan 1970 00:00:00 GMT", cookieHeaderValues[0]);
}

[Fact]
public void NoParamsDeleteRemovesCookieCreatedByAdd()
{
var headers = new HeaderDictionary();
var cookies = new ResponseCookies(headers);
var testcookie = "TestCookie";

cookies.Append(testcookie, testcookie);
cookies.Delete(testcookie);

var cookieHeaderValues = headers[HeaderNames.SetCookie];
Assert.Equal(1, cookieHeaderValues.Count);
Assert.StartsWith(testcookie, cookieHeaderValues[0]);
Assert.Contains("path=/", cookieHeaderValues[0]);
Assert.Contains("expires=Thu, 01 Jan 1970 00:00:00 GMT", cookieHeaderValues[0]);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add a test where Append adds the cookie and then Delete removes it.


}
}