Skip to content

Commit

Permalink
fix: 쿠키 요청 구분 보완 (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
Choi-JJunho authored and hozzijeong committed Oct 20, 2023
1 parent f1a8b29 commit 13ebb02
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.official.pium.controller;

import java.util.HashMap;
import java.util.Map;

public class HttpCookieManager {

private static final int KEY_INDEX = 0;
private static final int VALUE_INDEX = 1;
private static final int COOKIE_SIZE = 2;
private static final String SESSION_DELIMITER = "=";
private static final String SESSIONS_DELIMITER = ";";

private final Map<String, String> cookies = new HashMap<>();

public HttpCookieManager(String cookies) {
for (String cookie : cookies.split(SESSIONS_DELIMITER)) {
String[] cookiePair = cookie.split(SESSION_DELIMITER);
if (cookiePair.length != COOKIE_SIZE) {
continue;
}
this.cookies.put(cookiePair[KEY_INDEX], cookiePair[VALUE_INDEX]);
}
}

public String getCookie(String key) {
return cookies.get(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
@RequiredArgsConstructor
public class MemberArgumentResolver implements HandlerMethodArgumentResolver {

private static final int VALUE_INDEX = 1;
private static final String SESSION_DELIMITER = "=";
private static final String SESSION_KEY = "KAKAO_ID";
private static final String JSESSIONID = "JSESSIONID";
private static final String COOKIE_HEADER = "cookie";
private static final int MIN_SESSION_SIZE = 2;

private final MemberRepository memberRepository;
private final SessionGroupService sessionGroupService;
Expand Down Expand Up @@ -53,10 +51,7 @@ private String parseSessionCookie(String sessionCookie) {
if (sessionCookie == null || sessionCookie.isBlank()) {
return null;
}
String[] keyAndValue = sessionCookie.split(SESSION_DELIMITER);
if (keyAndValue.length < MIN_SESSION_SIZE) {
return null;
}
return keyAndValue[VALUE_INDEX];
HttpCookieManager httpCookieManager = new HttpCookieManager(sessionCookie);
return httpCookieManager.getCookie(JSESSIONID);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.official.pium.controller;

import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class HttpCookieManagerTest {

@Test
void JSESSIONID를_가져온다() {
String requestCookie = "JSESSIONID=F51D50B1C12CE2BC58C6AC6EF8EF0092; _ga_8SL2D547VW=GS1.1.1697719895.1.1.1697720060.0.0.0";
HttpCookieManager httpCookieManager = new HttpCookieManager(requestCookie);

String jsessionid = httpCookieManager.getCookie("JSESSIONID");

assertThat(jsessionid).isEqualTo("F51D50B1C12CE2BC58C6AC6EF8EF0092");
}

@Test
void 일치하는_KEY가_없으면_NULL을_반환한다() {
String requestCookie = "JSESSIONID=; _ga_8SL2D547VW=GS1.1.1697719895.1.1.1697720060.0.0.0";
HttpCookieManager httpCookieManager = new HttpCookieManager(requestCookie);

String jsessionid = httpCookieManager.getCookie("JSESSIONID");

assertThat(jsessionid).isNull();
}
}

0 comments on commit 13ebb02

Please sign in to comment.