-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1a8b29
commit 13ebb02
Showing
3 changed files
with
64 additions
and
8 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
backend/pium/src/main/java/com/official/pium/controller/HttpCookieManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
backend/pium/src/test/java/com/official/pium/controller/HttpCookieManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |