Skip to content

Commit

Permalink
feat: Context에 회원가입 기능 연동
Browse files Browse the repository at this point in the history
  • Loading branch information
apptie committed Sep 4, 2023
1 parent a7cb925 commit 3cfc7f6
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
);
}
}
Original file line number Diff line number Diff line change
@@ -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 protected]")
);

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 protected]")
);
request.initSessionManager(new SessionManager());

final Response actual = handler.service(request);

assertThat(actual.convertResponseMessage()).contains("302 Found");
}
}

0 comments on commit 3cfc7f6

Please sign in to comment.