|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
|
namespace HttpClientStudy.UnitTest
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 基础HttpClient 测试
|
|
|
/// </summary>
|
|
|
public class NormalHttpClientTest
|
|
|
{
|
|
|
private readonly ITestOutputHelper _logger;
|
|
|
|
|
|
public NormalHttpClientTest(ITestOutputHelper outputHelper)
|
|
|
{
|
|
|
_logger = outputHelper;
|
|
|
}
|
|
|
|
|
|
#region Get请求
|
|
|
|
|
|
[Fact]
|
|
|
public async Task GetAllAccounts_Test()
|
|
|
{
|
|
|
HttpClient httpClient = new HttpClient()
|
|
|
{
|
|
|
BaseAddress = new Uri(TestConfig.WebApiBaseUrl),
|
|
|
};
|
|
|
|
|
|
var responseMessage = await httpClient.GetAsync("/api/Normal/GetAllAccounts");
|
|
|
|
|
|
responseMessage.EnsureSuccessStatusCode();
|
|
|
|
|
|
Assert.NotNull(responseMessage);
|
|
|
Assert.True(responseMessage.IsSuccessStatusCode);
|
|
|
|
|
|
var result = await responseMessage.Content.ReadFromJsonAsync<BaseResult<List<Account>>>();
|
|
|
Assert.IsType<BaseResult<List<Account>>>(result);
|
|
|
Assert.Equal(1, result.Code);
|
|
|
Assert.NotNull(result.Data);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// GET 请求
|
|
|
/// (默认参数方式:url传参)
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
[Fact]
|
|
|
public async Task GetAccount_Id_Test()
|
|
|
{
|
|
|
HttpClient httpClient = new HttpClient()
|
|
|
{
|
|
|
BaseAddress = new Uri(TestConfig.WebApiBaseUrl),
|
|
|
};
|
|
|
|
|
|
var responseMessage = await httpClient.GetAsync("/api/Normal/GetAccount?Id=2");
|
|
|
|
|
|
responseMessage.EnsureSuccessStatusCode();
|
|
|
|
|
|
Assert.NotNull(responseMessage);
|
|
|
Assert.True(responseMessage.IsSuccessStatusCode);
|
|
|
|
|
|
var result = await responseMessage.Content.ReadFromJsonAsync<BaseResult<Account>>();
|
|
|
Assert.IsType<BaseResult<Account>>(result);
|
|
|
Assert.Equal(1, result.Code);
|
|
|
Assert.Equal(2, result.Data?.Id);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// GET 请求
|
|
|
/// (路由传参)
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
[Fact]
|
|
|
public async Task GetAccount_Route_Test()
|
|
|
{
|
|
|
HttpClient httpClient = new HttpClient()
|
|
|
{
|
|
|
BaseAddress = new Uri(TestConfig.WebApiBaseUrl),
|
|
|
};
|
|
|
|
|
|
var responseMessage = await httpClient.GetAsync("/api/Normal/GetAccount/管理员02");
|
|
|
|
|
|
responseMessage.EnsureSuccessStatusCode();
|
|
|
|
|
|
Assert.NotNull(responseMessage);
|
|
|
Assert.True(responseMessage.IsSuccessStatusCode);
|
|
|
|
|
|
var result = await responseMessage.Content.ReadFromJsonAsync<BaseResult<Account>>();
|
|
|
Assert.IsType<BaseResult<Account>>(result);
|
|
|
Assert.Equal(1, result.Code);
|
|
|
Assert.Equal("管理员02", result.Data?.Name);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// GET 请求
|
|
|
/// (中文参数:现在浏览器直接支持,为最大兼容老旧浏览器使用url编码)
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
[Fact]
|
|
|
public async Task GetAccountByName_Test()
|
|
|
{
|
|
|
HttpClient httpClient = new HttpClient()
|
|
|
{
|
|
|
BaseAddress = new Uri(TestConfig.WebApiBaseUrl),
|
|
|
};
|
|
|
|
|
|
var ChineseName = System.Net.WebUtility.UrlEncode("管理员01");
|
|
|
var ChineseName2 = System.Net.WebUtility.UrlEncode("管理员02");
|
|
|
|
|
|
//现在浏览器直接支持中文参数
|
|
|
//var responseMessage = await httpClient.GetAsync("/api/Normal/GetAccountByName?ChineseName=管理员01&ChineseName2=管理员02");
|
|
|
|
|
|
//兼容老浏览器写法
|
|
|
var responseMessage = await httpClient.GetAsync($"/api/Normal/GetAccountByName?ChineseName={ChineseName}&ChineseName2={ChineseName2}");
|
|
|
|
|
|
responseMessage.EnsureSuccessStatusCode();
|
|
|
|
|
|
Assert.NotNull(responseMessage);
|
|
|
Assert.True(responseMessage.IsSuccessStatusCode);
|
|
|
|
|
|
var result = await responseMessage.Content.ReadFromJsonAsync<BaseResult<List<Account>>>();
|
|
|
Assert.IsType<BaseResult<List<Account>>>(result);
|
|
|
Assert.Equal(1, result.Code);
|
|
|
Assert.Equal(2, result.Data?.Count);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// GET 请求
|
|
|
/// (查询参数传参)
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
[Fact]
|
|
|
public async Task GetAccountByRole_Test()
|
|
|
{
|
|
|
HttpClient httpClient = new HttpClient()
|
|
|
{
|
|
|
BaseAddress = new Uri(TestConfig.WebApiBaseUrl),
|
|
|
};
|
|
|
|
|
|
var responseMessage = await httpClient.GetAsync("/api/Normal/GetAccountByRole?role=Admin");
|
|
|
|
|
|
responseMessage.EnsureSuccessStatusCode();
|
|
|
|
|
|
Assert.NotNull(responseMessage);
|
|
|
Assert.True(responseMessage.IsSuccessStatusCode);
|
|
|
|
|
|
var result = await responseMessage.Content.ReadFromJsonAsync<BaseResult<List<Account>>>();
|
|
|
Assert.IsType<BaseResult<List<Account>>>(result);
|
|
|
Assert.Equal(1, result.Code);
|
|
|
//每项的角色都是查询的角色
|
|
|
Assert.False(result.Data?.Any(d=>d.Role != "Admin"));
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// GET 请求
|
|
|
/// (请求头传参:中文必须Url编码,服务端Url解码后使用)
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
[Fact]
|
|
|
public async Task GetAccountFromHeader_Test()
|
|
|
{
|
|
|
HttpClient httpClient = new HttpClient()
|
|
|
{
|
|
|
BaseAddress = new Uri(TestConfig.WebApiBaseUrl),
|
|
|
};
|
|
|
|
|
|
//中文参数必须先编码
|
|
|
httpClient.DefaultRequestHeaders.Add("name", System.Net.WebUtility.UrlEncode("管理员01"));
|
|
|
var responseMessage = await httpClient.GetAsync("/api/Normal/GetAccountFromHeader");
|
|
|
|
|
|
responseMessage.EnsureSuccessStatusCode();
|
|
|
|
|
|
Assert.NotNull(responseMessage);
|
|
|
Assert.True(responseMessage.IsSuccessStatusCode);
|
|
|
|
|
|
var result = await responseMessage.Content.ReadFromJsonAsync<BaseResult<Account>>();
|
|
|
Assert.IsType<BaseResult<Account>>(result);
|
|
|
Assert.Equal(1, result.Code);
|
|
|
Assert.Equal(1,result.Data?.Id);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// GET 请求
|
|
|
/// (客户端不传参,服务端参数直接来由IoC中注入的服务)
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
[Fact]
|
|
|
public async Task GetAccountByService_Test()
|
|
|
{
|
|
|
HttpClient httpClient = new HttpClient()
|
|
|
{
|
|
|
BaseAddress = new Uri(TestConfig.WebApiBaseUrl),
|
|
|
};
|
|
|
|
|
|
var responseMessage = await httpClient.GetAsync("/api/Normal/GetAccountFromServices");
|
|
|
|
|
|
responseMessage.EnsureSuccessStatusCode();
|
|
|
|
|
|
Assert.NotNull(responseMessage);
|
|
|
Assert.True(responseMessage.IsSuccessStatusCode);
|
|
|
|
|
|
var result = await responseMessage.Content.ReadFromJsonAsync<BaseResult<Account>>();
|
|
|
Assert.IsType<BaseResult<Account>>(result);
|
|
|
Assert.Equal(1, result.Code);
|
|
|
Assert.Equal("服务注入示例",result.Data?.Name);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region PUT 请求
|
|
|
/// <summary>
|
|
|
/// Put 请求
|
|
|
/// (新建或替换、更新资源)
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
[Fact]
|
|
|
public async Task PutDemo_Test()
|
|
|
{
|
|
|
HttpClient httpClient = new HttpClient()
|
|
|
{
|
|
|
BaseAddress = new Uri(TestConfig.WebApiBaseUrl),
|
|
|
};
|
|
|
|
|
|
var responseMessage = await httpClient.PutAsync("/api/Normal/PutDemo?id=2",null);
|
|
|
|
|
|
responseMessage.EnsureSuccessStatusCode();
|
|
|
|
|
|
Assert.NotNull(responseMessage);
|
|
|
Assert.True(responseMessage.IsSuccessStatusCode);
|
|
|
|
|
|
var result = await responseMessage.Content.ReadAsStringAsync();
|
|
|
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region HEAD 请求
|
|
|
/// <summary>
|
|
|
/// Head 请求
|
|
|
/// 注意:HttpClient没有直接提供HEAD请求方法:
|
|
|
/// 步骤1:构建 HttpRequestMessage 对象,设置HttpMethod.Head
|
|
|
/// 步骤2:把上步构建的HttpRequestMessage 对象,作为参数传递给 HttpClient实例的 Send 或 SendAsync 方法
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
[Fact]
|
|
|
public async Task HeadDemo_Test()
|
|
|
{
|
|
|
HttpClient httpClient = new HttpClient()
|
|
|
{
|
|
|
BaseAddress = new Uri(TestConfig.WebApiBaseUrl),
|
|
|
};
|
|
|
|
|
|
var requestMessage = new HttpRequestMessage(HttpMethod.Head, "/api/Normal/HeadDemo?Id=2");
|
|
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
|
|
responseMessage.EnsureSuccessStatusCode();
|
|
|
|
|
|
Assert.NotNull(responseMessage);
|
|
|
Assert.True(responseMessage.IsSuccessStatusCode);
|
|
|
|
|
|
//head请求没有返回体
|
|
|
var result = await responseMessage.Content.ReadAsStringAsync();
|
|
|
Assert.Empty(result);
|
|
|
}
|
|
|
#endregion
|
|
|
}
|
|
|
}
|