From 3fe89dce7fba733bcb529508c4a60a419c849b89 Mon Sep 17 00:00:00 2001 From: waterricecake Date: Mon, 4 Sep 2023 16:55:06 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=80=20=EC=98=A4=EB=A5=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jwp/controller/LoginController.java | 23 +++++++ .../jwp/controller/ViewController.java | 31 +++++++++- .../coyote/http11/handler/ApiHandler.java | 5 +- .../coyote/http11/response/Response.java | 6 +- .../coyote/http11/Http11ProcessorTest.java | 62 ++++++++++++++++++- 5 files changed, 118 insertions(+), 9 deletions(-) diff --git a/tomcat/src/main/java/nextstep/jwp/controller/LoginController.java b/tomcat/src/main/java/nextstep/jwp/controller/LoginController.java index 2befbe0571..9d85b50561 100644 --- a/tomcat/src/main/java/nextstep/jwp/controller/LoginController.java +++ b/tomcat/src/main/java/nextstep/jwp/controller/LoginController.java @@ -1,5 +1,8 @@ package nextstep.jwp.controller; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.util.HashMap; import java.util.Map; import nextstep.jwp.db.InMemorySession; @@ -10,6 +13,7 @@ import org.apache.coyote.http11.request.Request; import org.apache.coyote.http11.response.HttpStatus; import org.apache.coyote.http11.response.Response; +import org.apache.coyote.http11.servlet.Servlet; public class LoginController { @@ -27,7 +31,10 @@ public static Response login(Request request){ } return Response.builder() .status(HttpStatus.FOUND) + .contentType("html") .cookie(cookie) + .location("index.html") + .responseBody(getFile("index.html")) .build(); } @@ -40,6 +47,22 @@ public static Response signUp(Request request){ InMemoryUserRepository.save(user); return Response.builder() .status(HttpStatus.FOUND) + .contentType("html") + .location("index.html") + .responseBody(getFile("index.html")) .build(); } + + + private static String getFile(String fileName){ + try { + final var fileUrl = Servlet.class.getClassLoader().getResource("static/" + fileName); + final var fileBytes = Files.readAllBytes(new File(fileUrl.getFile()).toPath()); + return new String(fileBytes); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (NullPointerException e){ + return ""; + } + } } diff --git a/tomcat/src/main/java/nextstep/jwp/controller/ViewController.java b/tomcat/src/main/java/nextstep/jwp/controller/ViewController.java index 5025bf63b6..71a8b62ee2 100644 --- a/tomcat/src/main/java/nextstep/jwp/controller/ViewController.java +++ b/tomcat/src/main/java/nextstep/jwp/controller/ViewController.java @@ -1,19 +1,48 @@ package nextstep.jwp.controller; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import org.apache.coyote.http11.request.Request; import org.apache.coyote.http11.response.HttpStatus; import org.apache.coyote.http11.response.Response; +import org.apache.coyote.http11.servlet.Servlet; public class ViewController { - public static Response getIndex(Request request){ + public static Response getLogin(Request request){ return Response.builder() .status(HttpStatus.OK) + .contentType("html") + .responseBody(getFile("login.html")) .build(); } public static Response getRegister(Request request){ return Response.builder() .status(HttpStatus.OK) + .contentType("html") + .responseBody(getFile("register.html")) .build(); } + + public static Response getVoid(Request request){ + return Response.builder() + .status(HttpStatus.OK) + .responseBody("Hello world!") + .contentType("html") + .build(); + } + + + private static String getFile(String fileName){ + try { + final var fileUrl = Servlet.class.getClassLoader().getResource("static/" + fileName); + final var fileBytes = Files.readAllBytes(new File(fileUrl.getFile()).toPath()); + return new String(fileBytes); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (NullPointerException e){ + return ""; + } + } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/handler/ApiHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/handler/ApiHandler.java index 16c84b10a8..5a8fae020b 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/handler/ApiHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/handler/ApiHandler.java @@ -13,7 +13,8 @@ public class ApiHandler extends Handler{ private static final List apiList =new ArrayList<>(); static{ - addApi("/login", ViewController::getIndex,"login.html",false,false); + addApi("/", ViewController::getVoid,"Hello world!",false,false); + addApi("/login", ViewController::getLogin,"login.html",false,false); addApi("/register", ViewController::getRegister,"register.html",false,false); addApi("/login", LoginController::login,"index.html",false,true); addApi("/register", LoginController::signUp,"index.html",false, true); @@ -30,7 +31,7 @@ String getResponse(Request request) { boolean isHandle = api.isHandle(request); if(isHandle){ Response response = api.controller.apply(request); - return response.redirect(getFile(api.file),api.file).getResponse(); + return response.getResponse(); } } return next.getResponse(request); diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/Response.java b/tomcat/src/main/java/org/apache/coyote/http11/response/Response.java index 3c266f5af5..8f47631f03 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/Response.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/Response.java @@ -32,13 +32,11 @@ public static Response badResponse(HttpStatus httpStatus){ } public String getResponse(){ - return String.join("\r\n", "HTTP/1.1 " + status + " ", "Content-Type: text/" + contentType + ";charset=utf-8 ", "Content-Length: " + responseBody.getBytes().length + " ", - makeLocation(), - makeCookie(), + makeLocation() + makeCookie(), responseBody); } @@ -57,7 +55,7 @@ private String makeLocation(){ if(location==null){ return ""; } - return "location : " + location; + return "location : " + location + "\r\n"; } private String makeCookie(){ if(cookie == null){ diff --git a/tomcat/src/test/java/nextstep/org/apache/coyote/http11/Http11ProcessorTest.java b/tomcat/src/test/java/nextstep/org/apache/coyote/http11/Http11ProcessorTest.java index 512b919f09..3699dad1a6 100644 --- a/tomcat/src/test/java/nextstep/org/apache/coyote/http11/Http11ProcessorTest.java +++ b/tomcat/src/test/java/nextstep/org/apache/coyote/http11/Http11ProcessorTest.java @@ -1,5 +1,7 @@ package nextstep.org.apache.coyote.http11; +import com.sun.jdi.connect.Connector.Argument; +import java.util.stream.Stream; import support.StubSocket; import org.apache.coyote.http11.Http11Processor; import org.junit.jupiter.api.Test; @@ -12,7 +14,6 @@ import static org.assertj.core.api.Assertions.assertThat; class Http11ProcessorTest { - @Test void process() { // given @@ -51,9 +52,66 @@ void index() throws IOException { // then final URL resource = getClass().getClassLoader().getResource("static/index.html"); + final String responseBody = new String(Files.readAllBytes(new File(resource.getFile()).toPath())); + var expected = "HTTP/1.1 200 OK \r\n" + + "Content-Type: text/html;charset=utf-8 \r\n" + + "Content-Length: " + responseBody.getBytes().length + " \r\n" + + "\r\n"+ + responseBody; + + assertThat(socket.output()).isEqualTo(expected); + } + + @Test + void login() throws IOException { + // given + final String httpRequest= String.join("\r\n", + "GET /login.html HTTP/1.1 ", + "Host: localhost:8080 ", + "Connection: keep-alive ", + "", + ""); + + final var socket = new StubSocket(httpRequest); + final Http11Processor processor = new Http11Processor(socket); + + // when + processor.process(socket); + + // then + final URL resource = getClass().getClassLoader().getResource("static/login.html"); + final String responseBody = new String(Files.readAllBytes(new File(resource.getFile()).toPath())); + var expected = "HTTP/1.1 200 OK \r\n" + + "Content-Type: text/html;charset=utf-8 \r\n" + + "Content-Length: " + responseBody.getBytes().length + " \r\n" + + "\r\n"+ + new String(Files.readAllBytes(new File(resource.getFile()).toPath())); + + assertThat(socket.output()).isEqualTo(expected); + } + + @Test + void register() throws IOException { + // given + final String httpRequest= String.join("\r\n", + "GET /register.html HTTP/1.1 ", + "Host: localhost:8080 ", + "Connection: keep-alive ", + "", + ""); + + final var socket = new StubSocket(httpRequest); + final Http11Processor processor = new Http11Processor(socket); + + // when + processor.process(socket); + + // then + final URL resource = getClass().getClassLoader().getResource("static/register.html"); + final String responseBody = new String(Files.readAllBytes(new File(resource.getFile()).toPath())); var expected = "HTTP/1.1 200 OK \r\n" + "Content-Type: text/html;charset=utf-8 \r\n" + - "Content-Length: 5564 \r\n" + + "Content-Length: " + responseBody.getBytes().length + " \r\n" + "\r\n"+ new String(Files.readAllBytes(new File(resource.getFile()).toPath()));