-
Notifications
You must be signed in to change notification settings - Fork 0
대소문자를 구분하지 않는 Attributes #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: freddie-noel
Are you sure you want to change the base?
Changes from all commits
5106c52
532ad2e
c698959
76a3235
0d3fa81
b0ee9ae
0c06f60
780f6e9
2851f2a
dfe49f7
49d127e
66dcf7c
f61aa7f
8dbb272
41592da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,7 @@ | |
| import util.HttpRequestUtils; | ||
| import webserver.Const; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.StringJoiner; | ||
| import java.util.*; | ||
|
|
||
| public class Attributes { | ||
| private final Map<String, String> attributes = new LinkedHashMap<>(); | ||
|
|
@@ -16,36 +13,52 @@ public static Attributes from(Map<String, String> attributes) { | |
| } | ||
|
|
||
| public static Attributes from(String headerText) { | ||
| Map<String, String> attributes = new LinkedHashMap<>(); | ||
| Attributes attributes = new Attributes(); | ||
|
|
||
| String[] splittedHeaderTexts = headerText.split(Const.CRLF); | ||
| for (String splittedHeaderText : splittedHeaderTexts) { | ||
| HttpRequestUtils.Pair pair = HttpRequestUtils.parseHeader(splittedHeaderText); | ||
|
|
||
| if (pair != null) { | ||
| attributes.put(pair.getKey(), pair.getValue()); | ||
| attributes.add(pair.getKey(), pair.getValue()); | ||
| } | ||
| } | ||
|
|
||
| return from(attributes); | ||
| return attributes; | ||
| } | ||
|
|
||
| public Attributes add(String key, String value) { | ||
| attributes.put(key, value); | ||
| public Attributes add(String targetKey, String value) { | ||
| for (String currentKey : attributes.keySet()) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍👍👍👍 |
||
| if (currentKey.equalsIgnoreCase(targetKey)) { | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| attributes.put(targetKey, value); | ||
| return this; | ||
| } | ||
|
|
||
| public Attributes addAll(Map<String, String> attributes) { | ||
| this.attributes.putAll(attributes); | ||
| for (String currentKey : attributes.keySet()) { | ||
| add(currentKey, attributes.get(currentKey)); | ||
| } | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public String get(String key) { | ||
| return attributes.get(key); | ||
| public String get(String targetKey) { | ||
| return getOrDefault(targetKey, null); | ||
| } | ||
|
|
||
| public String getOrDefault(String key, String defaultValue) { | ||
| return attributes.getOrDefault(key, defaultValue); | ||
| public String getOrDefault(String targetKey, String defaultValue) { | ||
|
|
||
| for (String currentKey : attributes.keySet()) { | ||
| if (currentKey.equalsIgnoreCase(targetKey)) { | ||
| return attributes.get(currentKey); | ||
| } | ||
| } | ||
Dae-Hwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return defaultValue; | ||
| } | ||
|
|
||
| public String toHeaderText() { | ||
|
|
@@ -70,4 +83,9 @@ public int hashCode() { | |
| return Objects.hash(attributes); | ||
| } | ||
|
|
||
|
|
||
| public int size(){ | ||
| return attributes.size(); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| package webserver.http.attribute; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
|
|
@@ -11,6 +13,8 @@ | |
| import java.util.stream.Stream; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
| import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
|
||
| class AttributesTest { | ||
|
|
||
|
|
@@ -39,14 +43,42 @@ void fromHeaderText() { | |
| assertThat(actualAttributes).isEqualTo(expectedAttributes); | ||
| } | ||
|
|
||
| @Test | ||
| void add() { | ||
|
|
||
| @ParameterizedTest | ||
| @DisplayName("add 테스트: key의 대소문자 관계 없이 Attributes를 꺼내올 수 있음") | ||
| @MethodSource("add") | ||
| void add(String expectedValue, String actualValue) { | ||
| assertThat(actualValue).isEqualTo(expectedValue); | ||
| } | ||
|
|
||
| static Stream<Arguments> add() { | ||
| Attributes attributes = new Attributes(); | ||
| attributes.add("key", "value"); | ||
|
|
||
| return Stream.of( | ||
| Arguments.of( | ||
| "value", | ||
| attributes.get("KEY") | ||
| ), | ||
| Arguments.of( | ||
| "value", | ||
| attributes.get("key") | ||
| ), | ||
| Arguments.of( | ||
| "value", | ||
| attributes.get("kEy") | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void addAll() { | ||
| @ParameterizedTest | ||
| @DisplayName("addAll 테스트: key의 대소문자 관계 없이 Attributes를 꺼내올 수 있음") | ||
| @MethodSource("addAll") | ||
| void addAll(String expectedValue, String actualValue) { | ||
| assertThat(actualValue).isEqualTo(expectedValue); | ||
| } | ||
|
|
||
| static Stream<Arguments> addAll() { | ||
| Attributes attributes = new Attributes(); | ||
| Map<String, String> attributeMap = new LinkedHashMap<>(); | ||
| attributeMap.put("key", "value"); | ||
|
|
@@ -55,7 +87,20 @@ void addAll() { | |
| Attributes expectedAttributes = new Attributes(); | ||
| expectedAttributes.add("key", "value"); | ||
|
|
||
| assertThat(attributes).isEqualTo(expectedAttributes); | ||
| return Stream.of( | ||
| Arguments.of( | ||
| expectedAttributes.get("key"), | ||
| attributes.get("KEY") | ||
| ), | ||
| Arguments.of( | ||
| expectedAttributes.get("KEY"), | ||
| attributes.get("kEY") | ||
| ), | ||
| Arguments.of( | ||
| expectedAttributes.get("kEy"), | ||
| attributes.get("kEy") | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -66,13 +111,27 @@ void get() { | |
| assertThat(attributes.get("key")).isEqualTo("value"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("존재하지 않는 키를 조회하면 null") | ||
| void getValueWithEmptyKey() { | ||
| Attributes attributes = new Attributes(); | ||
| assertThat(attributes.get("null")).isNull(); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("getOrDefault") | ||
| void getOrDefault(Attributes attributes, String defaultValue, String key, String expectedValue) { | ||
| String actualValue = attributes.getOrDefault(key, defaultValue); | ||
| assertThat(actualValue).isEqualTo(expectedValue); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("존재하지 않는 키를 조회하면 주어진 기본값으로 대체") | ||
| void getDefaultValueWithEmptyKey() { | ||
| Attributes attributes = new Attributes(); | ||
| assertThat(attributes.getOrDefault("emptyKey", "happy")).isEqualTo("happy"); | ||
| } | ||
|
|
||
| static Stream<Arguments> getOrDefault() { | ||
| return Stream.of( | ||
| Arguments.of( | ||
|
|
@@ -99,4 +158,19 @@ void toHeaderText() { | |
| assertThat(attributes.toHeaderText()).isEqualTo("key1: value1\r\nkey2: value2"); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("value가 null인 key") | ||
| void isNullWhenNullValue(){ | ||
| Attributes attributes = new Attributes(); | ||
| attributes.add("null", null); | ||
| attributes.add("null2", null); | ||
|
|
||
| assertAll( | ||
| ()-> assertThat(attributes.size()).isEqualTo(2), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. null을 확인하는 테스트 같은데 사이즈를 확인하는 이유가 어떤건가요? |
||
| ()-> assertThat(attributes.get("null")).isNull(), | ||
| ()-> assertThat(attributes.get("null2")).isNull() | ||
|
Comment on lines
+171
to
+172
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 둘 다 null을 확인하는 별개의 테스트케이스 같은데, 따로 묶은 이유가 있으신가요? |
||
| ); | ||
|
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.