#19 was : parsing inputStream

This commit is contained in:
haerong22
2022-09-01 03:28:40 +09:00
parent 0934c0f2a7
commit d3e4dc074c
6 changed files with 147 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
package org.example;
public class QueryString {
private final String key;
private final String value;
public QueryString(String key, String value) {
this.key = key;
this.value = value;
}
public boolean exist(String key) {
return this.key.equals(key);
}
public String getValue() {
return this.value;
}
}

View File

@@ -0,0 +1,31 @@
package org.example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class QueryStrings {
private final List<QueryString> queryStrings = new ArrayList<>();
public QueryStrings(String queryStringLine) {
String[] queryStringTokens = queryStringLine.split("&");
Arrays.stream(queryStringTokens)
.forEach(queryString -> {
String[] values = queryString.split("=");
if (values.length != 2) {
throw new IllegalArgumentException("잘못된 QueryString 포맷을 가진 문자열입니다.");
}
queryStrings.add(new QueryString(values[0], values[1]));
});
}
public String getValue(String key) {
return this.queryStrings.stream()
.filter(queryString -> queryString.exist(key))
.map(QueryString::getValue)
.findFirst()
.orElse(null);
}
}

View File

@@ -0,0 +1,53 @@
package org.example;
import java.util.Objects;
public class RequestLine {
private final String method;
private final String urlPath;
private QueryStrings queryStrings;
public RequestLine(String method, String urlPath, String queryString) {
this.method = method;
this.urlPath = urlPath;
this.queryStrings = new QueryStrings(queryString);
}
public RequestLine(String requestLine) {
String[] tokens = requestLine.split(" ");
this.method = tokens[0];
String[] urlPathTokens = tokens[1].split("\\?");
this.urlPath = urlPathTokens[0];
if (urlPathTokens.length == 2) {
this.queryStrings = new QueryStrings(urlPathTokens[1]);
}
}
public boolean isGetRequest() {
return "GET".equals(this.method);
}
public boolean matchPath(String requestPath) {
return this.urlPath.equals(requestPath);
}
public QueryStrings getQueryStrings() {
return this.queryStrings;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof RequestLine)) return false;
RequestLine that = (RequestLine) o;
return Objects.equals(method, that.method) && Objects.equals(urlPath, that.urlPath) && Objects.equals(queryStrings, that.queryStrings);
}
@Override
public int hashCode() {
return Objects.hash(method, urlPath, queryStrings);
}
}

View File

@@ -0,0 +1,14 @@
package org.example;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
public class QueryStringTest {
@Test
void createTest() {
QueryString queryString = new QueryString("operand1", "11");
Assertions.assertThat(queryString).isNotNull();
}
}

View File

@@ -0,0 +1,14 @@
package org.example;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
public class QueryStringsTest {
@Test
void createTest() {
QueryStrings queryStrings = new QueryStrings("operand1=11&operator=*&operand2=55");
Assertions.assertThat(queryStrings).isNotNull();
}
}

View File

@@ -0,0 +1,16 @@
package org.example;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class RequestLineTest {
@Test
void create() {
RequestLine requestLine = new RequestLine("GET /calculate?operand1=11&operator=*&operand2=55");
assertThat(requestLine).isNotNull();
assertThat(requestLine).isEqualTo(new RequestLine("GET", "/calculate", "operand1=11&operator=*&operand2=55"));
}
}