上传PDF文件方法Xunit测试和错误是(不支持的媒体类型)
这是方法:
[Fact]
public async Task UploadPermitFileAsync_ShouldWork()
{
var stream = File.OpenRead(@"D:\Repos\administrationservice\AdministrationService.Tests\Test.pdf");
var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name))
{
Headers = new HeaderDictionary(),
ContentType = "application/pdf"
};
await GovRequest<FormFile, string> (file, HttpMethod.Post, "TaskController/Upload");
}
私有异步任务govrequest&lt; m,t&gt;(m型号,httpmethod httpmethod,string apiurl) { var data = jsonconvert.serializeObject(Model);
var httpContent = new StringContent(data, Encoding.UTF8, "application/json");
var request = new HttpRequestMessage(httpMethod, $"https://localhost:44365/{apiUrl}")
{
Content = httpContent
};
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _tokenApiModel.Result.AccessToken);
var response = await _client.SendAsync(request);
var responseBody = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<SuccessRespondModel<T>>(responseBody);
Assert.True(result.Success);
}
This is Method :
[Fact]
public async Task UploadPermitFileAsync_ShouldWork()
{
var stream = File.OpenRead(@"D:\Repos\administrationservice\AdministrationService.Tests\Test.pdf");
var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name))
{
Headers = new HeaderDictionary(),
ContentType = "application/pdf"
};
await GovRequest<FormFile, string> (file, HttpMethod.Post, "TaskController/Upload");
}
private async Task GovRequest<M, T>(M model, HttpMethod httpMethod, string apiUrl)
{
var data = JsonConvert.SerializeObject(model);
var httpContent = new StringContent(data, Encoding.UTF8, "application/json");
var request = new HttpRequestMessage(httpMethod, quot;https://localhost:44365/{apiUrl}")
{
Content = httpContent
};
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _tokenApiModel.Result.AccessToken);
var response = await _client.SendAsync(request);
var responseBody = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<SuccessRespondModel<T>>(responseBody);
Assert.True(result.Success);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论