Skip to content

Commit

Permalink
test: 통합 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kevstevie committed Sep 4, 2023
1 parent 77d5eff commit 2339099
Showing 1 changed file with 143 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package nextstep.org.apache.coyote.http11;

import static nextstep.jwp.db.InMemoryUserRepository.findByAccount;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.jupiter.api.Assertions.assertAll;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -58,4 +61,144 @@ void index() throws IOException {

assertThat(socket.output()).isEqualTo(expected);
}

@Test
void loginPage() throws IOException {
//given
final String HttpRequest = String.join("\r\n",
"GET /login HTTP/1.1 ",
"Host: localhost:8080 ",
"Connection: keep-alive ",
"",
"");

final var socket = new StubSocket(HttpRequest);
final Http11Processor processor = new Http11Processor(socket);

//when
processor.process(socket);

//then
final URL resource = getClass().getClassLoader().getResource("static/login.html");
var expected = "HTTP/1.1 200 OK \r\n" +
"Content-Type: text/html;charset=utf-8 \r\n" +
"Content-Length: " + new File(resource.getPath()).length() + " \r\n" +
"\r\n" +
new String(Files.readAllBytes(new File(resource.getFile()).toPath()));

assertThat(socket.output()).isEqualTo(expected);
}

@Test
void 로그인_성공() throws IOException {
//given
String body = "account=gugu&password=password";
final var request = String.join("\r\n",
"POST /login HTTP/1.1",
"Host: localhost:8080",
"Connection: keep-alive",
"Content-Length: " + body.length(),
"Content-Type: application/x-www-form-urlencoded",
"",
body);

final var socket = new StubSocket(request);
final Http11Processor processor = new Http11Processor(socket);

//when
processor.process(socket);

//then
final URL resource = getClass().getClassLoader().getResource("static/index.html");
var expected = "HTTP/1.1 302 Found \r\n" +
"Content-Length: " + new File(resource.getPath()).length();

assertThat(socket.output()).contains(expected);
}

@Test
void 로그인_실패() throws IOException {
//given
String body = "account=123&password=123";
final var request = String.join("\r\n",
"POST /login HTTP/1.1",
"Host: localhost:8080",
"Connection: keep-alive",
"Content-Length: " + body.length(),
"Content-Type: application/x-www-form-urlencoded",
"",
body);

final var socket = new StubSocket(request);
final Http11Processor processor = new Http11Processor(socket);

//when
processor.process(socket);

//then
final URL resource = getClass().getClassLoader().getResource("static/401.html");
var expected = "HTTP/1.1 401 Unauthorized \r\n" +
"Content-Type: text/html;charset=utf-8 \r\n" +
"Content-Length: " + new File(resource.getPath()).length();

assertThat(socket.output()).contains(expected);
}

@Test
void registerPage() throws IOException {
//given
final String HttpRequest = String.join("\r\n",
"GET /register HTTP/1.1 ",
"Host: localhost:8080 ",
"Connection: keep-alive ",
"",
"");

final var socket = new StubSocket(HttpRequest);
final Http11Processor processor = new Http11Processor(socket);

//when
processor.process(socket);

//then
final URL resource = getClass().getClassLoader().getResource("static/register.html");
var expected = "HTTP/1.1 200 OK \r\n" +
"Content-Type: text/html;charset=utf-8 \r\n" +
"Content-Length: " + new File(resource.getPath()).length() + " \r\n" +
"\r\n" +
new String(Files.readAllBytes(new File(resource.getFile()).toPath()));

assertThat(socket.output()).isEqualTo(expected);
}

@Test
void register() throws IOException {
//given
String body = "account=newnew&password=1234";
final var request = String.join("\r\n",
"POST /register HTTP/1.1",
"Host: localhost:8080",
"Connection: keep-alive",
"Content-Length: " + body.length(),
"Content-Type: application/x-www-form-urlencoded",
"",
body);

final var socket = new StubSocket(request);
final Http11Processor processor = new Http11Processor(socket);

//when
processor.process(socket);

//then
final URL resource = getClass().getClassLoader().getResource("static/index.html");
var expected = "HTTP/1.1 302 Found \r\n" +
"Content-Length: " + new File(resource.getPath()).length();

assertAll(
() -> assertThat(socket.output()).contains(expected),
() -> assertThatCode(() -> findByAccount("newnew").get()).doesNotThrowAnyException(),
() -> assertThat(findByAccount("newnew").get().getPassword()).isEqualTo("1234")
);
}
}

0 comments on commit 2339099

Please sign in to comment.