我有一个奇怪的问题。我正在尝试编写非常简单的基于 c# nunit 的 http 请求测试。 但是当我向不存在的端点发送 GET 请求时,我仍然收到 200“OK”代码? Postman 发送的请求工作正常,并在预期时给出 404。 “Users”端点存在并返回空 json(目前),Users2 emdpoint 不存在。
抱歉审查了网站地址,但我认为这与这里无关。
我在这里做错了什么?
namespace TestProject1
{
[TestFixture("OK", "Users2")]
[TestFixture("OK", "Users")]
public class Tests
{
public string Code { get; set;}
public string Url = "https://examplewebsite.net";
public Tests(string code)
{
this.Code = code;
}
public Tests(string code, string endpoint)
: this(code)
{
Url = string.Format("{0}/{1}",
Url,
endpoint);
}
public async static Task<string> HttpGetAsync(string url)
{
Console.WriteLine(url);
string content = null;
var client = new HttpClient
{
BaseAddress = new Uri(url)
};
var response = await client.GetAsync("");
if (response.IsSuccessStatusCode)
{
content = await response.Content.ReadAsStringAsync();
}
Console.WriteLine(content);
return response.StatusCode.ToString();
}
[Test]
public void Test1()
{
var task = Task.Run(() => HttpGetAsync(Url));
task.Wait();
string responseCode = task.Result;
Assert.AreEqual(Code, responseCode);
}
}
}
结果,我期望端点是基本 url + 端点,而网页的基本 url 与端点不同。