Skip to content

Commit

Permalink
Fix a problem with missing html file (#1933)
Browse files Browse the repository at this point in the history
* Fix 404

* Added relevant middleware, added tests

* Small fixes

* Typo...

* More small fixes
  • Loading branch information
HarelM authored Jul 30, 2023
1 parent 58dbe69 commit adc37ef
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
using Microsoft.AspNetCore.Http;
using Wangkanai.Detection.Services;

namespace IsraelHiking.API.Services
namespace IsraelHiking.API.Services.Middleware
{
/// <summary>
/// This middleware is responsible in returning the index.html file or a simple page with info for the crawlers
/// This middleware is responsible in returning a simple page with info for the crawlers
/// </summary>
public class CrawlersMiddleware
{
Expand Down
42 changes: 42 additions & 0 deletions IsraelHiking.API/Services/Middleware/SpaDefaultHtmlMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;

namespace IsraelHiking.API.Services.Middleware;

/// <summary>
/// This middleware is responsible in returning the index.html file
/// </summary>
public class SpaDefaultHtmlMiddleware
{
private readonly RequestDelegate _next;
private readonly IWebHostEnvironment _environment;

/// <summary>
/// Constructor
/// </summary>
/// <param name="next"></param>
/// <param name="environment"></param>
public SpaDefaultHtmlMiddleware(RequestDelegate next,
IWebHostEnvironment environment)
{
_next = next;
_environment = environment;
}
/// <summary>
/// Main middleware method required for asp.net
/// </summary>
/// <param name="context"></param>
public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Path.StartsWithSegments("/api"))
{
await _next.Invoke(context);
return;
}
var indexFileInfo = _environment.WebRootFileProvider.GetFileInfo("/index.html");
context.Response.ContentType = "text/html";
context.Response.ContentLength = indexFileInfo.Length;
await context.Response.SendFileAsync(indexFileInfo);
}
}
7 changes: 4 additions & 3 deletions IsraelHiking.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using IsraelHiking.API;
using IsraelHiking.API.Services;
using IsraelHiking.API.Services.Middleware;
using IsraelHiking.API.Swagger;
using IsraelHiking.Common.Configuration;
using IsraelHiking.Common.Extensions;
Expand Down Expand Up @@ -54,17 +55,17 @@ void SetupApplication(WebApplication app)
{
ContentTypeProvider = new FileExtensionContentTypeProvider
{
Mappings = { {".pbf", "application/x-protobuf"} } // for the fonts files
Mappings = { { ".pbf", "application/x-protobuf" } } // for the fonts files
}
});
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Israel Hiking Map API V1");
});
// This should be the last middleware
app.UseMiddleware<CrawlersMiddleware>();
app.MapFallbackToFile("index.html");
// This should be the last middleware
app.UseMiddleware<SpaDefaultHtmlMiddleware>();
InitializeServices(app.Services);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using IsraelHiking.API.Services;
using System;
using System.IO;
using System.Text;
using IsraelHiking.API.Services;
using IsraelHiking.API.Services.Middleware;
using IsraelHiking.API.Services.Poi;
using IsraelHiking.Common;
using IsraelHiking.Common.Configuration;
Expand All @@ -9,12 +13,9 @@
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
using NSubstitute;
using System;
using System.IO;
using System.Text;
using Wangkanai.Detection.Services;

namespace IsraelHiking.API.Tests.Services
namespace IsraelHiking.API.Tests.Services.Middleware
{
[TestClass]
public class CrawlersMiddlewareTests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.IO;
using IsraelHiking.API.Services.Middleware;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.FileProviders;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;

namespace IsraelHiking.API.Tests.Services.Middleware;

[TestClass]
public class SpaDefaultHtmlMiddlewareTests
{
private SpaDefaultHtmlMiddleware _middleware;
private IWebHostEnvironment _environment;
private RequestDelegate _next;

[TestInitialize]
public void TestInitialize()
{
_next = Substitute.For<RequestDelegate>();
_environment = Substitute.For<IWebHostEnvironment>();
_middleware = new SpaDefaultHtmlMiddleware(_next, _environment);
}

[TestMethod]
public void TestAPI_ShouldPassThrough()
{
var context = new DefaultHttpContext
{
Request =
{
Path = new PathString("/api/something"),
Host = new HostString("israelhiking.osm.org.il"),
QueryString = QueryString.Empty,
PathBase = PathString.Empty,
Scheme = "http"
}
};

_middleware.InvokeAsync(context).Wait();

_next.Received().Invoke(context);
}

[TestMethod]
public void TestOther_ShouldReturnHtmlFile()
{
var context = new DefaultHttpContext
{
Request =
{
Path = new PathString("/pther"),
Host = new HostString("israelhiking.osm.org.il"),
QueryString = QueryString.Empty,
PathBase = PathString.Empty,
Scheme = "http"
}
};
var fileInfo = Substitute.For<IFileInfo>();
fileInfo.CreateReadStream().Returns(new MemoryStream(new byte[] {1}));
_environment.WebRootFileProvider.GetFileInfo(Arg.Any<string>()).Returns(fileInfo);

_middleware.InvokeAsync(context).Wait();

_next.DidNotReceive().Invoke(context);
}
}

0 comments on commit adc37ef

Please sign in to comment.