Skip to content
Merged

v109 #1963

Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Ignore exceptions parsing response cookies (#2015)
Co-authored-by: Francesc Castells <[email protected]>
  • Loading branch information
fcastells and Francesc Castells authored Mar 4, 2023
commit 1d289f988542d917b8deae6f6a5c4c716e6cd2b2
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");
}
}