Skip to content

Commit 311c7ae

Browse files
authored
Merge pull request #4 from jinan159/chapter34/step4
3장 요구사항 4 - 302 status code 적용
2 parents 06a92e1 + b9edd55 commit 311c7ae

3 files changed

Lines changed: 29 additions & 9 deletions

File tree

src/main/java/webserver/RequestHandler.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void run() {
4545
HttpRequestBody body = requestMessage.body();
4646
registerNewUser(body);
4747

48-
responseDynamic(out);
48+
responseRedirect(out, URI.create("/index.html"));
4949
}
5050

5151
} catch (IOException e) {
@@ -81,6 +81,13 @@ private void responseDynamic(OutputStream out) {
8181
responseBody(dos, body);
8282
}
8383

84+
private void responseRedirect(OutputStream out, URI uri) {
85+
DataOutputStream dos = new DataOutputStream(out);
86+
byte[] body = new byte[]{};
87+
response302Header(dos, uri);
88+
responseBody(dos, body);
89+
}
90+
8491
private boolean isStaticResourceRequest(URI uri) {
8592
return !uri.getPath().equals(USER_CREATE_URI_PATH);
8693
}
@@ -96,6 +103,16 @@ private void response200Header(DataOutputStream dos, int lengthOfBodyContent) {
96103
}
97104
}
98105

106+
private void response302Header(DataOutputStream dos, URI uri) {
107+
try {
108+
dos.writeBytes("HTTP/1.1 302 FOUND \r\n");
109+
dos.writeBytes("Location: " + uri.getPath() + "\r\n");
110+
dos.writeBytes("\r\n");
111+
} catch (IOException e) {
112+
log.error(e.getMessage());
113+
}
114+
}
115+
99116
private void responseBody(DataOutputStream dos, byte[] body) {
100117
try {
101118
dos.write(body, 0, body.length);

src/test/java/webserver/HttpRequestMessageTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,4 @@ void parseTest() throws IOException {
7474
assertThat(body.get("password")).isEqualTo("1234");
7575
}
7676
}
77-
78-
@Test
79-
void test() {
80-
String str = "userId=jay&password=1234";
81-
System.out.println(str.length());
82-
System.out.println(str.getBytes().length);
83-
}
8477
}

src/test/java/webserver/RequestHandlerTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
import java.net.ServerSocket;
1414
import java.net.URI;
1515
import java.net.http.HttpClient;
16+
import java.net.http.HttpHeaders;
1617
import java.net.http.HttpRequest;
1718
import java.net.http.HttpRequest.BodyPublisher;
1819
import java.net.http.HttpRequest.BodyPublishers;
1920
import java.net.http.HttpResponse;
2021
import java.net.http.HttpResponse.BodyHandlers;
22+
import java.util.Optional;
2123
import java.util.Random;
2224

2325
import static org.assertj.core.api.Assertions.assertThat;
@@ -61,7 +63,15 @@ void createUserTest() throws IOException, InterruptedException {
6163
User savedUser = DataBase.findUserById(expectedUser.getUserId());
6264

6365
// then
64-
assertThat(response.statusCode()).isEqualTo(200);
66+
assertThat(response.statusCode()).isEqualTo(302);
67+
68+
HttpHeaders headers = response.headers();
69+
assertThat(headers).isNotNull();
70+
71+
Optional<String> locationHeader = headers.firstValue("Location");
72+
assertThat(locationHeader.isPresent()).isTrue();
73+
assertThat(locationHeader.orElseThrow()).isEqualTo("/index.html");
74+
6575
assertThat(savedUser).isNotNull();
6676
assertThat(savedUser).isEqualTo(expectedUser);
6777
}

0 commit comments

Comments
 (0)