Skip to content
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

Bug fix. Response cookie with empty domain fails #2015

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/RestSharp/RestClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ public partial class RestClient {
// Parse all the cookies from the response and update the cookie jar with cookies
if (responseMessage.Headers.TryGetValues(KnownHeaders.SetCookie, out var cookiesHeader)) {
foreach (var header in cookiesHeader) {
cookieContainer.SetCookies(url, header);
try {
cookieContainer.SetCookies(url, header);
}
catch (CookieException) {
// Do not fail request if we cannot parse a cookie
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions test/RestSharp.Tests.Integrated/CookieTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,21 @@ public class CookieTests {
c.HttpOnly.Should().Be(httpOnly);
}
}

[Fact]
public async Task GET_Async_With_Response_Cookies_Should_Not_Fail_With_Cookie_With_Empty_Domain() {
var request = new RestRequest("set-cookies");
var response = await _client.ExecuteAsync(request);
response.Content.Should().Be("success");

Cookie? notFoundCookie = FindCookie("cookie_empty_domain");
notFoundCookie.Should().BeNull();

HeaderParameter? emptyDomainCookieHeader = response.Headers!
.SingleOrDefault(h => h.Name == KnownHeaders.SetCookie && ((string)h.Value!).StartsWith("cookie_empty_domain"));
emptyDomainCookieHeader.Should().NotBeNull();
((string)emptyDomainCookieHeader!.Value!).Should().Contain("domain=;");

Cookie? FindCookie(string name) => response!.Cookies!.FirstOrDefault(p => p.Name == name);
}
}
10 changes: 10 additions & 0 deletions test/RestSharp.Tests.Integrated/Server/Handlers/CookieHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public static class CookieHandlers {
HttpOnly = true
}
);

ctx.Response.Cookies.Append(
"cookie_empty_domain",
"value_empty_domain",
new CookieOptions {
HttpOnly = true,
Domain = string.Empty
}
);

return Results.Content("success");
}
}