Skip to content

Commit af55aec

Browse files
committed
Revise IsSpaRoute
Removed `static` from `SpaGetRoutes` and `SpaPostRoutes` because their modification was lasting between test instantiations.
1 parent 3f66c57 commit af55aec

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/SIL.XForge.Scripture/Startup.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class Startup
5555
/// <summary>
5656
/// Routes that should be handled by SPA in both production and development.
5757
/// </summary>
58-
private static readonly HashSet<string> SpaGetRoutes =
58+
private readonly HashSet<string> SpaGetRoutes =
5959
[
6060
"index.html",
6161
"prerendered-routes.json",
@@ -90,7 +90,7 @@ public class Startup
9090

9191
private static readonly HashSet<string> DevelopmentSpaPostRoutes = ["sockjs-node"];
9292
private static readonly HashSet<string> ProductionSpaPostRoutes = [];
93-
private static readonly HashSet<string> SpaPostRoutes = [];
93+
private readonly HashSet<string> SpaPostRoutes = [];
9494

9595
public Startup(IConfiguration configuration, IWebHostEnvironment env, ILoggerFactory loggerFactory)
9696
{
@@ -360,15 +360,15 @@ internal bool IsSpaRoute(HttpContext context)
360360
// /@vite/client
361361
// Anything could conceivably contain a '?'.
362362

363-
// Look at what is after the first slash, and before the next slash or '?'. Match paths like /projects/123456789
363+
// Look at what is after starting slashes, and before the next slash or '?'. Match paths like /projects/123456789
364364
// as "projects", /login?a=b as "login", and /safety-worker.js as "safety-worker.js".
365-
string exact = path.Split('/', '?').ElementAtOrDefault(1) ?? "";
365+
string exact = path?.TrimStart('/').Split('/', '?').FirstOrDefault() ?? string.Empty;
366366
if (spaRoutes.Contains(exact))
367367
return true;
368368

369369
// Then look at what is before the first dash or dot. Match paths like /polyfills-C3D4E5F6.js.map and
370370
// /polyfills.js as "polyfills".
371-
string beginning = exact.Split(['-', '.']).FirstOrDefault();
371+
string beginning = exact.Split('-', '.').FirstOrDefault();
372372
if (spaRoutes.Contains(beginning))
373373
return true;
374374

test/SIL.XForge.Scripture.Tests/StartupTests.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@
1111

1212
namespace SIL.XForge.Scripture;
1313

14-
/// <summary>
15-
/// Represents what environment the application is running in.
16-
/// </summary>
17-
public enum RunMode
18-
{
19-
Development,
20-
Production,
21-
}
22-
2314
[TestFixture]
2415
public class StartupTests
2516
{
@@ -177,10 +168,15 @@ private static void IsSpaRoute_Helper(string path, RunMode[] runModes, bool expe
177168
[TestCase("/connect-project")]
178169
[TestCase("/serval-administration")]
179170
[TestCase("/system-administration")]
171+
// Handling '//login' may not be very important but would make sense to handle it as if it was normalized to
172+
// '/login'.
173+
[TestCase("//login")]
174+
[TestCase("//login//")]
175+
[TestCase("///login")]
180176
public void IsSpaRoute_ProductionAndDevelopment_True(string path)
181177
{
182178
RunMode[] runModes = [RunMode.Production, RunMode.Development];
183-
bool expected = true;
179+
const bool expected = true;
184180
IsSpaRoute_Helper(path, runModes, expected);
185181
}
186182

@@ -195,7 +191,7 @@ public void IsSpaRoute_ProductionAndDevelopment_True(string path)
195191
public void IsSpaRoute_Development_True(string path)
196192
{
197193
RunMode[] runModes = [RunMode.Development];
198-
bool expected = true;
194+
const bool expected = true;
199195
IsSpaRoute_Helper(path, runModes, expected);
200196
}
201197

@@ -221,7 +217,9 @@ public void IsSpaRoute_Development_True(string path)
221217
[TestCase("/??/login")]
222218
[TestCase("/#login")]
223219
[TestCase("/#/login")]
224-
// It may or may not be possible for the path to be the empty string. If it is, let's have ASP.NET handle it.
220+
// It may or may not be possible in practice for the path to be null or empty. If it is, let's have ASP.NET handle
221+
// it.
222+
[TestCase(null)]
225223
[TestCase("")]
226224
// The paths "/", "/Index", and "/Status/Error" are handled by ASP.NET and don't even get to IsSpaRoute. If they did
227225
// for some reason, we'll have IsSpaRoute return false.
@@ -235,7 +233,7 @@ public void IsSpaRoute_Development_True(string path)
235233
public void IsSpaRoute_ProductionAndDevelopment_False(string path)
236234
{
237235
RunMode[] runModes = [RunMode.Production, RunMode.Development];
238-
bool expected = false;
236+
const bool expected = false;
239237
IsSpaRoute_Helper(path, runModes, expected);
240238
}
241239

@@ -244,10 +242,19 @@ public void IsSpaRoute_ProductionAndDevelopment_False(string path)
244242
public void IsSpaRoute_Development_False(string path)
245243
{
246244
RunMode[] runModes = [RunMode.Development];
247-
bool expected = false;
245+
const bool expected = false;
248246
IsSpaRoute_Helper(path, runModes, expected);
249247
}
250248

249+
/// <summary>
250+
/// Represents what environment the application is running in.
251+
/// </summary>
252+
private enum RunMode
253+
{
254+
Development,
255+
Production,
256+
}
257+
251258
private class TestEnvironment
252259
{
253260
public TestEnvironment(string? environmentName = null)

0 commit comments

Comments
 (0)