zer*_*ing 5 c# asp.net cookies asp.net-core-mvc
我创建了一个会话中间件,想要测试它.所以我TestServer
用于测试目的.
测试代码如下:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想用cookie提出请求,我已经知道但是不知道如何将cookie放到请求中.
我怎样才能做到这一点?
从根本上说,Cookie只是一个标题.您可以将sid值存储Set-Cookie
在字符串中,然后为每个请求添加标头:
request.Headers.Add("Cookie", new CookieHeaderValue(cookie.Name, cookie.Value).ToString());
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1669 次 |
最近记录: |