Skip to content

Commit 1d289f9

Browse files
fcastellsFrancesc Castells
andauthored
Ignore exceptions parsing response cookies (#2015)
Co-authored-by: Francesc Castells <[email protected]>
1 parent 3a9fe6d commit 1d289f9

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/RestSharp/RestClient.Async.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo
119119
// Parse all the cookies from the response and update the cookie jar with cookies
120120
if (responseMessage.Headers.TryGetValues(KnownHeaders.SetCookie, out var cookiesHeader)) {
121121
foreach (var header in cookiesHeader) {
122-
cookieContainer.SetCookies(url, header);
122+
try {
123+
cookieContainer.SetCookies(url, header);
124+
}
125+
catch (CookieException) {
126+
// Do not fail request if we cannot parse a cookie
127+
}
123128
}
124129
}
125130

test/RestSharp.Tests.Integrated/CookieTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,21 @@ void AssertCookie(string name, string value, Func<DateTime, bool> checkExpiratio
4848
c.HttpOnly.Should().Be(httpOnly);
4949
}
5050
}
51+
52+
[Fact]
53+
public async Task GET_Async_With_Response_Cookies_Should_Not_Fail_With_Cookie_With_Empty_Domain() {
54+
var request = new RestRequest("set-cookies");
55+
var response = await _client.ExecuteAsync(request);
56+
response.Content.Should().Be("success");
57+
58+
Cookie? notFoundCookie = FindCookie("cookie_empty_domain");
59+
notFoundCookie.Should().BeNull();
60+
61+
HeaderParameter? emptyDomainCookieHeader = response.Headers!
62+
.SingleOrDefault(h => h.Name == KnownHeaders.SetCookie && ((string)h.Value!).StartsWith("cookie_empty_domain"));
63+
emptyDomainCookieHeader.Should().NotBeNull();
64+
((string)emptyDomainCookieHeader!.Value!).Should().Contain("domain=;");
65+
66+
Cookie? FindCookie(string name) => response!.Cookies!.FirstOrDefault(p => p.Name == name);
67+
}
5168
}

test/RestSharp.Tests.Integrated/Server/Handlers/CookieHandlers.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ public static IResult HandleSetCookies(HttpContext ctx) {
5555
HttpOnly = true
5656
}
5757
);
58+
59+
ctx.Response.Cookies.Append(
60+
"cookie_empty_domain",
61+
"value_empty_domain",
62+
new CookieOptions {
63+
HttpOnly = true,
64+
Domain = string.Empty
65+
}
66+
);
67+
5868
return Results.Content("success");
5969
}
6070
}

0 commit comments

Comments
 (0)