-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Development: Re-add client forward resource to support reloading the …
…page
- Loading branch information
Stephan Krusche
committed
Mar 20, 2024
1 parent
5c4a24e
commit c22dcf6
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/de/tum/in/www1/artemis/web/rest/ClientForwardResource.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,28 @@ | ||
package de.tum.in.www1.artemis.web.rest; | ||
|
||
import static de.tum.in.www1.artemis.config.Constants.PROFILE_CORE; | ||
|
||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import de.tum.in.www1.artemis.security.annotations.EnforceNothing; | ||
import de.tum.in.www1.artemis.security.annotations.ManualConfig; | ||
|
||
@Profile(PROFILE_CORE) | ||
@Controller | ||
public class ClientForwardResource { | ||
|
||
/** | ||
* Forwards any unmapped paths (except those containing a period) to the client index.html | ||
* This is important so that reloads of the client app do not lead to NOT FOUND | ||
* | ||
* @return Forward Instruction for Browser | ||
*/ | ||
@RequestMapping({ "{path:[^\\.]*}", "{path:^(?!websocket).*}/**/{path:[^\\.]*}" }) | ||
@EnforceNothing | ||
@ManualConfig | ||
public String forward() { | ||
return "forward:/"; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/test/java/de/tum/in/www1/artemis/ClientForwardTest.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,78 @@ | ||
package de.tum.in.www1.artemis; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import javax.servlet.http.Cookie; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseCookie; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
import de.tum.in.www1.artemis.security.jwt.JWTCookieService; | ||
import de.tum.in.www1.artemis.security.jwt.JWTFilter; | ||
import de.tum.in.www1.artemis.web.rest.ClientForwardResource; | ||
|
||
/** | ||
* Test class for the ClientForwardController REST controller. | ||
* | ||
* @see ClientForwardResource | ||
*/ | ||
class ClientForwardTest extends AbstractSpringIntegrationIndependentTest { | ||
|
||
@Autowired | ||
private JWTCookieService jwtCookieService; | ||
|
||
@Test | ||
void testClientEndpoint() throws Exception { | ||
ResultActions perform = request.getMvc().perform(get("/non-existent-mapping")); | ||
perform.andExpect(status().isOk()).andExpect(forwardedUrl("/")); | ||
} | ||
|
||
@Test | ||
void testNestedClientEndpoint() throws Exception { | ||
request.getMvc().perform(get("/admin/user-management")).andExpect(status().isOk()).andExpect(forwardedUrl("/")); | ||
} | ||
|
||
@Test | ||
void getUnmappedDottedEndpoint() throws Exception { | ||
request.getMvc().perform(get("/foo.js")).andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
void getUnmappedNestedDottedEndpoint() throws Exception { | ||
request.getMvc().perform(get("/foo/bar.js")).andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
void getWebsocketInfoEndpoint() throws Exception { | ||
request.getMvc().perform(get("/websocket/info")).andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
void getWebsocketEndpointFailedHandshakeNoCookie() throws Exception { | ||
request.getMvc().perform(get("/websocket/308/sessionId/websocket")).andExpect(status().isOk()); // Failed handshake without cookie returns 200 | ||
} | ||
|
||
@Test | ||
void getWebsocketEndpointWithInvalidCookie() throws Exception { | ||
Cookie cookie = new Cookie(JWTFilter.JWT_COOKIE_NAME, "invalidCookie"); | ||
request.getMvc().perform(get("/websocket/308/sessionId/websocket").cookie(cookie)).andExpect(status().isOk()); // Failed handshake with invalid cookie returns 200 | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "admin", roles = "ADMIN") | ||
void getWebsocketEndpointWithCookie() throws Exception { | ||
ResponseCookie responseCookie = jwtCookieService.buildLoginCookie(true); | ||
Cookie cookie = new Cookie(responseCookie.getName(), responseCookie.getValue()); | ||
request.getMvc().perform(get("/websocket/308/sessionId/websocket").cookie(cookie)).andExpect(status().isBadRequest()) | ||
.andExpect(content().string("Can \"Upgrade\" only to \"WebSocket\".")); // Handshake is successfull but connection fails to upgrade using MockMvc | ||
} | ||
|
||
@Test | ||
void getWebsocketFallbackEndpoint() throws Exception { | ||
request.getMvc().perform(get("/websocket/308/sessionId/xhr_streaming")).andExpect(status().isNotFound()); | ||
} | ||
} |