Skip to content

Commit

Permalink
#34 - Test: 올바른 POST /api/v1/member/login 요청의 응답 헤더에 Authentication 값…
Browse files Browse the repository at this point in the history
…(JWT 토큰)이 있는지 테스트
  • Loading branch information
ahah525 committed Nov 8, 2022
1 parent 295851c commit 60aa1ce
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.transaction.annotation.Transactional;

import java.nio.charset.StandardCharsets;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -47,4 +50,35 @@ void t1() throws Exception {
resultActions
.andExpect(status().is2xxSuccessful());
}

@Test
@DisplayName("POST /api/v1/member/login 으로 올바른 username과 password 데이터를 넘기면 JWT키를 발급해 응답 헤더에 담아준다.")
void t2() throws Exception {
// When
ResultActions resultActions = mvc
.perform(
post("/api/v1/member/login")
.content("""
{
"username": "user1",
"password": "1234"
}
""".stripIndent())
.contentType(new MediaType(MediaType.APPLICATION_JSON, StandardCharsets.UTF_8))
)
.andDo(print());

// Then
resultActions
.andExpect(status().is2xxSuccessful());

// 응답 결과
MvcResult mvcResult = resultActions.andReturn();

MockHttpServletResponse response = mvcResult.getResponse();
// 응답 헤더 Authentication(key) : JWT 키(value)
String authentication = response.getHeader("Authentication");

assertThat(authentication).isNotEmpty();
}
}

0 comments on commit 60aa1ce

Please sign in to comment.