-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
tomcat/src/main/java/org/apache/coyote/handler/RegisterHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
tomcat/src/test/java/org/apache/coyote/handler/RegisterHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |