-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HttpCookie.Value should return the URL decoded cookie value #394
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1e76943
Fix HttpCookie to return the urldecoded format when Value is called
afshinm 06b3dbf
Another testcase for urlencoded Values
afshinm 700cf13
Add StringComparison to .Contains
afshinm 2c9bc4a
tests: change testing cookie value to contain url unsafe chars
afshinm 0743fdf
tests: urldecoded set-cookie headers
afshinm 98ee975
Add ResponseHeaderTests
afshinm 14f7373
Populate the set-cookie header using SetCookieHeaderValue
afshinm d61f54d
Revert ResponseStreamTests
afshinm 08f7e3e
Cookie Value can be null
afshinm 5272034
Revert "tests: urldecoded set-cookie headers"
afshinm 63606bb
Revert "tests: change testing cookie value to contain url unsafe chars"
afshinm 321be4f
tests: add "using"s to HttpResponseMessage
afshinm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
test/Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests/ResponseHeaderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Net.Http.Headers; | ||
using Xunit; | ||
using SameSiteMode = System.Web.SameSiteMode; | ||
|
||
namespace Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests; | ||
|
||
[Collection(nameof(SelfHostedTests))] | ||
public class ResponseHeaderTests | ||
{ | ||
private readonly string ContentValue = Guid.NewGuid().ToString(); | ||
|
||
[Fact] | ||
public async Task SetCookie() | ||
{ | ||
using var result = await RunAsync(context => | ||
{ | ||
var cookie = new HttpCookie("test", ContentValue); | ||
context.Response.Cookies.Add(cookie); | ||
}); | ||
|
||
var cookies = result.Headers.GetValues(HeaderNames.SetCookie).ToList(); | ||
Assert.Single(cookies); | ||
Assert.Equal($"test={ContentValue}; path=/; samesite=lax", cookies.First()); | ||
} | ||
|
||
[Fact] | ||
public async Task SetMultipleCookie() | ||
{ | ||
// Arrange | ||
var cookie1 = new HttpCookie("test1", Guid.NewGuid().ToString()); | ||
var cookie2 = new HttpCookie("test2", Guid.NewGuid().ToString()); | ||
|
||
// Act | ||
using var result = await RunAsync(context => | ||
{ | ||
context.Response.Cookies.Add(cookie1); | ||
context.Response.Cookies.Add(cookie2); | ||
}); | ||
|
||
// Assert | ||
var cookies = result.Headers.GetValues(HeaderNames.SetCookie).ToList(); | ||
Assert.Equal(2, cookies.Count); | ||
Assert.Equal($"test1={cookie1.Value}; path=/; samesite=lax", cookies.First()); | ||
Assert.Equal($"test2={cookie2.Value}; path=/; samesite=lax", cookies.Last()); | ||
} | ||
|
||
[Fact] | ||
public async Task SetCookieWithPath() | ||
{ | ||
using var result = await RunAsync(context => | ||
{ | ||
var cookie = new HttpCookie("test", ContentValue) { Path = "/abc" }; | ||
context.Response.Cookies.Add(cookie); | ||
}); | ||
|
||
Assert.Equal($"test={ContentValue}; path=/abc; samesite=lax", result.Headers.GetValues(HeaderNames.SetCookie).First()); | ||
} | ||
|
||
[Fact] | ||
public async Task SetCookieSameSite() | ||
{ | ||
using var result = await RunAsync(context => | ||
{ | ||
var cookie = new HttpCookie("test", ContentValue) { SameSite = SameSiteMode.None }; | ||
context.Response.Cookies.Add(cookie); | ||
}); | ||
|
||
Assert.Equal($"test={ContentValue}; path=/; samesite=none", result.Headers.GetValues(HeaderNames.SetCookie).First()); | ||
} | ||
|
||
[Fact] | ||
public async Task SetCookieUrlencodedValue() | ||
{ | ||
// Arrange | ||
const string cookieValue = "hello|world"; | ||
var cookie = new HttpCookie("test", cookieValue) { SameSite = SameSiteMode.None }; | ||
|
||
// Act | ||
using var result = await RunAsync(context => | ||
{ | ||
context.Response.Cookies.Add(cookie); | ||
}); | ||
|
||
// Assert | ||
Assert.Equal($"test={cookieValue}; path=/; samesite=none", result.Headers.GetValues(HeaderNames.SetCookie).First()); | ||
} | ||
|
||
private static Task<HttpResponseMessage> RunAsync(Action<HttpContext> action, Action<IEndpointConventionBuilder>? builder = null) | ||
=> RunAsync(ctx => | ||
{ | ||
action(ctx); | ||
return Task.CompletedTask; | ||
}, builder); | ||
|
||
private static async Task<HttpResponseMessage> RunAsync(Func<HttpContext, Task> action, Action<IEndpointConventionBuilder>? builder = null) | ||
{ | ||
builder ??= _ => { }; | ||
|
||
using var host = await new HostBuilder() | ||
.ConfigureWebHost(webBuilder => | ||
{ | ||
webBuilder | ||
.UseTestServer(options => | ||
{ | ||
options.AllowSynchronousIO = true; | ||
}) | ||
.ConfigureServices(services => | ||
{ | ||
services.AddRouting(); | ||
services.AddSystemWebAdapters(); | ||
}) | ||
.Configure(app => | ||
{ | ||
app.UseRouting(); | ||
app.UseSystemWebAdapters(); | ||
app.UseEndpoints(endpoints => | ||
{ | ||
builder(endpoints.Map("/", (context) => action(context))); | ||
}); | ||
}); | ||
}) | ||
.StartAsync(); | ||
|
||
var uri = new Uri("/", UriKind.Relative); | ||
|
||
try | ||
{ | ||
return await host.GetTestClient().GetAsync(uri).ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
await host.StopAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this what system.web was doing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@twsouthwick no, System.web is doing an
.IndexOf
without theSystemComparison
argument. Do you want me to change it to IndexOf?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No this is fine -I was wondering if the rest of it was something that system.web was doing