Skip to content

Commit

Permalink
fix : 로그인 페이지 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
waterricecake committed Sep 4, 2023
1 parent 2431d4c commit 3fe89dc
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 9 deletions.
23 changes: 23 additions & 0 deletions tomcat/src/main/java/nextstep/jwp/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package nextstep.jwp.controller;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import nextstep.jwp.db.InMemorySession;
Expand All @@ -10,6 +13,7 @@
import org.apache.coyote.http11.request.Request;
import org.apache.coyote.http11.response.HttpStatus;
import org.apache.coyote.http11.response.Response;
import org.apache.coyote.http11.servlet.Servlet;

public class LoginController {

Expand All @@ -27,7 +31,10 @@ public static Response login(Request request){
}
return Response.builder()
.status(HttpStatus.FOUND)
.contentType("html")
.cookie(cookie)
.location("index.html")
.responseBody(getFile("index.html"))
.build();
}

Expand All @@ -40,6 +47,22 @@ public static Response signUp(Request request){
InMemoryUserRepository.save(user);
return Response.builder()
.status(HttpStatus.FOUND)
.contentType("html")
.location("index.html")
.responseBody(getFile("index.html"))
.build();
}


private static String getFile(String fileName){
try {
final var fileUrl = Servlet.class.getClassLoader().getResource("static/" + fileName);
final var fileBytes = Files.readAllBytes(new File(fileUrl.getFile()).toPath());
return new String(fileBytes);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (NullPointerException e){
return "";
}
}
}
31 changes: 30 additions & 1 deletion tomcat/src/main/java/nextstep/jwp/controller/ViewController.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
package nextstep.jwp.controller;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.apache.coyote.http11.request.Request;
import org.apache.coyote.http11.response.HttpStatus;
import org.apache.coyote.http11.response.Response;
import org.apache.coyote.http11.servlet.Servlet;

public class ViewController {
public static Response getIndex(Request request){
public static Response getLogin(Request request){
return Response.builder()
.status(HttpStatus.OK)
.contentType("html")
.responseBody(getFile("login.html"))
.build();
}

public static Response getRegister(Request request){
return Response.builder()
.status(HttpStatus.OK)
.contentType("html")
.responseBody(getFile("register.html"))
.build();
}

public static Response getVoid(Request request){
return Response.builder()
.status(HttpStatus.OK)
.responseBody("Hello world!")
.contentType("html")
.build();
}


private static String getFile(String fileName){
try {
final var fileUrl = Servlet.class.getClassLoader().getResource("static/" + fileName);
final var fileBytes = Files.readAllBytes(new File(fileUrl.getFile()).toPath());
return new String(fileBytes);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (NullPointerException e){
return "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class ApiHandler extends Handler{
private static final List<API> apiList =new ArrayList<>();

static{
addApi("/login", ViewController::getIndex,"login.html",false,false);
addApi("/", ViewController::getVoid,"Hello world!",false,false);
addApi("/login", ViewController::getLogin,"login.html",false,false);
addApi("/register", ViewController::getRegister,"register.html",false,false);
addApi("/login", LoginController::login,"index.html",false,true);
addApi("/register", LoginController::signUp,"index.html",false, true);
Expand All @@ -30,7 +31,7 @@ String getResponse(Request request) {
boolean isHandle = api.isHandle(request);
if(isHandle){
Response response = api.controller.apply(request);
return response.redirect(getFile(api.file),api.file).getResponse();
return response.getResponse();
}
}
return next.getResponse(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ public static Response badResponse(HttpStatus httpStatus){
}

public String getResponse(){

return String.join("\r\n",
"HTTP/1.1 " + status + " ",
"Content-Type: text/" + contentType + ";charset=utf-8 ",
"Content-Length: " + responseBody.getBytes().length + " ",
makeLocation(),
makeCookie(),
makeLocation() + makeCookie(),
responseBody);
}

Expand All @@ -57,7 +55,7 @@ private String makeLocation(){
if(location==null){
return "";
}
return "location : " + location;
return "location : " + location + "\r\n";
}
private String makeCookie(){
if(cookie == null){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package nextstep.org.apache.coyote.http11;

import com.sun.jdi.connect.Connector.Argument;
import java.util.stream.Stream;
import support.StubSocket;
import org.apache.coyote.http11.Http11Processor;
import org.junit.jupiter.api.Test;
Expand All @@ -12,7 +14,6 @@
import static org.assertj.core.api.Assertions.assertThat;

class Http11ProcessorTest {

@Test
void process() {
// given
Expand Down Expand Up @@ -51,9 +52,66 @@ void index() throws IOException {

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

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

@Test
void login() throws IOException {
// given
final String httpRequest= String.join("\r\n",
"GET /login.html 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");
final String responseBody = new String(Files.readAllBytes(new File(resource.getFile()).toPath()));
var expected = "HTTP/1.1 200 OK \r\n" +
"Content-Type: text/html;charset=utf-8 \r\n" +
"Content-Length: " + responseBody.getBytes().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
final String httpRequest= String.join("\r\n",
"GET /register.html 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");
final String responseBody = new String(Files.readAllBytes(new File(resource.getFile()).toPath()));
var expected = "HTTP/1.1 200 OK \r\n" +
"Content-Type: text/html;charset=utf-8 \r\n" +
"Content-Length: 5564 \r\n" +
"Content-Length: " + responseBody.getBytes().length + " \r\n" +
"\r\n"+
new String(Files.readAllBytes(new File(resource.getFile()).toPath()));

Expand Down

0 comments on commit 3fe89dc

Please sign in to comment.