|
| 1 | +package org.baeldung.web.controller.redirect; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.is; |
| 4 | +import static org.hamcrest.CoreMatchers.nullValue; |
| 5 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 6 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.flash; |
| 7 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; |
| 8 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; |
| 9 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; |
| 10 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 11 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; |
| 12 | +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; |
| 13 | + |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.test.context.ContextConfiguration; |
| 19 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 20 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 21 | +import org.springframework.test.web.servlet.MockMvc; |
| 22 | +import org.springframework.web.context.WebApplicationContext; |
| 23 | + |
| 24 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 25 | +@ContextConfiguration("file:src/main/webapp/WEB-INF/api-servlet.xml") |
| 26 | +@WebAppConfiguration |
| 27 | +public class RedirectControllerTest { |
| 28 | + |
| 29 | + private MockMvc mockMvc; |
| 30 | + |
| 31 | + @Autowired |
| 32 | + protected WebApplicationContext wac; |
| 33 | + |
| 34 | + @Before |
| 35 | + public void setup() { |
| 36 | + mockMvc = webAppContextSetup(wac).build(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { |
| 41 | + mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()).andExpect(view().name("RedirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithXMLConfig"))) |
| 42 | + .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig")); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { |
| 47 | + mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix"))) |
| 48 | + .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { |
| 53 | + mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes"))) |
| 54 | + .andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))).andExpect(model().attribute("flashAttribute", is(nullValue()))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { |
| 59 | + mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", is("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { |
| 64 | + mockMvc.perform(get("/forwardWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl")); |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments