Skip to content

Commit

Permalink
refactor: 회원 가입시 공백이 들어오면 bad request로 처리방식 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
seokhwan-an committed Sep 13, 2023
1 parent ea39cdc commit f1f9360
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ public class RegisterController extends AbstractController {
@Override
protected void doPost(final HttpRequest request, final HttpResponse response) {
if (request.hasBlankRegisterUserBody()) {
response.setStatusCode(StatusCode.FOUND)
.addHeader(LOCATION, REGISTER_PAGE)
.setBody("입력한 아이디, 비밀번호, 이메일에는 공백이 들어오면 안됩니다.");
final String body = "입력한 아이디, 비밀번호, 이메일에는 공백이 들어오면 안됩니다.";
response.setStatusCode(StatusCode.BAD_REQUEST)
.addHeader(CONTENT_TYPE, ContentType.HTML.getValue() + CHARSET_UTF_8)
.addHeader(CONTENT_LENGTH, String.valueOf(body.getBytes().length))
.setBody(body);
return;
}
final User registerUser = new User(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class RequestBody {
private static final String KEY_VALUE_DELIMITER = "=";
private static final int KEY_INDEX = 0;
private static final int VALUE_INDEX = 1;
private static final int VALUE_EMPTY_SIZE = 1;
private static final String SPACE = " ";

private final Map<String, String> body;

Expand All @@ -22,8 +24,14 @@ public static RequestBody fromRequest(final String request) {
final Map<String, String> body = Arrays.stream(requestBodies)
.map(headerContent -> headerContent.split(KEY_VALUE_DELIMITER))
.collect(Collectors.toMap(

headerContent -> headerContent[KEY_INDEX],
headerContent -> headerContent[VALUE_INDEX]));
headerContent -> {
if (headerContent.length == VALUE_EMPTY_SIZE) {
return SPACE;
}
return headerContent[VALUE_INDEX];
}));
return new RequestBody(body);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum StatusCode {

OK(200, "OK"),
FOUND(302, "Found"),
BAD_REQUEST(400, "Bad Request"),
NOT_FOUND(404, "Not Found"),
METHOD_NOT_ALLOWED(405, "Method Not Allow");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ void invalid_input_register_info(
registerController.doPost(httpRequest, httpResponse);

// then
final String expected = "HTTP/1.1 302 Found \r\n" +
"Location: /register.html \r\n" +
final String expected = "HTTP/1.1 400 Bad Request \r\n" +
"Content-Type: text/html;charset=utf-8 \r\n" +
"Content-Length: 87 \r\n" +
"\r\n" +
"입력한 아이디, 비밀번호, 이메일에는 공백이 들어오면 안됩니다.";

Expand Down

0 comments on commit f1f9360

Please sign in to comment.