-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookieUtilTest.java
More file actions
113 lines (93 loc) · 3.6 KB
/
CookieUtilTest.java
File metadata and controls
113 lines (93 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.mycodingtest.common.util;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.http.ResponseCookie;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@Tag("unit")
class CookieUtilTest {
private final String COOKIE_NAME = "jwt";
private CookieUtil cookieUtil;
@BeforeEach
void setUp() {
cookieUtil = new CookieUtil();
ReflectionTestUtils.setField(cookieUtil, "cookieName", COOKIE_NAME);
}
@Test
@DisplayName("JWT 토큰이 담긴 쿠키를 생성한다")
void generateJwtCookie() {
// given
String token = "test-jwt-token";
// when
ResponseCookie cookie = cookieUtil.generateJwtCookie(token);
// then
assertThat(cookie.getName()).isEqualTo(COOKIE_NAME);
assertThat(cookie.getValue()).isEqualTo(token);
assertThat(cookie.isHttpOnly()).isTrue();
assertThat(cookie.isSecure()).isTrue();
assertThat(cookie.getMaxAge().getSeconds()).isEqualTo(60 * 60 * 24 * 90);
assertThat(cookie.getPath()).isEqualTo("/");
assertThat(cookie.getSameSite()).isEqualTo("None");
}
@Test
@DisplayName("JWT 토큰이 담긴 쿠키를 요청에서 추출한다")
void getJwtFromCookie() {
// given
HttpServletRequest request = mock(HttpServletRequest.class);
String tokenValue = "test-jwt-token";
Cookie[] cookies = {
new Cookie("other", "value"),
new Cookie(COOKIE_NAME, tokenValue)
};
given(request.getCookies()).willReturn(cookies);
// when
String extractedToken = cookieUtil.getJwtFromCookie(request);
// then
assertThat(extractedToken).isEqualTo(tokenValue);
}
@Test
@DisplayName("JWT 쿠키가 없는 경우 null을 반환한다")
void getJwtFromCookie_WhenNoCookie() {
// given
HttpServletRequest request = mock(HttpServletRequest.class);
Cookie[] cookies = {
new Cookie("other", "value")
};
given(request.getCookies()).willReturn(cookies);
// when
String extractedToken = cookieUtil.getJwtFromCookie(request);
// then
assertThat(extractedToken).isNull();
}
@Test
@DisplayName("JWT 쿠키를 제거한다")
void generateClearJwtCookie() {
// when
ResponseCookie cookie = cookieUtil.generateClearJwtCookie();
// then
assertThat(cookie.getName()).isEqualTo(COOKIE_NAME);
assertThat(cookie.getValue()).isEqualTo("");//null을 넣으면 ""로 value 가 됨!
assertThat(cookie.isHttpOnly()).isTrue();
assertThat(cookie.isSecure()).isTrue();
assertThat(cookie.getMaxAge().getSeconds()).isEqualTo(0);
assertThat(cookie.getPath()).isEqualTo("/");
assertThat(cookie.getSameSite()).isEqualTo("None");
}
@Test
@DisplayName("요청에 쿠키가 아예 없는경우 null을 반환한다.")
void getJwtFromCookie_WhenRequestNotContainCookie() {
// given
HttpServletRequest request = mock(HttpServletRequest.class);
given(request.getCookies()).willReturn(null);
// when
String extractedToken = cookieUtil.getJwtFromCookie(request);
// then
assertThat(extractedToken).isNull();
}
}