You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
7.3 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Text.Unicode;
using System.Threading.Tasks;
using HttpClientStudy.Config;
using Newtonsoft.Json;
namespace HttpClientStudy.UnitTest.HttpClients
{
/// <summary>
/// 带请求体的Get请求 测试
/// </summary>
public class GetWithBodyTest
{
/// <summary>
/// 类型化客户端(静态方法)
/// </summary>
public static HttpClient GetHttpClient = new HttpClient()
{
BaseAddress = new Uri(WebApiConfigManager.GetWebApiConfig().BaseUrl),
};
/// <summary>
/// 测试日志
/// </summary>
private readonly ITestOutputHelper _logger;
/// <summary>
/// 构造
/// </summary>
public GetWithBodyTest(ITestOutputHelper outputHelper)
{
_logger = outputHelper;
}
#region Get请求中使用请求体强烈不推荐这种方法
/// <summary>
/// Get请求中使用请求体
/// 注意:服务器要设置(配置KestrelServerOptions AllowSynchronousIO值为true)
/// </summary>
/// <returns></returns>
[Fact]
public async Task GetWithBody_Test()
{
var formData = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("param1", "value1"),
new KeyValuePair<string, string>("param2", "value2")
};
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "/api/AdvancedGet/GetWithBody");
requestMessage.Content = new FormUrlEncodedContent(formData);
var response = await GetHttpClient.SendAsync(requestMessage);
response.EnsureSuccessStatusCode();
var responseText = await response.Content.ReadAsStringAsync();
_logger.WriteLine(responseText);
}
[Fact]
public async Task GetWithFormBody_Test()
{
var formData = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Id", "1"),
new KeyValuePair<string, string>("Name", "BeiJing")
};
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "/api/AdvancedGet/GetWithFormBody");
requestMessage.Content = new FormUrlEncodedContent(formData);
var response = await GetHttpClient.SendAsync(requestMessage);
response.EnsureSuccessStatusCode();
var responseText = await response.Content.ReadAsStringAsync();
_logger.WriteLine(responseText);
}
[Fact]
public async Task GetWithFormBody_MultipartFormData_Test()
{
var multiFormDataContent = new MultipartFormDataContent();
multiFormDataContent.Add(new StringContent("1", Encoding.UTF8, System.Net.Mime.MediaTypeNames.Multipart.FormData), "Id");
multiFormDataContent.Add(new StringContent("BeiJing", Encoding.UTF8, System.Net.Mime.MediaTypeNames.Multipart.FormData), "Name");
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "/api/AdvancedGet/GetWithFormBody");
requestMessage.Content = multiFormDataContent;
var response = await GetHttpClient.SendAsync(requestMessage);
response.EnsureSuccessStatusCode();
var responseText = await response.Content.ReadAsStringAsync();
_logger.WriteLine(responseText);
}
[Fact]
public async Task GetWithJsonBody_Test()
{
//因为get方法,不能使用System.Net.Http.Json中的扩展方法
var vm = new AdvancedGetModel() { Id = 3, Name = "张三" };
var content = new StringContent(JsonConvert.SerializeObject(vm), Encoding.UTF8, System.Net.Mime.MediaTypeNames.Application.Json);
var requestMessage = new HttpRequestMessage(HttpMethod.Get, "/api/AdvancedGet/GetWithJsonBody")
{
Content = content,
};
var response = await GetHttpClient.SendAsync(requestMessage);
response.EnsureSuccessStatusCode();
var responseText = await response.Content.ReadAsStringAsync();
_logger.WriteLine(responseText);
}
[Fact]
public async Task GetWithBodyByCopyStream_Test()
{
//因为get方法,不能使用System.Net.Http.Json中的扩展方法
var vm = new AdvancedGetModel() { Id = 3, Name = "张三" };
var content = new StringContent(JsonConvert.SerializeObject(vm), Encoding.UTF8, System.Net.Mime.MediaTypeNames.Application.Json);
var requestMessage = new HttpRequestMessage(HttpMethod.Get, "/api/AdvancedGet/GetWithBodyByCopyStream")
{
Content = content,
};
var response = await GetHttpClient.SendAsync(requestMessage);
response.EnsureSuccessStatusCode();
var responseText = await response.Content.ReadAsStringAsync();
_logger.WriteLine(responseText);
}
[Fact]
public async Task GetWithBodyByEnableBuffering_Test()
{
//因为get方法,不能使用System.Net.Http.Json中的扩展方法
var vm = new AdvancedGetModel() { Id = 3, Name = "张三" };
var content = new StringContent(JsonConvert.SerializeObject(vm), Encoding.UTF8, System.Net.Mime.MediaTypeNames.Application.Json);
var requestMessage = new HttpRequestMessage(HttpMethod.Get, "/api/AdvancedGet/GetWithBodyByEnableBuffering")
{
Content = content,
};
var response = await GetHttpClient.SendAsync(requestMessage);
response.EnsureSuccessStatusCode();
var responseText = await response.Content.ReadAsStringAsync();
_logger.WriteLine(responseText);
}
/// <summary>
/// Post模型绑定对比
/// </summary>
[Fact]
public async Task PostFormData_Test()
{
var formData = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Id", "1"),
new KeyValuePair<string, string>("Name", "伍佰")
};
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, "/api/AdvancedGet/PostFormData");
requestMessage.Content = new FormUrlEncodedContent(formData);
var response = await GetHttpClient.SendAsync(requestMessage);
response.EnsureSuccessStatusCode();
var responseText = await response.Content.ReadAsStringAsync();
_logger.WriteLine(responseText);
}
/// <summary>
/// Post模型绑定对比
/// </summary>
[Fact]
public async Task PostJsonData_Test()
{
var vm = new AdvancedGetModel() { Id = 2, Name = "伍佰" };
var response = await GetHttpClient.PostAsJsonAsync("/api/AdvancedGet/PostJsonData", vm);
response.EnsureSuccessStatusCode();
var responseText = await response.Content.ReadAsStringAsync();
_logger.WriteLine(responseText);
}
#endregion
}
}