-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor fallback away from GlobalExceptionHandler
- Loading branch information
Showing
4 changed files
with
92 additions
and
61 deletions.
There are no files selected for viewing
15 changes: 0 additions & 15 deletions
15
backend/src/main/java/de/neuefische/paulkreft/backend/exception/GlobalExceptionHandler.java
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
39 changes: 39 additions & 0 deletions
39
backend/src/main/java/de/neuefische/paulkreft/backend/fallback/Fallback.java
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,39 @@ | ||
package de.neuefische.paulkreft.backend.fallback; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
import org.springframework.web.servlet.resource.PathResourceResolver; | ||
|
||
import java.io.IOException; | ||
|
||
@Configuration | ||
public class Fallback implements WebMvcConfigurer { | ||
|
||
public static final String DEFAULT_STARTING_PAGE = "static/index.html"; | ||
|
||
static class ReactRoutingPathResourceResolver extends PathResourceResolver { | ||
@Override | ||
protected Resource getResource(String resourcePath, Resource location) throws IOException { | ||
var requestedResource = location.createRelative(resourcePath); | ||
|
||
// Is this a request to a real file? | ||
if (requestedResource.exists() && requestedResource.isReadable()) { | ||
return requestedResource; | ||
} | ||
|
||
// It seems to be only a frontend-routing request (Single-Page-Application). | ||
return new ClassPathResource(DEFAULT_STARTING_PAGE); | ||
} | ||
} | ||
|
||
@Override | ||
public void addResourceHandlers(ResourceHandlerRegistry registry) { | ||
registry.addResourceHandler("/**") | ||
.addResourceLocations("classpath:/static/") | ||
.resourceChain(true) | ||
.addResolver(new ReactRoutingPathResourceResolver()); | ||
} | ||
} |
46 changes: 0 additions & 46 deletions
46
...d/src/test/java/de/neuefische/paulkreft/backend/exception/GlobalExceptionHandlerTest.java
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
backend/src/test/java/de/neuefische/paulkreft/backend/fallback/FallbackTest.java
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,53 @@ | ||
package de.neuefische.paulkreft.backend.fallback; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.core.io.Resource; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class FallbackTest { | ||
|
||
@Test | ||
void expectRelativeResource_ifItExists() throws IOException { | ||
|
||
// GIVEN | ||
var resolver = new Fallback.ReactRoutingPathResourceResolver(); | ||
var location = mock(Resource.class); | ||
var relativeLocation = mock(Resource.class); | ||
var resourcePath = "index.html"; | ||
when(location.createRelative(resourcePath)).thenReturn(relativeLocation); | ||
when(relativeLocation.exists()).thenReturn(true); | ||
when(relativeLocation.isReadable()).thenReturn(true); | ||
|
||
// WHEN | ||
var actual = resolver.getResource(resourcePath, location); | ||
|
||
// THEN | ||
Assertions.assertEquals(relativeLocation, actual); | ||
} | ||
|
||
@Test | ||
void expectIndexHtml_ifRequestedResourceDoesNotExist() throws IOException { | ||
|
||
// GIVEN | ||
var resolver = new Fallback.ReactRoutingPathResourceResolver(); | ||
var location = mock(Resource.class); | ||
var relativeLocation = mock(Resource.class); | ||
var resourcePath = "index.html"; | ||
when(location.createRelative(resourcePath)).thenReturn(relativeLocation); | ||
when(relativeLocation.exists()).thenReturn(false); | ||
|
||
// WHEN | ||
var actual = resolver.getResource(resourcePath, location); | ||
|
||
// THEN | ||
ClassPathResource expected = new ClassPathResource("static/index.html"); | ||
Assertions.assertEquals(expected, actual); | ||
} | ||
|
||
} |