Skip to content
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
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 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo
// 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 @@ void AssertCookie(string name, string value, Func<DateTime, bool> checkExpiratio
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 IResult HandleSetCookies(HttpContext ctx) {
HttpOnly = true
}
);

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

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