Skip to content

Commit 035a2c8

Browse files
authored
[톰캣 구현하기 - 1, 2단계] 라온(문채원) 미션 제출합니다. (#376)
* test: FileTest 구현 * feat: step1 - index.html 응답 구현 * feat: step1 - CSS 지원 구현 * feat: step1 - query string 파싱 구현 * feat: step2 - http status code 302 구현 * refactor: step2 - http status code 302 구현 * refactor: step2 - http status code 302 구현 * feat: step2 - post 방식으로 회원가입 구현 * refactor: step2 - post 방식으로 로그인 리팩토링 * feat: step2 - 회원가입 저장 로직 구현 * feat: step2 - cookie에 JSESSIONID 값 저장 구현 * feat: step2 - session 구현 * refactor: HttpRequest, HttpResponse 객체 생성 * refactor: handleRequest 메서드 반환 타입 변경 * refactor: HttpStatusCode 구현 * refactor: HttpRequestParser 구현 * refactor: HttpResponse 정팩메 수정 * refactor: HttpRequestParser에게 책임 부여 * refactor: HttpHeaders 객체 생성 * test: IOStreamTest 구현
1 parent 68db530 commit 035a2c8

File tree

15 files changed

+563
-36
lines changed

15 files changed

+563
-36
lines changed

study/src/test/java/study/FileTest.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package study;
22

3-
import org.junit.jupiter.api.DisplayName;
4-
import org.junit.jupiter.api.Test;
3+
import static org.assertj.core.api.Assertions.assertThat;
54

5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.net.URL;
8+
import java.nio.file.Files;
69
import java.nio.file.Path;
7-
import java.util.Collections;
810
import java.util.List;
9-
10-
import static org.assertj.core.api.Assertions.assertThat;
11+
import org.junit.jupiter.api.DisplayName;
12+
import org.junit.jupiter.api.Test;
1113

1214
/**
1315
* 웹서버는 사용자가 요청한 html 파일을 제공 할 수 있어야 한다.
@@ -28,7 +30,8 @@ class FileTest {
2830
final String fileName = "nextstep.txt";
2931

3032
// todo
31-
final String actual = "";
33+
final URL resource = getClass().getClassLoader().getResource(fileName);
34+
final String actual = resource.getFile();
3235

3336
assertThat(actual).endsWith(fileName);
3437
}
@@ -40,14 +43,16 @@ class FileTest {
4043
* File, Files 클래스를 사용하여 파일의 내용을 읽어보자.
4144
*/
4245
@Test
43-
void 파일의_내용을_읽는다() {
46+
void 파일의_내용을_읽는다() throws IOException {
4447
final String fileName = "nextstep.txt";
4548

49+
final URL resource = getClass().getClassLoader().getResource(fileName);
50+
4651
// todo
47-
final Path path = null;
52+
final Path path = new File(resource.getPath()).toPath();
4853

4954
// todo
50-
final List<String> actual = Collections.emptyList();
55+
final List<String> actual = Files.readAllLines(path);
5156

5257
assertThat(actual).containsOnly("nextstep");
5358
}

study/src/test/java/study/IOStreamTest.java

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
package study;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.mockito.Mockito.atLeastOnce;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.verify;
7+
8+
import java.io.BufferedInputStream;
9+
import java.io.BufferedOutputStream;
10+
import java.io.BufferedReader;
11+
import java.io.ByteArrayInputStream;
12+
import java.io.ByteArrayOutputStream;
13+
import java.io.FilterInputStream;
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.io.InputStreamReader;
17+
import java.io.OutputStream;
318
import org.junit.jupiter.api.DisplayName;
419
import org.junit.jupiter.api.Nested;
520
import org.junit.jupiter.api.Test;
621

7-
import java.io.*;
8-
9-
import static org.assertj.core.api.Assertions.assertThat;
10-
import static org.mockito.Mockito.*;
11-
1222
/**
1323
* 자바는 스트림(Stream)으로부터 I/O를 사용한다.
1424
* 입출력(I/O)은 하나의 시스템에서 다른 시스템으로 데이터를 이동 시킬 때 사용한다.
@@ -53,7 +63,7 @@ class OutputStream_학습_테스트 {
5363
* todo
5464
* OutputStream 객체의 write 메서드를 사용해서 테스트를 통과시킨다
5565
*/
56-
66+
outputStream.write(bytes);
5767
final String actual = outputStream.toString();
5868

5969
assertThat(actual).isEqualTo("nextstep");
@@ -78,6 +88,7 @@ class OutputStream_학습_테스트 {
7888
* flush를 사용해서 테스트를 통과시킨다.
7989
* ByteArrayOutputStream과 어떤 차이가 있을까?
8090
*/
91+
outputStream.flush();
8192

8293
verify(outputStream, atLeastOnce()).flush();
8394
outputStream.close();
@@ -96,6 +107,11 @@ class OutputStream_학습_테스트 {
96107
* try-with-resources를 사용한다.
97108
* java 9 이상에서는 변수를 try-with-resources로 처리할 수 있다.
98109
*/
110+
try (OutputStream ignored = outputStream) {
111+
// outputStream을 사용하는 코드
112+
} catch (IOException e) {
113+
e.printStackTrace();
114+
}
99115

100116
verify(outputStream, atLeastOnce()).close();
101117
}
@@ -128,7 +144,16 @@ class InputStream_학습_테스트 {
128144
* todo
129145
* inputStream에서 바이트로 반환한 값을 문자열로 어떻게 바꿀까?
130146
*/
131-
final String actual = "";
147+
148+
byte[] byteArray = new byte[1024];
149+
int bytesRead;
150+
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
151+
while ((bytesRead = inputStream.read(byteArray)) != -1) {
152+
byteArrayOutputStream.write(byteArray, 0, bytesRead);
153+
}
154+
155+
// 바이트 배열을 UTF-8 문자열로 변환
156+
final String actual = new String(byteArrayOutputStream.toByteArray(), "UTF-8");
132157

133158
assertThat(actual).isEqualTo("🤩");
134159
assertThat(inputStream.read()).isEqualTo(-1);
@@ -148,6 +173,11 @@ class InputStream_학습_테스트 {
148173
* try-with-resources를 사용한다.
149174
* java 9 이상에서는 변수를 try-with-resources로 처리할 수 있다.
150175
*/
176+
try (InputStream ignored = inputStream) {
177+
// inputStream을 사용하는 코드
178+
} catch (IOException e) {
179+
e.printStackTrace();
180+
}
151181

152182
verify(inputStream, atLeastOnce()).close();
153183
}
@@ -169,12 +199,12 @@ class FilterStream_학습_테스트 {
169199
* 버퍼 크기를 지정하지 않으면 버퍼의 기본 사이즈는 얼마일까?
170200
*/
171201
@Test
172-
void 필터인_BufferedInputStream를_사용해보자() {
202+
void 필터인_BufferedInputStream를_사용해보자() throws IOException {
173203
final String text = "필터에 연결해보자.";
174204
final InputStream inputStream = new ByteArrayInputStream(text.getBytes());
175-
final InputStream bufferedInputStream = null;
205+
final InputStream bufferedInputStream = new BufferedInputStream(inputStream);
176206

177-
final byte[] actual = new byte[0];
207+
final byte[] actual = bufferedInputStream.readAllBytes();
178208

179209
assertThat(bufferedInputStream).isInstanceOf(FilterInputStream.class);
180210
assertThat(actual).isEqualTo("필터에 연결해보자.".getBytes());
@@ -206,6 +236,14 @@ class InputStreamReader_학습_테스트 {
206236
final InputStream inputStream = new ByteArrayInputStream(emoji.getBytes());
207237

208238
final StringBuilder actual = new StringBuilder();
239+
try (final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
240+
String line;
241+
while ((line = bufferedReader.readLine()) != null) {
242+
actual.append(line).append("\r\n");
243+
}
244+
} catch (IOException e) {
245+
throw new RuntimeException(e);
246+
}
209247

210248
assertThat(actual).hasToString(emoji);
211249
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package nextstep.jwp.model;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class Session {
7+
8+
private final String id;
9+
private final Map<String, Object> values = new HashMap<>();
10+
11+
public Session(final String id) {
12+
this.id = id;
13+
}
14+
15+
public String getId() {
16+
return id;
17+
}
18+
19+
public void setAttribute(final String name, final Object value) {
20+
values.put(name, value);
21+
}
22+
}

tomcat/src/main/java/org/apache/catalina/Manager.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.apache.catalina;
22

3-
import jakarta.servlet.http.HttpSession;
4-
53
import java.io.IOException;
4+
import nextstep.jwp.model.Session;
65

76
/**
87
* A <b>Manager</b> manages the pool of Sessions that are associated with a
@@ -29,7 +28,7 @@ public interface Manager {
2928
*
3029
* @param session Session to be added
3130
*/
32-
void add(HttpSession session);
31+
void add(Session session);
3332

3433
/**
3534
* Return the active Session, associated with this Manager, with the
@@ -45,12 +44,12 @@ public interface Manager {
4544
* @return the request session or {@code null} if a session with the
4645
* requested ID could not be found
4746
*/
48-
HttpSession findSession(String id) throws IOException;
47+
Session findSession(String id) throws IOException;
4948

5049
/**
5150
* Remove this Session from the active Sessions for this Manager.
5251
*
5352
* @param session Session to be removed
5453
*/
55-
void remove(HttpSession session);
54+
void remove(Session session);
5655
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.apache.catalina;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import nextstep.jwp.model.Session;
6+
7+
public class SessionManager implements Manager {
8+
9+
private static final Map<String, Session> SESSIONS = new HashMap<>();
10+
11+
@Override
12+
public void add(final Session session) {
13+
SESSIONS.put(session.getId(), session);
14+
}
15+
16+
@Override
17+
public Session findSession(final String id) {
18+
return SESSIONS.get(id);
19+
}
20+
21+
@Override
22+
public void remove(final Session session) {
23+
SESSIONS.remove(session.getId());
24+
}
25+
26+
public SessionManager() {}
27+
}

0 commit comments

Comments
 (0)