@Controller
public class UserWebController {
@GetMapping("/user/signup")
public String signupPage() {
return "/user/signup";
}
@GetMapping("/user/login")
public String loginPage() {
return "/user/login";
}
@GetMapping("/user/update")
public String updatePage() {
return "/user/update";
}
@GetMapping("/user/escape")
public String escapePage() {
return "/user/escape";
}
}
->
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@AutoConfigureMockMvc(addFilters = false)
@SpringBootTest
class UserWebControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void signupPage() throws Exception {
mockMvc.perform(get("/user/signup"))
.andExpect(status().isOk())
.andExpect(view().name("/user/signup"))
.andDo(print());
}
@Test
void loginPage() throws Exception {
mockMvc.perform(get("/user/login"))
.andExpect(status().isOk())
.andExpect(view().name("/user/login"))
.andDo(print());
}
@Test
void updatePage() throws Exception {
mockMvc.perform(get("/user/update"))
.andExpect(status().isOk())
.andExpect(view().name("/user/update"))
.andDo(print());
}
@Test
void escapePage() throws Exception {
mockMvc.perform(get("/user/escape"))
.andExpect(status().isOk())
.andExpect(view().name("/user/escape"))
.andDo(print());
}
}
@Controller
@RequiredArgsConstructor
public class MainController {
private final BoardService boardService;
@GetMapping("/")
public String index(Model model) {
List<BoardReadAllResponseDto> boards = boardService.readAllBoard();
model.addAttribute("boards", boards);
model.addAttribute("message", "Thymeleaf를 사용한 Spring 웹서비스");
return "/main";
}
@GetMapping("/board/{boardId}")
public String getBoard(@PathVariable Long boardId, Model model) {
BoardReadAllResponseDto boardSearchList = boardService.readChoiceBoard(boardId);
model.addAttribute("boardData", boardSearchList);
return "/board/boardView"; // boardView.html로 이동
}
}
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
class MainControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void index() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("/main"))
.andDo(print());
}
@Test
void getBoard() throws Exception {
mockMvc.perform(get("/board/{boardId}", 1L ))
.andExpect(status().isOk())
.andExpect(view().name("/board/boardView"))
.andDo(print());
}
}
userWebController에서 로그아웃 매핑
쿠키 삭제 기능 + "/"(login 페이지로 이동 )
@GetMapping("/user/logout")
public String logout() {
/**
* 쿠키 삭제 기능 추가
*/
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = attributes.getResponse();
Cookie cookie = new Cookie("Authorization", null);
cookie.setMaxAge(0);
cookie.setPath("/");
response.addCookie(cookie);
return "/";
}