Skip to content

Commit

Permalink
πŸ— : add controller to manage natural routes for frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubuisson committed Apr 9, 2020
1 parent ebb886c commit 9f9090c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.codeka.gaia.client.controller

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping

@Controller
class ClientForwardController {

@RequestMapping("/**/{path:[^\\.]*}")
fun redirect(): String = "forward:/"

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.codeka.gaia.client.controller

import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

class ClientForwardControllerTest {

lateinit var mockMvc: MockMvc

@BeforeEach
fun setup() {
mockMvc = MockMvcBuilders
.standaloneSetup(ClientForwardController(), TestController())
.build()
}

@Test
fun `clientForwardController should forward to home page for unknown mapping`() {
mockMvc.perform(MockMvcRequestBuilders.get("/path/to/go"))
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.forwardedUrl("/"))
}

@Test
fun `clientForwardController should ignore backend`() {
mockMvc.perform(MockMvcRequestBuilders.get("/api/test"))
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaType.TEXT_PLAIN_VALUE))
.andExpect(MockMvcResultMatchers.content().string("test"))
}

@RestController
class TestController {

@RequestMapping("/api/test")
fun test(): String = "test"

}

}

0 comments on commit 9f9090c

Please sign in to comment.