Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/main/java/webserver/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,19 @@ public void run() {
);

DataBase.addUser(newUser);
responseMessage = response302Header();

responseMessage = "HTTP/1.1 302 Found" + System.lineSeparator() +
"Location: http://localhost:8080/index.html";
} else if (path.equals("/user/login")) {
Map<String, String> parameters = request.getRequestMessage().getParameters();
User findUser = DataBase.findUserById(parameters.get("userId"));

if (findUser == null) {
responseMessage = response302Header();
} else if (findUser.getPassword().equals(parameters.get("password"))) {
responseMessage = response302HeaderWithCookie("/user/list.html","logined=true");
} else {
responseMessage = response302HeaderWithCookie("/user/login_failed.html","logined=false");
}
}

Response response = Response.from(responseMessage);
Expand All @@ -101,4 +111,18 @@ public void run() {
log.error(e.getMessage(), e);
}
}

private String response302Header() {
String responseMessage = "HTTP/1.1 302 Found" + System.lineSeparator() +
"Location: /index.html" + System.lineSeparator();
return responseMessage;
}

private String response302HeaderWithCookie(String location, String cookie) {
String responseMessage = "HTTP/1.1 302 Found" + System.lineSeparator() +
"Location: "+location+ System.lineSeparator()+
"Set-Cookie: "+cookie+"; Path=/";
return responseMessage;
}

Comment on lines +115 to +127
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋네요 Header 클래스에 static 메소드 같은걸로 잡아놓아도 괜찮을 것 같습니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그런데 이게 지금 단계에서만 리팩토링해야되는게 아니기 때문에(다른 부분도 함께 되야 하니까) 따로 이슈 남기고 작업해봐도 괜찮을 것 같아요

}
127 changes: 126 additions & 1 deletion src/test/java/webserver/RequestHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import db.DataBase;
import model.User;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -18,6 +21,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class RequestHandlerTest {
@ParameterizedTest
@MethodSource("run")
Expand Down Expand Up @@ -100,7 +104,7 @@ static Stream<Arguments> run() throws IOException {
"" + System.lineSeparator() +
"userId=dae&password=dae&name=dae&email=dae%40dae",
"HTTP/1.1 302 Found" + System.lineSeparator() +
"Location: http://localhost:8080/index.html" + System.lineSeparator()
"Location: /index.html" + System.lineSeparator()
)
);
}
Expand Down Expand Up @@ -194,4 +198,125 @@ static Stream<Arguments> runWithFailToCreateUser() {
"a=b"
));
}

@ParameterizedTest
@MethodSource("로그인_테스트")
void 로그인_테스트(String 테스트설명, String message, String expectedResponseMessage) throws IOException {

DataBase.addUser(new User("1234", "1234", "1234", "1234@1234"));

int port = ThreadLocalRandom.current().nextInt(50000, 60000);
ServerSocket server = new ServerSocket(port);

Socket browser = new Socket("localhost", port);

RequestHandler requestHandler = new RequestHandler(server.accept());

BufferedOutputStream browserStream = new BufferedOutputStream(browser.getOutputStream());

browserStream.write(message.getBytes(StandardCharsets.UTF_8));
browserStream.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
browserStream.flush();

requestHandler.run();


BufferedReader br = new BufferedReader(new InputStreamReader(browser.getInputStream()));

assertThat(br.lines().collect(Collectors.joining(System.lineSeparator()))).describedAs(테스트설명).isEqualTo(expectedResponseMessage);
}

static Stream<Arguments> 로그인_테스트() {
return Stream.of(
Arguments.arguments(
"로그인성공",
"POST /user/login HTTP/1.1" + System.lineSeparator() +
"Host: localhost:8080" + System.lineSeparator() +
"Connection: keep-alive" + System.lineSeparator() +
"Content-Length: 53" + System.lineSeparator() +
"Cache-Control: max-age=0" + System.lineSeparator() +
"sec-ch-ua: \"Google Chrome\";v=\"89\", \"Chromium\";v=\"89\", \";Not A Brand\";v=\"99\"" + System.lineSeparator() +
"sec-ch-ua-mobile: ?0" + System.lineSeparator() +
"Upgrade-Insecure-Requests: 1" + System.lineSeparator() +
"Origin: http://localhost:8080" + System.lineSeparator() +
"Content-Type: application/x-www-form-urlencoded" + System.lineSeparator() +
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" + System.lineSeparator() +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" + System.lineSeparator() +
"Sec-Fetch-Site: same-origin" + System.lineSeparator() +
"Sec-Fetch-Mode: navigate" + System.lineSeparator() +
"Sec-Fetch-User: ?1" + System.lineSeparator() +
"Sec-Fetch-Dest: document" + System.lineSeparator() +
"Referer: http://localhost:8080/user/form.html" + System.lineSeparator() +
"Accept-Encoding: gzip, deflate, br" + System.lineSeparator() +
"Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7" + System.lineSeparator() +
"Cookie: _ga=GA1.1.773336800.1611186274; Idea-dc7ca9b6=ac856d6e-e872-46ac-b153-000bdad105ec" + System.lineSeparator() +
"" + System.lineSeparator() +
"userId=1234&password=1234",

"HTTP/1.1 302 Found" + System.lineSeparator() +
"Location: /user/list.html" + System.lineSeparator() +
"Set-Cookie: logined=true; Path=/" + System.lineSeparator()
),
Arguments.arguments(
"올바르지 않은 비밀번호 테스트",
"POST /user/login HTTP/1.1" + System.lineSeparator() +
"Host: localhost:8080" + System.lineSeparator() +
"Connection: keep-alive" + System.lineSeparator() +
"Content-Length: 53" + System.lineSeparator() +
"Cache-Control: max-age=0" + System.lineSeparator() +
"sec-ch-ua: \"Google Chrome\";v=\"89\", \"Chromium\";v=\"89\", \";Not A Brand\";v=\"99\"" + System.lineSeparator() +
"sec-ch-ua-mobile: ?0" + System.lineSeparator() +
"Upgrade-Insecure-Requests: 1" + System.lineSeparator() +
"Origin: http://localhost:8080" + System.lineSeparator() +
"Content-Type: application/x-www-form-urlencoded" + System.lineSeparator() +
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" + System.lineSeparator() +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" + System.lineSeparator() +
"Sec-Fetch-Site: same-origin" + System.lineSeparator() +
"Sec-Fetch-Mode: navigate" + System.lineSeparator() +
"Sec-Fetch-User: ?1" + System.lineSeparator() +
"Sec-Fetch-Dest: document" + System.lineSeparator() +
"Referer: http://localhost:8080/user/form.html" + System.lineSeparator() +
"Accept-Encoding: gzip, deflate, br" + System.lineSeparator() +
"Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7" + System.lineSeparator() +
"Cookie: _ga=GA1.1.773336800.1611186274; Idea-dc7ca9b6=ac856d6e-e872-46ac-b153-000bdad105ec" + System.lineSeparator() +
"" + System.lineSeparator() +
"userId=1234&password=4321",

"HTTP/1.1 302 Found" + System.lineSeparator() +
"Location: /user/login_failed.html" + System.lineSeparator() +
"Set-Cookie: logined=false; Path=/" + System.lineSeparator()
),
Arguments.arguments(
"유효하지 않은 계정 로그인 테스트",
"POST /user/login HTTP/1.1" + System.lineSeparator() +
"Host: localhost:8080" + System.lineSeparator() +
"Connection: keep-alive" + System.lineSeparator() +
"Content-Length: 53" + System.lineSeparator() +
"Cache-Control: max-age=0" + System.lineSeparator() +
"sec-ch-ua: \"Google Chrome\";v=\"89\", \"Chromium\";v=\"89\", \";Not A Brand\";v=\"99\"" + System.lineSeparator() +
"sec-ch-ua-mobile: ?0" + System.lineSeparator() +
"Upgrade-Insecure-Requests: 1" + System.lineSeparator() +
"Origin: http://localhost:8080" + System.lineSeparator() +
"Content-Type: application/x-www-form-urlencoded" + System.lineSeparator() +
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" + System.lineSeparator() +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" + System.lineSeparator() +
"Sec-Fetch-Site: same-origin" + System.lineSeparator() +
"Sec-Fetch-Mode: navigate" + System.lineSeparator() +
"Sec-Fetch-User: ?1" + System.lineSeparator() +
"Sec-Fetch-Dest: document" + System.lineSeparator() +
"Referer: http://localhost:8080/user/form.html" + System.lineSeparator() +
"Accept-Encoding: gzip, deflate, br" + System.lineSeparator() +
"Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7" + System.lineSeparator() +
"Cookie: _ga=GA1.1.773336800.1611186274; Idea-dc7ca9b6=ac856d6e-e872-46ac-b153-000bdad105ec" + System.lineSeparator() +
"" + System.lineSeparator() +
"userId=emptyId&password=emptyPw",

"HTTP/1.1 302 Found" + System.lineSeparator() +
"Location: /index.html" + System.lineSeparator()
)

);
}


}