#506 cookie sessions
This commit is contained in:
parent
9547b13eb3
commit
97486adc02
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -90,5 +90,10 @@ namespace Flurl.Http
|
||||
cookies = request.Cookies;
|
||||
return request;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new CookieSession, under which all requests and responses share a cookie collection.
|
||||
/// </summary>
|
||||
public static CookieSession StartCookieSession(this IFlurlClient client) => new CookieSession(client);
|
||||
}
|
||||
}
|
||||
|
37
src/Flurl.Http/CookieSession.cs
Normal file
37
src/Flurl.Http/CookieSession.cs
Normal file
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// A context where multiple requests and responses share the same cookie collection. Created using FlurlClient.StartCookieSession.
|
||||
/// </summary>
|
||||
public class CookieSession : IDisposable
|
||||
{
|
||||
private readonly IFlurlClient _client;
|
||||
|
||||
internal CookieSession(IFlurlClient client) {
|
||||
_client = client;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A collection of cookies sent by all requests and received by all responses within this session.
|
||||
/// </summary>
|
||||
public IDictionary<string, Cookie> Cookies { get; } = new Dictionary<string, Cookie>();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new IFlurlRequest with this session's cookies that can be further built and sent fluently.
|
||||
/// </summary>
|
||||
/// <param name="urlSegments">The URL or URL segments for the request.</param>
|
||||
public IFlurlRequest Request(params object[] urlSegments) => _client.Request(urlSegments).WithCookies(Cookies);
|
||||
|
||||
/// <summary>
|
||||
/// Not necessary to call. IDisposable is implemented mainly for the syntactic sugar of using statements.
|
||||
/// </summary>
|
||||
public void Dispose() => Cookies.Clear();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user