From 97486adc02026b06a33411c81325feea2bb82909 Mon Sep 17 00:00:00 2001 From: Todd Date: Tue, 24 Mar 2020 20:40:17 -0500 Subject: [PATCH] #506 cookie sessions --- Test/Flurl.Test/Http/CookieTests.cs | 23 ++++++++++++++++++ src/Flurl.Http/CookieExtensions.cs | 5 ++++ src/Flurl.Http/CookieSession.cs | 37 +++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/Flurl.Http/CookieSession.cs diff --git a/Test/Flurl.Test/Http/CookieTests.cs b/Test/Flurl.Test/Http/CookieTests.cs index cc4f6ae..5688753 100644 --- a/Test/Flurl.Test/Http/CookieTests.cs +++ b/Test/Flurl.Test/Http/CookieTests.cs @@ -51,5 +51,28 @@ namespace Flurl.Test.Http Assert.AreEqual("foo", cookies["x"].Value); Assert.AreEqual("bazz", cookies["y"].Value); } + + [Test] + public async Task can_do_cookie_session() { + HttpTest + .RespondWith("hi", cookies: new { x = "foo", y = "bar" }) + .RespondWith("hi") + .RespondWith("hi", cookies: new { y = "bazz" }) + .RespondWith("hi"); + + var client = new FlurlClient("https://cookies.com"); + using (var cs = client.StartCookieSession()) { + await cs.Request().GetAsync(); + await cs.Request("1").GetAsync(); + await cs.Request("2").GetAsync(); + await cs.Request("3").GetAsync(); + + HttpTest.ShouldHaveMadeACall().WithHeader("Cookie", "x=foo; y=bar").Times(2); + HttpTest.ShouldHaveMadeACall().WithHeader("Cookie", "x=foo; y=bazz").Times(1); + Assert.AreEqual(2, cs.Cookies.Count); + Assert.AreEqual("foo", cs.Cookies["x"].Value); + Assert.AreEqual("bazz", cs.Cookies["y"].Value); + } + } } } diff --git a/src/Flurl.Http/CookieExtensions.cs b/src/Flurl.Http/CookieExtensions.cs index 707130a..05ff664 100644 --- a/src/Flurl.Http/CookieExtensions.cs +++ b/src/Flurl.Http/CookieExtensions.cs @@ -90,5 +90,10 @@ namespace Flurl.Http cookies = request.Cookies; return request; } + + /// + /// Creates a new CookieSession, under which all requests and responses share a cookie collection. + /// + public static CookieSession StartCookieSession(this IFlurlClient client) => new CookieSession(client); } } diff --git a/src/Flurl.Http/CookieSession.cs b/src/Flurl.Http/CookieSession.cs new file mode 100644 index 0000000..dd64098 --- /dev/null +++ b/src/Flurl.Http/CookieSession.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace Flurl.Http +{ + /// + /// A context where multiple requests and responses share the same cookie collection. Created using FlurlClient.StartCookieSession. + /// + public class CookieSession : IDisposable + { + private readonly IFlurlClient _client; + + internal CookieSession(IFlurlClient client) { + _client = client; + } + + /// + /// A collection of cookies sent by all requests and received by all responses within this session. + /// + public IDictionary Cookies { get; } = new Dictionary(); + + /// + /// Creates a new IFlurlRequest with this session's cookies that can be further built and sent fluently. + /// + /// The URL or URL segments for the request. + public IFlurlRequest Request(params object[] urlSegments) => _client.Request(urlSegments).WithCookies(Cookies); + + /// + /// Not necessary to call. IDisposable is implemented mainly for the syntactic sugar of using statements. + /// + public void Dispose() => Cookies.Clear(); + } +}