#506 shoring up cookie tests

This commit is contained in:
Todd 2020-07-24 14:27:17 -05:00
parent 331b262c46
commit 6897af2216
6 changed files with 91 additions and 58 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Flurl.Http;
using NUnit.Framework;
@ -12,7 +13,31 @@ namespace Flurl.Test.Http
public class CookieTests : HttpTestFixtureBase
{
[Test]
public async Task can_send_cookies_per_request() {
public async Task can_send_and_receive_cookies_per_request() {
HttpTest
.RespondWith("hi", cookies: new { x = "bar" })
.RespondWith("hi")
.RespondWith("hi");
// explicitly reuse client to be extra certain we're NOT maintaining cookie state between calls.
var cli = new FlurlClient("https://cookies.com");
var responses = new[] {
await cli.Request().WithCookie("x", "foo").GetAsync(),
await cli.Request().WithCookies(new { y = "bar", z = "fizz" }).GetAsync(),
await cli.Request().GetAsync()
};
HttpTest.ShouldHaveMadeACall().WithCookies(new { x = "foo" }).Times(1);
HttpTest.ShouldHaveMadeACall().WithCookies(new { y = "bar", z = "fizz" }).Times(1);
HttpTest.ShouldHaveMadeACall().WithoutCookies().Times(1);
Assert.AreEqual("bar", responses[0].Cookies.TryGetValue("x", out var c) ? c.Value : null);
Assert.IsEmpty(responses[1].Cookies);
Assert.IsEmpty(responses[2].Cookies);
}
[Test]
public async Task can_send_and_receive_cookies_with_jar() {
HttpTest
.RespondWith("hi", cookies: new { x = "foo", y = "bar" })
.RespondWith("hi")
@ -20,10 +45,10 @@ namespace Flurl.Test.Http
.RespondWith("hi");
var responses = new[] {
await "https://cookies.com".WithCookies(out var cookies).GetAsync(),
await "https://cookies.com/1".WithCookies(cookies).GetAsync(),
await "https://cookies.com".WithCookies(cookies).GetAsync(),
await "https://cookies.com/2".WithCookies(cookies).GetAsync()
await "https://cookies.com".WithCookies(out var jar).GetAsync(),
await "https://cookies.com/1".WithCookies(jar).GetAsync(),
await "https://cookies.com".WithCookies(jar).GetAsync(),
await "https://cookies.com/2".WithCookies(jar).GetAsync()
};
Assert.AreEqual(2, responses[0].Cookies.Count);
@ -34,65 +59,34 @@ namespace Flurl.Test.Http
HttpTest.ShouldHaveMadeACall().WithCookies(new { x = "foo", y = "bar" }).Times(2);
HttpTest.ShouldHaveMadeACall().WithCookies(new { x = "foo", y = "bazz" }).Times(1);
Assert.AreEqual(2, cookies.Count);
Assert.AreEqual("foo", cookies["x"].Value);
Assert.AreEqual("bazz", cookies["y"].Value);
Assert.AreEqual(2, jar.Count);
Assert.AreEqual("foo", jar["x"].Value);
Assert.AreEqual("bazz", jar["y"].Value);
}
[Test]
public void jar_syncs_with_request_cookies() {
var jar = new CookieJar();
jar.AddOrUpdate("x", "foo", "https://cookies.com");
var req = new FlurlRequest("http://cookies.com").WithCookies(jar);
Assert.IsTrue(req.Cookies.ContainsKey("x"));
Assert.AreEqual("foo", req.Cookies["x"]);
jar["x"].Value = "new val!";
Assert.AreEqual("new val!", req.Cookies["x"]);
jar.AddOrUpdate("y", "bar", "https://cookies.com");
Assert.IsTrue(req.Cookies.ContainsKey("y"));
Assert.AreEqual("bar", req.Cookies["y"]);
jar["x"].Secure = true;
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() {
public async Task can_send_and_receive_cookies_with_jar_initialized() {
HttpTest
.RespondWith("hi")
.RespondWith("hi")
.RespondWith("hi", cookies: new { y = "bazz" })
.RespondWith("hi");
var cookies = new CookieJar()
var jar = new CookieJar()
.AddOrUpdate("x", "foo", "https://cookies.com")
.AddOrUpdate("y", "bar", "https://cookies.com");
await "https://cookies.com".WithCookies(cookies).GetAsync();
await "https://cookies.com/1".WithCookies(cookies).GetAsync();
await "https://cookies.com".WithCookies(cookies).GetAsync();
await "https://cookies.com/2".WithCookies(cookies).GetAsync();
await "https://cookies.com".WithCookies(jar).GetAsync();
await "https://cookies.com/1".WithCookies(jar).GetAsync();
await "https://cookies.com".WithCookies(jar).GetAsync();
await "https://cookies.com/2".WithCookies(jar).GetAsync();
HttpTest.ShouldHaveMadeACall().WithCookies(new { x = "foo", y = "bar" }).Times(3);
HttpTest.ShouldHaveMadeACall().WithCookies(new { x = "foo", y = "bazz" }).Times(1);
Assert.AreEqual(2, cookies.Count);
Assert.AreEqual("foo", cookies["x"].Value);
Assert.AreEqual("bazz", cookies["y"].Value);
Assert.AreEqual(2, jar.Count);
Assert.AreEqual("foo", jar["x"].Value);
Assert.AreEqual("bazz", jar["y"].Value);
}
[Test]
@ -120,6 +114,55 @@ namespace Flurl.Test.Http
}
}
[Test]
public void jar_syncs_with_request_cookies() {
var jar = new CookieJar().AddOrUpdate("x", "foo", "https://cookies.com");
var req = new FlurlRequest("http://cookies.com").WithCookies(jar);
Assert.IsTrue(req.Cookies.ContainsKey("x"));
Assert.AreEqual("foo", req.Cookies["x"]);
jar["x"].Value = "new val!";
Assert.AreEqual("new val!", req.Cookies["x"]);
jar.AddOrUpdate("y", "bar", "https://cookies.com");
Assert.IsTrue(req.Cookies.ContainsKey("y"));
Assert.AreEqual("bar", req.Cookies["y"]);
jar["x"].Secure = true;
Assert.IsFalse(req.Cookies.ContainsKey("x"));
}
[Test]
public async Task request_cookies_do_not_sync_to_jar() {
var jar = new CookieJar().AddOrUpdate("x", "foo", "https://cookies.com");
var req = new FlurlRequest("http://cookies.com").WithCookies(jar);
Assert.IsTrue(req.Cookies.ContainsKey("x"));
Assert.AreEqual("foo", req.Cookies["x"]);
// changing cookie at request level shouldn't touch jar
req.Cookies["x"] = "bar";
Assert.AreEqual("foo", jar["x"].Value);
await req.GetAsync();
HttpTest.ShouldHaveMadeACall().WithCookies(new { x = "bar" });
// re-check after send
Assert.AreEqual("foo", jar["x"].Value);
}
[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);
}
[TestCase("https://domain1.com", "https://domain1.com", true)]
[TestCase("https://domain1.com", "https://domain1.com/path", true)]
[TestCase("https://domain1.com", "https://www.domain1.com", false)]

View File

@ -1,6 +1,4 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

View File

@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Flurl.Http;

View File

@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Flurl.Http;

View File

@ -1,5 +1,4 @@
using System;
using System.Net;
using System.Net.Http;
using Flurl.Http.Content;

View File

@ -1,12 +1,9 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Linq;
using Flurl.Http.Configuration;
using Flurl.Http.Testing;
using Flurl.Util;
namespace Flurl.Http
{