第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在單元測試中返回 FileContentResult 時,Http 響應(yīng)標頭解析器失敗

在單元測試中返回 FileContentResult 時,Http 響應(yīng)標頭解析器失敗

C#
紫衣仙女 2023-08-13 16:16:43
我正在嘗試為以下 AspNetCore 控制器方法編寫單元測試:[HttpGet]public async Task<IActionResult> GetFile(string id){    FileContent file = await fileRepository.GetFile(id);    if (file == null)        return NotFound();    Response.Headers.Add("Content-Disposition", file.FileName);    return File(file.File, file.ContentType);}文件內(nèi)容類:public class FileContent{    public FileContent(string fileName, string contentType, byte[] file)    {        FileName = fileName;        ContentType = contentType;        File = file;    }    public string FileName { get; }    public string ContentType { get; }    public byte[] File { get; }}這是測試初始化:[TestInitialize]public void TestInitialize(){    repositoryMock = new Mock<IFileRepository>();    controller = new FilesController(repositoryMock.Object);    var httpContext = new Mock<HttpContext>(MockBehavior.Strict);    var response = new Mock<HttpResponse>(MockBehavior.Strict);    var headers = new HeaderDictionary();    response.Setup(x => x.Headers).Returns(headers);    httpContext.SetupGet(x => x.Response).Returns(response.Object);    controller.ControllerContext = new ControllerContext(new ActionContext(httpContext.Object, new RouteData(), new ControllerActionDescriptor()));}及測試方法:[TestMethod]public async Task GetShouldReturnCorrectResponse(){    repositoryMock        .Setup(x => x.GetFile(It.IsAny<string>(), null))        .ReturnsAsync(new FileContent("test.txt", "File Content.", Encoding.UTF8.GetBytes("File Content.")));    IActionResult response = await controller.GetFile(DocumentId);    // .. some assertions}在以下控制器線路上測試失?。簉eturn File(file.File, file.ContentType);例外情況:System.FormatException:標頭在索引 0 處包含無效值:“文件內(nèi)容”。在 Microsoft.Net.Http.Headers.HttpHeaderParser`1.ParseValue(StringSegment value, Int32& index) 在 Microsoft.AspNetCore.Mvc.FileContentResult..ctor(Byte[] fileContents, String contentType) 在 Microsoft.AspNetCore.Mvc.ControllerBase。文件(字節(jié)[]文件內(nèi)容,字符串內(nèi)容類型,字符串文件下載名稱)我不明白這里出了什么問題。請指教。
查看完整描述

1 回答

?
LEATH

TA貢獻1936條經(jīng)驗 獲得超7個贊

當您向響應(yīng)添加標頭時,ASP.NET Core 將驗證已知標頭以確保它們包含有效值。在您的情況下,您嘗試將內(nèi)容類型設(shè)置為"File Content."此處:

repositoryMock
????.Setup(x?=>?x.GetFile(It.IsAny<string>(),?null))
????.ReturnsAsync(new?FileContent("test.txt",?"File?Content.",?Encoding.UTF8.GetBytes("File?Content.")));
????//????????????????????????????????????????????↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

File Content.不是有效的MIME type,因此該值的驗證失敗。

相反,您應(yīng)該使用實際的 MIME 類型,例如,text/plain因為您的測試中還有純文本內(nèi)容:

repositoryMock
????.Setup(x?=>?x.GetFile(It.IsAny<string>(),?null))
????.ReturnsAsync(new?FileContent("test.txt",?"text/plain",?Encoding.UTF8.GetBytes("File?Content.")));



查看完整回答
反對 回復(fù) 2023-08-13
  • 1 回答
  • 0 關(guān)注
  • 123 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號