Skip to content

Commit

Permalink
Merge pull request #4 from jinan159/chapter34/step4
Browse files Browse the repository at this point in the history
3장 요구사항 4 - 302 status code 적용
  • Loading branch information
jinan159 authored Jul 21, 2022
2 parents 06a92e1 + b9edd55 commit 311c7ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
19 changes: 18 additions & 1 deletion src/main/java/webserver/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void run() {
HttpRequestBody body = requestMessage.body();
registerNewUser(body);

responseDynamic(out);
responseRedirect(out, URI.create("/index.html"));
}

} catch (IOException e) {
Expand Down Expand Up @@ -81,6 +81,13 @@ private void responseDynamic(OutputStream out) {
responseBody(dos, body);
}

private void responseRedirect(OutputStream out, URI uri) {
DataOutputStream dos = new DataOutputStream(out);
byte[] body = new byte[]{};
response302Header(dos, uri);
responseBody(dos, body);
}

private boolean isStaticResourceRequest(URI uri) {
return !uri.getPath().equals(USER_CREATE_URI_PATH);
}
Expand All @@ -96,6 +103,16 @@ private void response200Header(DataOutputStream dos, int lengthOfBodyContent) {
}
}

private void response302Header(DataOutputStream dos, URI uri) {
try {
dos.writeBytes("HTTP/1.1 302 FOUND \r\n");
dos.writeBytes("Location: " + uri.getPath() + "\r\n");
dos.writeBytes("\r\n");
} catch (IOException e) {
log.error(e.getMessage());
}
}

private void responseBody(DataOutputStream dos, byte[] body) {
try {
dos.write(body, 0, body.length);
Expand Down
7 changes: 0 additions & 7 deletions src/test/java/webserver/HttpRequestMessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,4 @@ void parseTest() throws IOException {
assertThat(body.get("password")).isEqualTo("1234");
}
}

@Test
void test() {
String str = "userId=jay&password=1234";
System.out.println(str.length());
System.out.println(str.getBytes().length);
}
}
12 changes: 11 additions & 1 deletion src/test/java/webserver/RequestHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
import java.net.ServerSocket;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.Optional;
import java.util.Random;

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

// then
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.statusCode()).isEqualTo(302);

HttpHeaders headers = response.headers();
assertThat(headers).isNotNull();

Optional<String> locationHeader = headers.firstValue("Location");
assertThat(locationHeader.isPresent()).isTrue();
assertThat(locationHeader.orElseThrow()).isEqualTo("/index.html");

assertThat(savedUser).isNotNull();
assertThat(savedUser).isEqualTo(expectedUser);
}
Expand Down

0 comments on commit 311c7ae

Please sign in to comment.