.NET Core 通过XUnit进行接口单元测试(带请求头及参数)
程序员文章站
2023-01-19 14:19:47
最近在做core的接口单元测试,所以在这拿出来分享一下, 话不多说,直接上代码了: ......
最近在做core的接口单元测试,所以在这拿出来分享一下,
话不多说,直接上代码了:
[Fact] public string GetOrder() { string result = ""; string url = "http://请求地址"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; string content = "{ 'data':'','encrypt':'0'}";//添加Post 参数 #region 请求头 byte[] data = Encoding.UTF8.GetBytes(content); req.Headers.Add("HeadConten", "{ '**': '**'}"); //请求头header req.Headers.Add("Client", "{'***': '**’ }"); req.ContentType = "application/json"; req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(data, 0, data.Length); reqStream.Close(); } #endregion HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); //获取响应内容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return result;//返回json格式数据 }
上一篇: Spring Security OAuth2 SSO
下一篇: 生活用具、食品的幽默表白