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));
|
// 可以进一步验证返回的数据是否符合预期
|
}
|
|
}
|
|
}
|