-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a problem with missing html file (#1933)
* Fix 404 * Added relevant middleware, added tests * Small fixes * Typo... * More small fixes
- Loading branch information
Showing
5 changed files
with
122 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
IsraelHiking.API/Services/Middleware/SpaDefaultHtmlMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
Tests/IsraelHiking.API.Tests/Services/Middleware/SpaDefaultHtmlMiddlewareTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |