Skip to content

Commit

Permalink
Fix content-type for ilivalidator plain text log
Browse files Browse the repository at this point in the history
  • Loading branch information
flenny committed Apr 29, 2024
1 parent 76b32bc commit e14e01d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/ILICheck.Web/Controllers/DownloadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Mime;
using System.Web;

namespace ILICheck.Web.Controllers
Expand Down Expand Up @@ -40,8 +41,14 @@ public IActionResult Download(Guid jobId, LogType logType)
try
{
logger.LogInformation("Log file (<{LogType}>) for job identifier <{JobId}> requested.", HttpUtility.HtmlEncode(logType), jobId);
var contentType = logType == LogType.GeoJson ? "application/geo+json" : "text/xml; charset=utf-8";
return File(fileProvider.OpenText(fileProvider.GetLogFile(logType)).BaseStream, contentType);
var fileStream = fileProvider.OpenText(fileProvider.GetLogFile(logType)).BaseStream;
return logType switch
{
LogType.Log => File(fileStream, MediaTypeNames.Text.Plain),
LogType.Xtf => File(fileStream, "text/xml; charset=utf-8"),
LogType.GeoJson => File(fileStream, "application/geo+json"),
_ => throw new NotSupportedException($"Log type <{logType}> is not supported."),
};
}
catch (Exception)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ILICheck.Web/PhysicalFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public FileStream CreateFile(string file)
}

/// <inheritdoc/>
public StreamReader OpenText(string file)
public virtual StreamReader OpenText(string file)
{
if (!initialized) throw new InvalidOperationException("The file provider needs to be initialized first.");
return File.OpenText(Path.Combine(HomeDirectory.FullName, file));
Expand Down
57 changes: 57 additions & 0 deletions tests/ILICheck.Web.Test/DownloadControllerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.IO;

namespace ILICheck.Web.Controllers
{
[TestClass]
public sealed class DownloadControllerTest
{
private readonly Guid jobId = Guid.Parse("4dcc790d-541a-4d34-bf91-bc9c59360fee");

private Mock<ILogger<DownloadController>> loggerMock;
private Mock<IFileProvider> fileProviderMock;
private DownloadController controller;

public TestContext TestContext { get; set; }

[TestInitialize]
public void Initialize()
{
loggerMock = new Mock<ILogger<DownloadController>>();
fileProviderMock = new Mock<IFileProvider>(MockBehavior.Strict);

controller = new DownloadController(loggerMock.Object, fileProviderMock.Object);
}

[TestCleanup]
public void Cleanup()
{
loggerMock.VerifyAll();
fileProviderMock.VerifyAll();

controller.Dispose();
}

[TestMethod]
public void Download()
{
fileProviderMock.Setup(x => x.Initialize(jobId));
fileProviderMock.Setup(x => x.GetFiles()).Returns(new[] { "OCEANSTEED_log.xtf", "DARKFOOT_log.log", "HAPPYPOINT_log.geojson" });
fileProviderMock.Setup(x => x.OpenText(It.IsAny<string>())).Returns(StreamReader.Null);

void AssertContentType(LogType logType, string contentType)
{
var result = controller.Download(jobId, logType) as FileStreamResult;
Assert.AreEqual(result.ContentType, contentType);
}

AssertContentType(LogType.Log, "text/plain");
AssertContentType(LogType.Xtf, "text/xml; charset=utf-8");
AssertContentType(LogType.GeoJson, "application/geo+json");
}
}
}
3 changes: 3 additions & 0 deletions tests/ILICheck.Web.Test/ValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public async Task ValidateXmlAsyncForEmpty()
public async Task ValidateXmlAsyncForInvalid()
{
validatorMock.SetupGet(x => x.TransferFile).Returns("invalid.xtf");
fileProviderMock.Setup(x => x.OpenText("invalid.xtf")).CallBase();
await validatorMock.Object.ValidateXmlAsync().ConfigureAwait(false);
}

Expand All @@ -72,6 +73,7 @@ public async Task ValidateXmlAsyncForInvalid()
public async Task ValidateXmlAsyncForFileNotFound()
{
validatorMock.SetupGet(x => x.TransferFile).Returns("unavailable.xtf");
fileProviderMock.Setup(x => x.OpenText("unavailable.xtf")).CallBase();
await validatorMock.Object.ValidateXmlAsync().ConfigureAwait(false);
}

Expand All @@ -80,6 +82,7 @@ public async Task ValidateXmlAsyncForFileNotFound()
public async Task ValidateXmlAsync()
{
validatorMock.SetupGet(x => x.TransferFile).Returns("example.xtf");
fileProviderMock.Setup(x => x.OpenText("example.xtf")).CallBase();
await validatorMock.Object.ValidateXmlAsync().ConfigureAwait(false);
}

Expand Down

0 comments on commit e14e01d

Please sign in to comment.