mac
2024-07-25 16bea1d248f0010049bceaa562939297fa26b130
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NUnit.Framework;
 
namespace NunitTestIos
{
    [TestFixture]
    public class ApiPostTests
    {
        private HttpClient client;
        private const string apiUrl = "https://api.example.com/";
 
        [SetUp]
        public void Setup()
        {
            client = new HttpClient();
            client.BaseAddress = new Uri(apiUrl);
        }
 
        [Test]
        public async Task TestPostRequest()
        {
            // 准备 POST 数据
            var postData = new { key = "value" };
            var json = JsonConvert.SerializeObject(postData);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
 
            // 发起 POST 请求
            HttpResponseMessage response = await client.PostAsync("endpoint", content);
 
            // 验证状态码
            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
 
            // 验证返回的数据或响应内容
            string responseBody = await response.Content.ReadAsStringAsync();
            Assert.IsTrue(!string.IsNullOrEmpty(responseBody));
            // 可以进一步验证返回的数据是否符合预期
        }
 
    }
 
}