diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Response.java b/tomcat/src/main/java/org/apache/coyote/http11/Response.java index 5629f82b07..b1e52365fc 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Response.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Response.java @@ -31,14 +31,4 @@ public static String of(final String contentType, final String responseBody, fin "", responseBody); } - - public static String ofRedirect(final String contentType, final String responseBody, final String status, final HttpCookie httpCookie) { - return String.join("\r\n", - "HTTP/1.1 " + status + " ", - "Set-Cookie: " + "JSESSIONID=" + httpCookie.getJSessionId() + " ", - "Content-Type: " + contentType + "charset=utf-8 ", - "Content-Length: " + responseBody.getBytes().length + " ", - "", - responseBody); - } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/enums/ContentType.java b/tomcat/src/main/java/org/apache/coyote/http11/enums/ContentType.java index 625d5f71bf..8b3992d10c 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/enums/ContentType.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/enums/ContentType.java @@ -1,7 +1,5 @@ package org.apache.coyote.http11.enums; -import java.util.Arrays; - public enum ContentType { JS(".js","Application/javascript;"), @@ -17,11 +15,13 @@ public enum ContentType { } public static String getContentType(final String path) { - return Arrays.stream(ContentType.values()) - .filter(value -> path.endsWith(value.getFileType())) - .findFirst() - .orElse(HTML) - .getPath(); + if (path.endsWith(JS.getFileType())) { + return JS.getPath(); + } + if (path.endsWith(CSS.getFileType())) { + return CSS.getPath(); + } + return HTML.getPath(); } public String getFileType() { diff --git a/tomcat/src/test/java/nextstep/org/apache/coyote/http11/Http11ProcessorTest.java b/tomcat/src/test/java/nextstep/org/apache/coyote/http11/Http11ProcessorTest.java index fd803a98b0..dd6ccf7000 100644 --- a/tomcat/src/test/java/nextstep/org/apache/coyote/http11/Http11ProcessorTest.java +++ b/tomcat/src/test/java/nextstep/org/apache/coyote/http11/Http11ProcessorTest.java @@ -1,8 +1,11 @@ package nextstep.org.apache.coyote.http11; -import support.StubSocket; +import nextstep.jwp.model.User; import org.apache.coyote.http11.Http11Processor; +import org.apache.coyote.http11.cookie.HttpCookie; +import org.apache.coyote.http11.cookie.SessionManager; import org.junit.jupiter.api.Test; +import support.StubSocket; import java.io.File; import java.io.IOException; @@ -31,6 +34,214 @@ void index() throws IOException { "", new String(Files.readAllBytes(new File(resource.getFile()).toPath()))); + assertThat(socket.output()).contains(expected); + } + + @Test + void index_css() { + // given + final String request = String.join("\r\n", "GET /css/styles.css HTTP/1.1 ", + "Host: localhost:8080 ", + "Accept: text/css,*/*;q=0.1 ", + "Connection: keep-alive ", + "", + ""); + final var socket = new StubSocket(request); + final var processor = new Http11Processor(socket); + + // when + processor.process(socket); + + // then + var expected = String.join("\r\n", + "HTTP/1.1 200 OK ", + "Content-Type: text/css;charset=utf-8 "); + + assertThat(socket.output()).contains(expected); + } + + @Test + void index_js() { + // given + final String request = String.join("\r\n", "GET scripts.js HTTP/1.1 ", + "Host: localhost:8080 ", + "Accept: text/css,*/*;q=0.1 ", + "Connection: keep-alive ", + "", + ""); + final var socket = new StubSocket(request); + final var processor = new Http11Processor(socket); + + // when + processor.process(socket); + + // then + var expected = String.join("\r\n", + "HTTP/1.1 200 OK ", + "Content-Type: Application/javascript;charset=utf-8 "); + + assertThat(socket.output()).contains(expected); + } + + @Test + void login_success_redirect_index() throws IOException { + // given + final String request = String.join("\r\n", "GET /login?account=gugu&password=password HTTP/1.1\r\nHost: localhost:8080\r\n\r\n"); + + final var socket = new StubSocket(request); + final var processor = new Http11Processor(socket); + final URL resource = getClass().getClassLoader().getResource("static/index.html"); + + // when + processor.process(socket); + + // then + var expected = String.join("\r\n", + "HTTP/1.1 200 OK ", + "Content-Type: text/html;charset=utf-8 ", + "Content-Length: 5564 ", + "", + new String(Files.readAllBytes(new File(resource.getFile()).toPath()))); + + assertThat(socket.output()).isEqualTo(expected); + } + + @Test + void login_failed_redirect_401() throws IOException { + // given + final String request = String.join("\r\n", "GET /login?account=judy&password=j HTTP/1.1\r\nHost: localhost:8080\r\n\r\n"); + + final var socket = new StubSocket(request); + final var processor = new Http11Processor(socket); + final URL resource = getClass().getClassLoader().getResource("static/401.html"); + + // when + processor.process(socket); + + // then + var expected = String.join("\r\n", + "HTTP/1.1 401 Unauthorized ", + "Content-Type: text/html;charset=utf-8 ", + "Content-Length: 2426 ", + "", + new String(Files.readAllBytes(new File(resource.getFile()).toPath()))); + assertThat(socket.output()).isEqualTo(expected); } + + @Test + void register_redirect_index() throws IOException { + // given + final var socket = new StubSocket("GET /register HTTP/1.1\r\nHost: localhost:8080\r\n\r\n"); + final var processor = new Http11Processor(socket); + final URL resource = getClass().getClassLoader().getResource("static/register.html"); + + // when + processor.process(socket); + + // then + var expected = String.join("\r\n", + "HTTP/1.1 200 OK ", + "Content-Type: text/html;charset=utf-8 ", + "Content-Length: 4319 ", + "", + new String(Files.readAllBytes(new File(resource.getFile()).toPath()))); + + assertThat(socket.output()).contains(expected); + } + + @Test + void login_not_saved_jsession() throws IOException { + // given + final String request = String.join("\r\n", "GET /login HTTP/1.1 ", + "Host: localhost:8080 ", + "Connection: keep-alive ", + "Cookie: JSESSIONID=74262fcd-872c-4bcb-ace8-4bb003882817", + "Content-Type: application/x-www-form-urlencoded ", + "Accept: */* ", + "", + ""); + + final var socket = new StubSocket(request); + final var processor = new Http11Processor(socket); + final URL resource = getClass().getClassLoader().getResource("static/login.html"); + + // when + processor.process(socket); + + // then + var expected = String.join("\r\n", + "HTTP/1.1 200 OK ", + "Content-Type: text/html;charset=utf-8 ", + "Content-Length: 3796 ", + "", + new String(Files.readAllBytes(new File(resource.getFile()).toPath()))); + + assertThat(socket.output()).isEqualTo(expected); + } + + @Test + void login_saved_jsession_redirect_index() throws IOException { + // given + final User judy = new User("judy", "judy", "judy@naver.com"); + final HttpCookie httpCookie = new HttpCookie(); + httpCookie.changeJSessionId("74262fcd-872c-4bcb-ace8-4bb003882818"); + SessionManager.add(judy, httpCookie); + + final String request = String.join("\r\n", "GET /login HTTP/1.1 ", + "Host: localhost:8080 ", + "Connection: keep-alive ", + "Cookie: JSESSIONID=74262fcd-872c-4bcb-ace8-4bb003882818", + "Content-Type: application/x-www-form-urlencoded ", + "Accept: */* ", + "", + ""); + + final var socket = new StubSocket(request); + final var processor = new Http11Processor(socket); + final URL resource = getClass().getClassLoader().getResource("static/index.html"); + + // when + processor.process(socket); + + // then + var expected = String.join("\r\n", + "HTTP/1.1 200 OK ", + "Content-Type: text/html;charset=utf-8 ", + "Content-Length: 5564 ", + "", + new String(Files.readAllBytes(new File(resource.getFile()).toPath()))); + + assertThat(socket.output()).isEqualTo(expected); + } + + @Test + void register_post() throws IOException { + // given + final String request = String.join("\r\n", "POST /register HTTP/1.1 ", + "Host: localhost:8080 ", + "Connection: keep-alive ", + "Content-Length: 80 ", + "Content-Type: application/x-www-form-urlencoded ", + "Accept: */* ", + "", + "account=gugu&password=password&email=hkkang%40woowahan.com "); + + final var socket = new StubSocket(request); + final var processor = new Http11Processor(socket); + final URL resource = getClass().getClassLoader().getResource("static/index.html"); + + // when + processor.process(socket); + + // then + var expected = String.join("\r\n", + "HTTP/1.1 200 OK ", + "Content-Type: text/html;charset=utf-8 ", + "Content-Length: 5564 ", + "", + new String(Files.readAllBytes(new File(resource.getFile()).toPath()))); + + assertThat(socket.output()).contains(expected); + } }