#506 sync request cookies to Cookie header on demand

This commit is contained in:
Todd 2020-07-24 11:19:57 -05:00
parent fb4d0555ef
commit 331b262c46
2 changed files with 19 additions and 5 deletions

View File

@ -59,6 +59,17 @@ namespace Flurl.Test.Http
Assert.IsFalse(req.Cookies.ContainsKey("x"));
}
[Test]
public void request_cookies_sync_with_cookie_header() {
var req = new FlurlRequest("http://cookies.com").WithCookie("x", "foo");
Assert.AreEqual("x=foo", req.Headers.TryGetValue("Cookie", out var val) ? val : null);
// should flow from CookieJar too
var jar = new CookieJar().AddOrUpdate("y", "bar", "http://cookies.com");
req = new FlurlRequest("http://cookies.com").WithCookies(jar);
Assert.AreEqual("y=bar", req.Headers.TryGetValue("Cookie", out val) ? val : null);
}
[Test]
public async Task can_send_cookies_per_request_initialized() {
HttpTest

View File

@ -64,6 +64,7 @@ namespace Flurl.Http
private FlurlHttpSettings _settings;
private IFlurlClient _client;
private Url _url;
private IDictionary<string, object> _headers = new ConcurrentDictionary<string, object>();
private CookieJar _cookieJar;
private FlurlCall _redirectedFrom;
@ -138,7 +139,13 @@ namespace Flurl.Http
}
/// <inheritdoc />
public IDictionary<string, object> Headers { get; } = new ConcurrentDictionary<string, object>();
public IDictionary<string, object> Headers {
get {
if (Cookies.Any())
_headers["Cookie"] = CookieCutter.ToRequestHeader(Cookies);
return _headers;
}
}
/// <inheritdoc />
public IDictionary<string, object> Cookies { get; } = new ConcurrentDictionary<string, object>();
@ -238,10 +245,6 @@ namespace Flurl.Http
private void SyncHeadersAndCookies(HttpRequestMessage request) {
// copy any client-level (default) to FlurlRequest
Headers.Merge(Client.Headers);
//Cookies.Merge(Client.Cookies);
if (Cookies.Any())
Headers["Cookie"] = CookieCutter.ToRequestHeader(Cookies);
// copy headers from FlurlRequest to HttpRequestMessage
foreach (var header in Headers)