Skip to content

Commit

Permalink
refactor: header 전역 변수 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
eunbii0213 committed Sep 12, 2023
1 parent 0aa0036 commit a89311c
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 18 deletions.
10 changes: 0 additions & 10 deletions tomcat/src/main/java/org/apache/coyote/http11/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.apache.coyote.http11.enums;

import java.util.Arrays;

public enum ContentType {

JS(".js","Application/javascript;"),
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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", "[email protected]");
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);
}
}

0 comments on commit a89311c

Please sign in to comment.