Skip to content

Commit

Permalink
refactor: Request에서 Content-Length를 가져올 수 있도록 수정 (상세내용 o)
Browse files Browse the repository at this point in the history
#66 #67

+ 헤더(Attribute) 삽입은 일괄 대문자로 하여, 키의 대소문자 관계없이 value를 꺼내올 수 있게 하였음
  • Loading branch information
sanhee committed Dec 2, 2021
1 parent a4f4cf2 commit a78533d
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/main/java/webserver/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public void run() {

RequestHeader requestHeader = RequestHeader.from(requestMessages.toString());

int contentLength = Integer.parseInt(requestHeader.getAttributes().getOrDefault("Content-Length", "0"));
// Request 에서 contentLength를 바로 가져올 수 있게는 못할까? 생각...
int contentLength = requestHeader.getContentLength();
String requestBody = IOUtils.readData(br, contentLength);

requestMessages.add(System.lineSeparator() + requestBody);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/webserver/http/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public String getPathExtension() {

return extension;
}

public int getContentLength() {
return requestMessage.getHeader().getContentLength();
}
}
14 changes: 10 additions & 4 deletions src/main/java/webserver/http/attribute/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,27 @@ public static Attributes from(String headerText) {
}

public Attributes add(String key, String value) {
attributes.put(key, value);
attributes.put(key.toUpperCase(), value);
return this;
}

public Attributes addAll(Map<String, String> attributes) {
this.attributes.putAll(attributes);
Map<String, String> upperAttributes = new LinkedHashMap<>();

for (String key : attributes.keySet()) {
upperAttributes.put(key.toUpperCase(), attributes.get(key));
}

this.attributes.putAll(upperAttributes);
return this;
}

public String get(String key) {
return attributes.get(key);
return attributes.get(key.toUpperCase());
}

public String getOrDefault(String key, String defaultValue) {
return attributes.getOrDefault(key, defaultValue);
return attributes.getOrDefault(key.toUpperCase(), defaultValue);
}

public String toHeaderText() {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/webserver/http/header/RequestHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ protected String getStatusLine() {
public String getQueryString() {
return statusLine.getQueryString();
}

public int getContentLength() {
return Integer.parseInt(super.getAttributes().getOrDefault("Content-Length", "0"));
}
}
54 changes: 38 additions & 16 deletions src/test/java/webserver/http/RequestTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package webserver.http;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -31,12 +32,12 @@ static Stream<Arguments> from() {
return Stream.of(
Arguments.of(
"GET /user/create?userId=javajigi&password=password&name=%EB%B0%95%EC%9E%AC%EC%84%B1&email=javajigi%40slipp.net HTTP/1.1" + System.lineSeparator() +
"Host: localhost:8080" + System.lineSeparator() +
"Connection: keep-alive" + System.lineSeparator() +
"Content-Length: 59" + System.lineSeparator() +
"Content-Type: application/x-www-form-urlencoded" + System.lineSeparator() +
"Accept: */*" + System.lineSeparator() +
"" + System.lineSeparator(),
"Host: localhost:8080" + System.lineSeparator() +
"Connection: keep-alive" + System.lineSeparator() +
"Content-Length: 59" + System.lineSeparator() +
"Content-Type: application/x-www-form-urlencoded" + System.lineSeparator() +
"Accept: */*" + System.lineSeparator() +
"" + System.lineSeparator(),
new GetMessage(RequestHeader.of(
Arrays.asList(
"GET",
Expand All @@ -53,13 +54,13 @@ static Stream<Arguments> from() {
))
), Arguments.of(
"POST /user/create HTTP/1.1" + System.lineSeparator() +
"Host: localhost:8080" + System.lineSeparator() +
"Connection: keep-alive" + System.lineSeparator() +
"Content-Length: 59" + System.lineSeparator() +
"Content-Type: application/x-www-form-urlencoded" + System.lineSeparator() +
"Accept: */*" + System.lineSeparator() +
"" + System.lineSeparator() +
"userId=javajigi&password=password&name=%EB%B0%95%EC%9E%AC%EC%84%B1&email=javajigi%40slipp.net",
"Host: localhost:8080" + System.lineSeparator() +
"Connection: keep-alive" + System.lineSeparator() +
"Content-Length: 59" + System.lineSeparator() +
"Content-Type: application/x-www-form-urlencoded" + System.lineSeparator() +
"Accept: */*" + System.lineSeparator() +
"" + System.lineSeparator() +
"userId=javajigi&password=password&name=%EB%B0%95%EC%9E%AC%EC%84%B1&email=javajigi%40slipp.net",
new PostMessage(
RequestHeader.of(
Arrays.asList(
Expand Down Expand Up @@ -87,7 +88,7 @@ void getPath(String desc, GetMessage getMessage, String expectedPath) {
String actualPath = new Request(getMessage).getPath();

assertThat(actualPath).as(desc)
.isEqualTo(expectedPath);
.isEqualTo(expectedPath);
}

static Stream<Arguments> getPath() {
Expand Down Expand Up @@ -135,8 +136,8 @@ static Stream<Arguments> getPath() {
@MethodSource("getPathExtension")
void getPathExtension(String desc, Request request, String expectedExtension) {
Assertions.assertThat(request.getPathExtension())
.as("status line에서 path의 확장자 가져오기 : %s", desc)
.isEqualTo(expectedExtension);
.as("status line에서 path의 확장자 가져오기 : %s", desc)
.isEqualTo(expectedExtension);
}

static Stream<Arguments> getPathExtension() {
Expand Down Expand Up @@ -280,4 +281,25 @@ static Stream<Arguments> getPathExtension() {
)
);
}


@Test
void getContentLength() {
Request request = new Request(new GetMessage(RequestHeader.of(
Arrays.asList(
"GET",
"/user/create?userId=javajigi&password=password&name=%EB%B0%95%EC%9E%AC%EC%84%B1&email=javajigi%40slipp.net",
"HTTP/1.1"
),
Attributes.from(new HashMap() {{
put("Host", "localhost:8080");
put("Connection", "keep-alive");
put("Content-Length", "59");
put("Content-Type", "application/x-www-form-urlencoded");
put("Accept", "*/*");
}})
)));

assertThat(request.getContentLength()).isEqualTo(59);
}
}
17 changes: 16 additions & 1 deletion src/test/java/webserver/http/attribute/AttributesTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package webserver.http.attribute;

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;
Expand All @@ -11,6 +12,8 @@
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

class AttributesTest {

Expand Down Expand Up @@ -40,12 +43,20 @@ void fromHeaderText() {
}

@Test
@DisplayName("add 테스트: key의 대소문자 관계 없이 Attributes를 꺼내올 수 있음")
void add() {
Attributes attributes = new Attributes();
attributes.add("key", "value");

assertAll(
() -> assertEquals("value", attributes.get("KEY")),
() -> assertEquals("value", attributes.get("key")),
() -> assertEquals("value", attributes.get("kEy"))
);
}

@Test
@DisplayName("addAll 테스트: key의 대소문자 관계 없이 Attributes를 꺼내올 수 있음")
void addAll() {
Attributes attributes = new Attributes();
Map<String, String> attributeMap = new LinkedHashMap<>();
Expand All @@ -55,7 +66,11 @@ void addAll() {
Attributes expectedAttributes = new Attributes();
expectedAttributes.add("key", "value");

assertThat(attributes).isEqualTo(expectedAttributes);
assertAll(
() -> assertEquals(expectedAttributes.get("key"), attributes.get("KEY")),
() -> assertEquals(expectedAttributes.get("KEY"), attributes.get("kEY")),
() -> assertEquals(expectedAttributes.get("kEy"), attributes.get("key"))
);
}

@Test
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/webserver/http/header/RequestHeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,54 @@ static Stream<Arguments> getPath() {
)
);
}

@ParameterizedTest
@MethodSource("getContentLength")
void getContentLength(String desc, RequestHeader requestHeader, int expectContentLength) {
int actualContentLength = requestHeader.getContentLength();

assertThat(actualContentLength).as(desc)
.isEqualTo(expectContentLength);
}

static Stream<Arguments> getContentLength() {
return Stream.of(
Arguments.of(
"단일 헤더: Content-Length 파싱",
new RequestHeader(null, new Attributes().add("Content-Length", "10")),
10
),
Arguments.of(
"단일 헤더: CONTENT-LENGTH 파싱",
new RequestHeader(null, new Attributes().add("CONTENT-LENGTH", "10")),
10
),
Arguments.of(
"단일 헤더: content-length 파싱",
new RequestHeader(null, new Attributes().add("content-length", "10")),
10
),
Arguments.of(
"단일 헤더: CONTENT-length 파싱",
new RequestHeader(null, new Attributes().add("CONTENT-length", "10")),
10
),
Arguments.of(
"복수 헤더: CONTENT-LENGTH 파싱",
new RequestHeader(null, new Attributes().addAll(new HashMap<String, String>() {{
put("Host", "localhost:8080");
put("CONTENT-length", "15");
}})),
15
),
Arguments.of(
"복수 헤더: Content-Length 파싱",
new RequestHeader(null, new Attributes().addAll(new HashMap<String, String>() {{
put("Host", "localhost:8080");
put("Content-Length", "15");
}})),
15
)
);
}
}

0 comments on commit a78533d

Please sign in to comment.