上传PDF文件方法Xunit测试和错误是(不支持的媒体类型)

发布于 2025-01-21 10:45:12 字数 1309 浏览 1 评论 0原文

这是方法:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文