#602 cookie received from redirect response should be added to cookiejar

This commit is contained in:
tranb3r 2021-03-03 13:47:45 +01:00
parent 188999bca4
commit 8c94c19101
3 changed files with 26 additions and 1 deletions

View File

@ -439,6 +439,19 @@ namespace Flurl.Test.Http
CollectionAssert.AreEquivalent(new[] { "3", "2" }, jar.Select(c => c.Value));
}
[Test]
public async Task cookie_received_from_redirect_response_is_added_to_jar() {
HttpTest
.RespondWith("redir", 302, new { Location = "/redir1" }, cookies: new { x = "foo" })
.RespondWith("hi", cookies: new { y = "bar" });
await "https://cookies.com".WithCookies(out var jar).GetAsync();
Assert.AreEqual(2, jar.Count);
Assert.AreEqual(1, jar.Count(c => c.Name == "x" && c.Value == "foo"));
Assert.AreEqual(1, jar.Count(c => c.Name == "y" && c.Value == "bar"));
}
/// <summary>
/// Performs a series of behavioral checks against a cookie based on its state. Used by lots of tests to make them more robust.
/// </summary>

View File

@ -340,6 +340,15 @@ namespace Flurl.Test.Http
Assert.AreEqual("123", resp1.cookies.x);
Assert.AreEqual("abc", resp2.cookies.x);
}
[Test]
public async Task can_receive_cookie_from_redirect_response_and_add_it_to_jar() {
// use httpbingo instead of httpbin because of redirect issue https://github.com/postmanlabs/httpbin/issues/617
var resp = await "https://httpbingo.org/redirect-to".SetQueryParam("url", "/cookies/set?x=foo").WithCookies(out var jar).GetJsonAsync();
Assert.AreEqual("foo", resp.x);
Assert.AreEqual(1, jar.Count);
}
#endregion
}
}

View File

@ -248,7 +248,10 @@ namespace Flurl.Http
redir.Client = Client;
redir._redirectedFrom = call;
redir.Settings.Defaults = Settings;
redir.WithHeaders(this.Headers).WithCookies(call.Response.Cookies);
redir.WithHeaders(this.Headers);
if (CookieJar != null) {
redir.WithCookies(CookieJar);
}
var changeToGet = call.Redirect.ChangeVerbToGet;