diff --git a/tomcat/src/main/java/org/apache/coyote/context/HelloWorldContext.java b/tomcat/src/main/java/org/apache/coyote/context/HelloWorldContext.java index 4739831178..e2f9dd210e 100644 --- a/tomcat/src/main/java/org/apache/coyote/context/HelloWorldContext.java +++ b/tomcat/src/main/java/org/apache/coyote/context/HelloWorldContext.java @@ -8,6 +8,7 @@ import org.apache.coyote.context.exception.InvalidRootContextPathException; import org.apache.coyote.handler.LoginHandler; import org.apache.coyote.handler.LoginPageHandler; +import org.apache.coyote.handler.RegisterHandler; import org.apache.coyote.handler.RegisterPageHandler; import org.apache.coyote.handler.ResourceHandler; import org.apache.coyote.handler.WelcomeHandler; @@ -50,6 +51,7 @@ private void initHandlers() { handlers.add(new LoginHandler("/login", rootContextPath)); handlers.add(new LoginPageHandler("/login", rootContextPath, "login.html", staticResourcePath)); handlers.add(new RegisterPageHandler("/register", rootContextPath, "register.html", staticResourcePath)); + handlers.add(new RegisterHandler("/register", rootContextPath)); } @Override diff --git a/tomcat/src/main/java/org/apache/coyote/handler/RegisterHandler.java b/tomcat/src/main/java/org/apache/coyote/handler/RegisterHandler.java new file mode 100644 index 0000000000..b0ac9f8081 --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/handler/RegisterHandler.java @@ -0,0 +1,64 @@ +package org.apache.coyote.handler; + +import java.io.IOException; +import nextstep.jwp.db.InMemoryUserRepository; +import nextstep.jwp.model.User; +import org.apache.coyote.Handler; +import org.apache.coyote.http.request.Request; +import org.apache.coyote.http.response.ContentType; +import org.apache.coyote.http.response.HttpStatusCode; +import org.apache.coyote.http.response.Response; +import org.apache.coyote.http.util.HeaderDto; +import org.apache.coyote.http.util.HttpConsts; +import org.apache.coyote.http.util.HttpMethod; + +public class RegisterHandler implements Handler { + + private static final String ACCOUNT_KEY = "account"; + private static final String PASSWORD_KEY = "password"; + private static final String EMAIL_KEY = "email"; + + private final String path; + private final String rootContextPath; + + public RegisterHandler(final String path, final String rootContextPath) { + this.path = path; + this.rootContextPath = rootContextPath; + } + + @Override + public boolean supports(final Request request) { + return isPostMethod(request) && isRegisterRequest(request); + } + + private boolean isPostMethod(final Request request) { + return request.matchesByMethod(HttpMethod.POST); + } + + private boolean isRegisterRequest(final Request request) { + return request.matchesByPath(path, rootContextPath) && request.hasQueryParameters(); + } + + @Override + public Response service(final Request request) throws IOException { + final String account = request.findQueryParameterValue(ACCOUNT_KEY); + final String password = request.findQueryParameterValue(PASSWORD_KEY); + final String email = request.findQueryParameterValue(EMAIL_KEY); + + if (account == null || password == null || email == null) { + return Response.of(request, HttpStatusCode.BAD_REQUEST, ContentType.JSON, HttpConsts.BLANK); + } + + final User user = new User(account, password, email); + + InMemoryUserRepository.save(user); + + return Response.of( + request, + HttpStatusCode.FOUND, + ContentType.JSON, + HttpConsts.BLANK, + new HeaderDto("Location", "/login") + ); + } +} diff --git a/tomcat/src/test/java/org/apache/coyote/handler/RegisterHandlerTest.java b/tomcat/src/test/java/org/apache/coyote/handler/RegisterHandlerTest.java new file mode 100644 index 0000000000..84bccbf5a8 --- /dev/null +++ b/tomcat/src/test/java/org/apache/coyote/handler/RegisterHandlerTest.java @@ -0,0 +1,92 @@ +package org.apache.coyote.handler; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import org.apache.coyote.http.SessionManager; +import org.apache.coyote.http.request.HttpRequestBody; +import org.apache.coyote.http.request.HttpRequestHeaders; +import org.apache.coyote.http.request.QueryParameters; +import org.apache.coyote.http.request.Request; +import org.apache.coyote.http.request.Url; +import org.apache.coyote.http.response.Response; +import org.apache.coyote.http.util.HttpMethod; +import org.apache.coyote.http.util.HttpVersion; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class RegisterHandlerTest { + + @Test + void 생성자는_경로와_rootContextPath를_전달하면_RegisterHandler를_초기화한다() { + final RegisterHandler actual = new RegisterHandler("/register", "/"); + + assertThat(actual).isNotNull(); + } + + @Test + void supports_메서드는_지원하는_요청인_경우_true를_반환한다() { + final RegisterHandler handler = new RegisterHandler("/register", "/"); + final HttpRequestHeaders headers = HttpRequestHeaders.from("Content-Type: application/json"); + final HttpMethod method = HttpMethod.findMethod("post"); + final Url url = Url.from("/register"); + final HttpVersion version = HttpVersion.findVersion("HTTP/1.1"); + final Request request = new Request( + headers, + method, + version, + url, + HttpRequestBody.EMPTY, + QueryParameters.fromBodyContent("account=asdf&password=asdf&email=asdf@asdf.com") + ); + + final boolean actual = handler.supports(request); + + assertThat(actual).isTrue(); + } + + @Test + void supports_메서드는_지원하지_않는_요청인_경우_false를_반환한다() { + final RegisterHandler handler = new RegisterHandler("/register", "/"); + final HttpRequestHeaders headers = HttpRequestHeaders.from("Content-Type: text/html;charset=utf-8"); + final HttpMethod method = HttpMethod.findMethod("get"); + final Url url = Url.from("/index.html"); + final HttpVersion version = HttpVersion.findVersion("HTTP/1.1"); + final Request request = new Request( + headers, + method, + version, + url, + HttpRequestBody.EMPTY, + QueryParameters.EMPTY + ); + + final boolean actual = handler.supports(request); + + assertThat(actual).isFalse(); + } + + @Test + void service_메서드는_요청을_처리하고_Response를_반환한다() throws IOException { + final RegisterHandler handler = new RegisterHandler("/register", "/"); + final HttpRequestHeaders headers = HttpRequestHeaders.from("Content-Type: application/json"); + final HttpMethod method = HttpMethod.findMethod("post"); + final Url url = Url.from("/register"); + final HttpVersion version = HttpVersion.findVersion("HTTP/1.1"); + final Request request = new Request( + headers, + method, + version, + url, + HttpRequestBody.EMPTY, + QueryParameters.fromBodyContent("account=asdf&password=asdf&email=asdf@asdf.com") + ); + request.initSessionManager(new SessionManager()); + + final Response actual = handler.service(request); + + assertThat(actual.convertResponseMessage()).contains("302 Found"); + } +}