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

Commit f7350bf

Browse files
committed
PR comments: doc comments, a missed method, and more tests
1 parent 7e93722 commit f7350bf

File tree

4 files changed

+81
-54
lines changed

4 files changed

+81
-54
lines changed

src/Microsoft.AspNetCore.Http.Features/IResponseCookies.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,39 @@
44
namespace Microsoft.AspNetCore.Http
55
{
66
/// <summary>
7-
/// A wrapper for the response Set-Cookie header
7+
/// A wrapper for the response Set-Cookie header.
88
/// </summary>
99
public interface IResponseCookies
1010
{
1111
/// <summary>
12-
/// Add a new cookie and value
12+
/// Add a new cookie and value.
1313
/// </summary>
14-
/// <param name="key"></param>
15-
/// <param name="value"></param>
14+
/// <param name="key">Name of the new cookie.</param>
15+
/// <param name="value">Value of the new cookie.</param>
1616
void Append(string key, string value);
1717

1818
/// <summary>
19-
/// Add a new cookie
19+
/// Add a new cookie.
2020
/// </summary>
21-
/// <param name="key"></param>
22-
/// <param name="value"></param>
23-
/// <param name="options"></param>
21+
/// <param name="key">Name of the new cookie.</param>
22+
/// <param name="value">Value of the new cookie.</param>
23+
/// <param name="options"><see cref="CookieOptions"/> included in the new cookie setting.</param>
2424
void Append(string key, string value, CookieOptions options);
2525

2626
/// <summary>
27-
/// Sets an expired cookie
27+
/// Sets an expired cookie.
2828
/// </summary>
29-
/// <param name="key"></param>
29+
/// <param name="key">Name of the cookie to expire.</param>
3030
void Delete(string key);
3131

3232
/// <summary>
33-
/// Sets an expired cookie
33+
/// Sets an expired cookie.
3434
/// </summary>
35-
/// <param name="key"></param>
36-
/// <param name="options"></param>
35+
/// <param name="key">Name of the cookie to expire.</param>
36+
/// <param name="options">
37+
/// <see cref="CookieOptions"/> used to discriminate the particular cookie to expire. The
38+
/// <see cref="CookieOptions.Domain"/> and <see cref="CookieOptions.Path"/> values are especially important.
39+
/// </param>
3740
void Delete(string key, CookieOptions options);
3841
}
3942
}

src/Microsoft.AspNetCore.Http.Features/IResponseCookiesFeature.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
namespace Microsoft.AspNetCore.Http.Features
55
{
66
/// <summary>
7-
/// Feature containing cookies which will be returned with the response.
7+
/// A helper for creating the response Set-Cookie header.
88
/// </summary>
99
public interface IResponseCookiesFeature
1010
{
1111
/// <summary>
12-
/// Cookies which will be returned with the response.
12+
/// Gets the wrapper for the response Set-Cookie header.
1313
/// </summary>
1414
IResponseCookies Cookies { get; }
1515
}

src/Microsoft.AspNetCore.Http/ResponseCookies.cs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
namespace Microsoft.AspNetCore.Http.Internal
1212
{
1313
/// <summary>
14-
/// A wrapper for the response Set-Cookie header
14+
/// A wrapper for the response Set-Cookie header.
1515
/// </summary>
1616
public class ResponseCookies : IResponseCookies
1717
{
1818
private readonly ObjectPool<StringBuilder> _builderPool;
1919

2020
/// <summary>
21-
/// Create a new wrapper
21+
/// Create a new wrapper.
2222
/// </summary>
2323
/// <param name="headers">The <see cref="IHeaderDictionary"/> for the response.</param>
2424
/// <param name="builderPool">The <see cref="ObjectPool{T}"/>, if available.</param>
@@ -35,11 +35,7 @@ public ResponseCookies(IHeaderDictionary headers, ObjectPool<StringBuilder> buil
3535

3636
private IHeaderDictionary Headers { get; set; }
3737

38-
/// <summary>
39-
/// Add a new cookie and value
40-
/// </summary>
41-
/// <param name="key"></param>
42-
/// <param name="value"></param>
38+
/// <inheritdoc />
4339
public void Append(string key, string value)
4440
{
4541
var setCookieHeaderValue = new SetCookieHeaderValue(
@@ -71,12 +67,7 @@ public void Append(string key, string value)
7167
Headers[HeaderNames.SetCookie] = StringValues.Concat(Headers[HeaderNames.SetCookie], cookieValue);
7268
}
7369

74-
/// <summary>
75-
/// Add a new cookie
76-
/// </summary>
77-
/// <param name="key"></param>
78-
/// <param name="value"></param>
79-
/// <param name="options"></param>
70+
/// <inheritdoc />
8071
public void Append(string key, string value, CookieOptions options)
8172
{
8273
if (options == null)
@@ -96,34 +87,34 @@ public void Append(string key, string value, CookieOptions options)
9687
};
9788

9889
string cookieValue;
99-
var stringBuilder = _builderPool.Get();
100-
try
90+
if (_builderPool == null)
10191
{
102-
setCookieHeaderValue.AppendToStringBuilder(stringBuilder);
103-
cookieValue = stringBuilder.ToString();
92+
cookieValue = setCookieHeaderValue.ToString();
10493
}
105-
finally
94+
else
10695
{
107-
_builderPool.Return(stringBuilder);
96+
var stringBuilder = _builderPool.Get();
97+
try
98+
{
99+
setCookieHeaderValue.AppendToStringBuilder(stringBuilder);
100+
cookieValue = stringBuilder.ToString();
101+
}
102+
finally
103+
{
104+
_builderPool.Return(stringBuilder);
105+
}
108106
}
109107

110108
Headers[HeaderNames.SetCookie] = StringValues.Concat(Headers[HeaderNames.SetCookie], cookieValue);
111109
}
112110

113-
/// <summary>
114-
/// Sets an expired cookie
115-
/// </summary>
116-
/// <param name="key"></param>
111+
/// <inheritdoc />
117112
public void Delete(string key)
118113
{
119114
Delete(key, new CookieOptions() { Path = "/" });
120115
}
121116

122-
/// <summary>
123-
/// Sets an expired cookie
124-
/// </summary>
125-
/// <param name="key"></param>
126-
/// <param name="options"></param>
117+
/// <inheritdoc />
127118
public void Delete(string key, CookieOptions options)
128119
{
129120
if (options == null)

test/Microsoft.AspNetCore.Http.Tests/ResponseCookiesTest.cs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,24 @@ public class ResponseCookiesTest
1414
private static readonly ObjectPool<StringBuilder> _builderPool =
1515
new DefaultObjectPoolProvider().Create<StringBuilder>(new StringBuilderPooledObjectPolicy());
1616

17-
[Fact]
18-
public void DeleteCookieShouldSetDefaultPath()
17+
public static TheoryData BuilderPoolData
18+
{
19+
get
20+
{
21+
return new TheoryData<ObjectPool<StringBuilder>>
22+
{
23+
null,
24+
_builderPool,
25+
};
26+
}
27+
}
28+
29+
[Theory]
30+
[MemberData(nameof(BuilderPoolData))]
31+
public void DeleteCookieShouldSetDefaultPath(ObjectPool<StringBuilder> builderPool)
1932
{
2033
var headers = new HeaderDictionary();
21-
var cookies = new ResponseCookies(headers, _builderPool);
34+
var cookies = new ResponseCookies(headers, builderPool);
2235
var testcookie = "TestCookie";
2336

2437
cookies.Delete(testcookie);
@@ -30,11 +43,12 @@ public void DeleteCookieShouldSetDefaultPath()
3043
Assert.Contains("expires=Thu, 01 Jan 1970 00:00:00 GMT", cookieHeaderValues[0]);
3144
}
3245

33-
[Fact]
34-
public void NoParamsDeleteRemovesCookieCreatedByAdd()
46+
[Theory]
47+
[MemberData(nameof(BuilderPoolData))]
48+
public void NoParamsDeleteRemovesCookieCreatedByAdd(ObjectPool<StringBuilder> builderPool)
3549
{
3650
var headers = new HeaderDictionary();
37-
var cookies = new ResponseCookies(headers, _builderPool);
51+
var cookies = new ResponseCookies(headers, builderPool);
3852
var testcookie = "TestCookie";
3953

4054
cookies.Append(testcookie, testcookie);
@@ -47,14 +61,33 @@ public void NoParamsDeleteRemovesCookieCreatedByAdd()
4761
Assert.Contains("expires=Thu, 01 Jan 1970 00:00:00 GMT", cookieHeaderValues[0]);
4862
}
4963

64+
public static TheoryData EscapesKeyValuesBeforeSettingCookieData
65+
{
66+
get
67+
{
68+
// key, value, object pool, expected
69+
return new TheoryData<string, string, ObjectPool<StringBuilder>, string>
70+
{
71+
{ "key", "value", null, "key=value" },
72+
{ "key,", "!value", null, "key%2C=%21value" },
73+
{ "ke#y,", "val^ue", null, "ke%23y%2C=val%5Eue" },
74+
{ "key", "value", _builderPool, "key=value" },
75+
{ "key,", "!value", _builderPool, "key%2C=%21value" },
76+
{ "ke#y,", "val^ue", _builderPool, "ke%23y%2C=val%5Eue" },
77+
};
78+
}
79+
}
80+
5081
[Theory]
51-
[InlineData("key", "value", "key=value")]
52-
[InlineData("key,", "!value", "key%2C=%21value")]
53-
[InlineData("ke#y,", "val^ue", "ke%23y%2C=val%5Eue")]
54-
public void EscapesKeyValuesBeforeSettingCookie(string key, string value, string expected)
82+
[MemberData(nameof(EscapesKeyValuesBeforeSettingCookieData))]
83+
public void EscapesKeyValuesBeforeSettingCookie(
84+
string key,
85+
string value,
86+
ObjectPool<StringBuilder> builderPool,
87+
string expected)
5588
{
5689
var headers = new HeaderDictionary();
57-
var cookies = new ResponseCookies(headers, _builderPool);
90+
var cookies = new ResponseCookies(headers, builderPool);
5891

5992
cookies.Append(key, value);
6093

0 commit comments

Comments
 (0)