我創建會話中間件並想測試它。所以我使用TestServer
進行測試。ASP.NET Core .NET Framework使用Cookie進行測試
測試代碼看起來如下:
using System.Linq;
using System.Threading.Tasks;
using ComponentsTest.StartUps;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using NUnit.Framework;
namespace ComponentsTest.IntegrationTest
{
[TestFixture]
public class SessionMwTest
{
[SetUp]
public void Setup()
{
_server = new TestServer(_hostBuilder);
}
private readonly IWebHostBuilder _hostBuilder = new WebHostBuilder().UseStartup<StartUpSession>();
private TestServer _server;
[Test]
public async Task BrowserRequestForCookies_SeveralRequest_ExpectToken()
{
var client = _server.CreateClient();
var req1 = await client.GetAsync("/");
var sid = (from r in req1.Headers
where r.Key == "Set-Cookie"
from h in r.Value
where h.Contains("sid")
select h).FirstOrDefault();
StringAssert.Contains("sid", sid);
}
}
}
我想和cookie的請求,我已經得到了,但不知道如何把cookie的請求。
我該怎麼做?
非常感謝@nemec。 –