Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ public class ConfigRoutes {
this.initialize();
}

void initialize() {
private void initialize() {
this.initializeGetRoutes();
this.initializePostRoutes();
//custom routes go here:
}

void initializeGetRoutes() {
private void initializeGetRoutes() {
ControllerForm controllerForm = new ControllerForm();
ControllerFile controllerFile = new ControllerFile();
router.addRoute("GET","/resources/form/index.html", controllerFile);
router.addRoute("GET", "/form", controllerForm);
}

void initializePostRoutes() {
private void initializePostRoutes() {
ControllerPost controllerPost = new ControllerPost();
router.addRoute("POST", "/resources/form", controllerPost);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ResponseParameters getResponse(RequestParameters requestParameters) throw
.build();
}

String getDirectoryListing(String filePath, String relativePath) {
protected String getDirectoryListing(String filePath, String relativePath) {
ArrayList<String> directoryContents = filesList(filePath);
ArrayList<String> formattedDirectory = formatDirectoryHtml(directoryContents, relativePath);
StringBuilder directoryListing = new StringBuilder();
Expand All @@ -29,7 +29,7 @@ String getDirectoryListing(String filePath, String relativePath) {
return directoryListing.toString();
}

ArrayList<String> filesList(String filePath) {
protected ArrayList<String> filesList(String filePath) {
File fullFilePath = new File(filePath);
try {
return new ArrayList<>(Arrays.asList(fullFilePath.list()));
Expand All @@ -38,7 +38,7 @@ ArrayList<String> filesList(String filePath) {
}
}

ArrayList<String> formatDirectoryHtml(ArrayList<String> directoryList, String relativePath) {
protected ArrayList<String> formatDirectoryHtml(ArrayList<String> directoryList, String relativePath) {

ArrayList<String> directoryResponseMessage = new ArrayList<>();
directoryResponseMessage.add("<!DOCTYPE html>\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RequestHandler implements Runnable {

RequestHandler(String directoryPath,
Socket socket,
Logger logger,
LoggerInterface logger,
RouterInterface router,
SendInterface sendInterface,
ReadInterface readInterface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ private RequestParameters(RequestBuilder builder) {
this.params = builder.params;
}

String getDirectoryPath() { return directoryPath; }
public String getDirectoryPath() { return directoryPath; }

String getHttpVerb() { return httpVerb; }
public String getHttpVerb() { return httpVerb; }

String getRequestPath() { return requestPath; }
public String getRequestPath() { return requestPath; }

String getHost() { return host; }
public String getHost() { return host; }

String getUserAgent() { return userAgent; }
public String getUserAgent() { return userAgent; }

String[] getAccept() { return accept; }
public String[] getAccept() { return accept; }

String getBodyContent() { return bodyContent; }
public String getBodyContent() { return bodyContent; }

String getParams() { return params; }
public String getParams() { return params; }

public static class RequestBuilder {
private final String directoryPath;
Expand Down Expand Up @@ -78,7 +78,7 @@ public RequestBuilder setHost(ArrayList<String> httpMessage) {
String host = null;
for(String line: httpMessage) {
String headerField = line.split(" ")[0];
if(headerField.equals("Host:")) {
if("Host:".equals(headerField)) {
host = line.split(" ")[1].trim();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/clojars/kyleannen/javaserver/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Set<String> getRoutePaths() {
}
}

Boolean routeExists(String route) { return this.routes.keySet().contains(route); }
protected Boolean routeExists(String route) { return this.routes.keySet().contains(route); }

public ResponseParameters getResponse(RequestParameters requestParameters) throws IOException {
String path = requestParameters.getRequestPath();
Expand Down
43 changes: 16 additions & 27 deletions src/main/java/org/clojars/kyleannen/javaserver/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.concurrent.ExecutorService;

public class Server implements Runnable{
private Logger logger;
private LoggerInterface logger;
private int portNumber = 3300;
private Boolean serverRunning = true;
private String directoryPath = System.getProperty("user.dir");
Expand All @@ -21,22 +21,18 @@ public class Server implements Runnable{
SendInterface sendInterface,
Router router,
LoggerInterface logger) {
this.logger = new Logger();
portNumber = this.setPortNumber(portNumber, args);
directoryPath = this.setDirectoryPath(directoryPath, args, logger);
//logger.log("Serving directory: " + directoryPath);
//this.announceServer(portNumber,this.logger);

this.requestExecutor = requestExecutor;
this.logger = logger;
this.router = router;
this.readInterface = readInterface;
this.sendInterface = sendInterface;
this.requestExecutor = requestExecutor;
portNumber = this.setPortNumber(portNumber, args);
directoryPath = this.setDirectoryPath(directoryPath, args);
new ConfigRoutes(this.router);
}

@Override
public void run() {
this.announceServer(portNumber, logger);
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(portNumber);
Expand All @@ -54,19 +50,24 @@ public void run() {
}
}

private void announceServer(int portNumber, Logger logger) {
String outputMessage = "com.github.kyleannen.javaserver.Server started at: http://localhost:" +
Integer.toString(portNumber);
//logger.log(outputMessage);
void stop() {
serverRunning = false;
}

String getDirectoryPath() {
return this.directoryPath;
}

private String setDirectoryPath(String directPath, String[] args, LoggerInterface logger) {
int getPortNumber() {
return this.portNumber;
}

private String setDirectoryPath(String directPath, String[] args) {
for(int i = 0; i < args.length; i++) {
if(args[i].equals("-d") && new File(args[i + 1]).isDirectory()) {
return args[i + 1];
}
}
// logger.log("No valid directory path provided.");
return directPath;
}

Expand All @@ -78,16 +79,4 @@ private int setPortNumber(int portNum, String[] args) {
}
return portNum;
}

int getPortNumber() {
return this.portNumber;
}

String getDirectoryPath() {
return this.directoryPath;
}

void stop() {
serverRunning = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.util.concurrent.ExecutorService;

public class ShutdownHook extends Thread{
Server httpServer;
ExecutorService requestExecutor;
private Server httpServer;
private ExecutorService requestExecutor;

ShutdownHook(Server httpServer, ExecutorService requestExecutor) {
this.httpServer = httpServer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
import java.util.ArrayList;

public class ConfigFileDownloadsTest {
@Test
void configFileDownloadsCanBeInitialized() {
ConfigFileDownloads configFileDownloads = new ConfigFileDownloads();
}

@Test
void configFileIsDownloadableReturnsCorrectBoolean() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class ConfigureServerTest {

@Test
void configureServerReturnsAServerWithPortConfiguredWhenPortPassed() {
public void configureServerReturnsAServerWithPortConfiguredWhenPortPassed() {
String testPort = "3333";
String[] args = new String[]{"-p", testPort};
Router testRouter = new Router();
Expand All @@ -16,7 +16,7 @@ void configureServerReturnsAServerWithPortConfiguredWhenPortPassed() {
}

@Test
void configureServerReturnsAServerWithDirectoryConfiguredWhenDirectoryPassed() {
public void configureServerReturnsAServerWithDirectoryConfiguredWhenDirectoryPassed() {
String testDirectory = System.getProperty("user.dir") + "/resources";
String[] args = new String[]{"-d", testDirectory};
Router router = new Router();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ class ControllerPostTest {
.build();
}


@Test
void postClassExists(){
ControllerPost post = new ControllerPost();
}

@Test
void postWillReturnAResponse() throws IOException {
ControllerPost post = new ControllerPost();
Expand All @@ -55,12 +49,4 @@ void parseFormDataParsesFormData() throws UnsupportedEncodingException {
assertEquals("annen", parsedData.get("last_name"));
assertEquals("kannen@gmail.com", parsedData.get("email"));
}

@Test
void saveFormDataSavesFormData() throws IOException {
String filePath = System.getProperty("user.dir") + "/resources/form/form-result.html";
ControllerPost post = new ControllerPost();
HashMap<String, String> parsedData = post.parseFormData(testRequestParams.getBodyContent());
assertEquals(true, new File(filePath).exists());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@

public class LoggerTest {

@Test
void loggerCanBeInitialized() {
try {
Logger logger = new Logger();
} catch (Exception e) {
assert(false);
}
}

@Test
void loggerLogCanBeCalled() {
Logger logger = new Logger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

class RequestHandlerTest extends TestDirectorySetup {
private Integer startPort1 = 2020;

LoggerInterface logger = new LoggerInterface() {
@Override
public void log(String string) {
//empty as a mock
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ void responseParameterContainsCorrectResponseStatus() {
ResponseParameters responseParameters = new ResponseParameters.ResponseBuilder(200)
.build();
String expectedStatus = "HTTP/1.1 200 OK\r\n";
String actaulStatus = responseParameters.getResponseStatus();
String actualStatus = responseParameters.getResponseStatus();
assertEquals(expectedStatus, actualStatus);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ public ResponseParameters getResponse(RequestParameters requestParameters) {
}
}


@Test
void RouterCanBeInstantiated() {
Router router = new Router();
}

@Test
void routerCanAddRouter() {
Router router = new Router();
Expand Down
10 changes: 0 additions & 10 deletions src/test/java/org/clojars/kyleannen/javaserver/RoutesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ public ResponseParameters getResponse(RequestParameters requestParameters) {
}
}

@Test
void routeCanBeInitialized() {
try {
Routes testRoutes = new Routes();
assert(true);
} catch (Exception e){
assert(false);
}
}

@Test
void routeCanBeAddedToRoutes() {
Routes testRoutes = new Routes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ public class SendResponseTest {
Server server;

class MockLogger implements LoggerInterface {

String string;
@Override
public void log(String string) {
this.string = string;
}
public String getLog() {
return this.string;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class TestDirectorySetup {

@BeforeAll
static void generateTestFileStructure() throws IOException {
ArrayList<Path> files = new ArrayList<>();
List<String> lines = Arrays.asList("test\r\n\r\ntest","test\n");
File dir = new File("./TestDirectory");
File dir2 = new File("./TestEmpty");
Expand Down