Skip to content

Commit

Permalink
Ignore exceptions parsing response cookies (#2015)
Browse files Browse the repository at this point in the history
Co-authored-by: Francesc Castells <francesc.castells@sermo.com>
  • Loading branch information
fcastells and Francesc Castells committed Mar 4, 2023
1 parent 3a9fe6d commit 1d289f9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
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");
}
}

0 comments on commit 1d289f9

Please sign in to comment.