Skip to content

Commit

Permalink
#95 - I'm a teapot (#221)
Browse files Browse the repository at this point in the history
* Add /coffee endpoint with 418 I'm a Teapot response

- Implement Teapot class for handling /coffee requests.
- Update HttpParser for path extraction.
- Modify ClientHandler for /coffee response.
- Add TeapotTest for endpoint testing.

* Add /coffee endpoint with 418 I'm a Teapot response

- Implement Teapot class for handling /coffee requests.
- Update HttpParser for path extraction.
- Modify ClientHandler for /coffee response.
- Add TeapotTest for endpoint testing.

* Add /coffee endpoint with 418 I'm a Teapot response

- Implement Teapot class for handling /coffee requests.
- Update HttpParser for path extraction.
- Modify ClientHandler for /coffee response.
- Add TeapotTest for endpoint testing.

* Add /coffee endpoint with 418 I'm a Teapot response

- Implement Teapot class for handling /coffee requests.
- Update HttpParser for path extraction.
- Modify ClientHandler for /coffee response.
- Add TeapotTest for endpoint testing.

---------

Co-authored-by: Martin Blomberg <martin.blomberg@outlook.com>
  • Loading branch information
Danback and kappsegla committed Feb 14, 2024
1 parent 292ece9 commit 727e51a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 9 deletions.
24 changes: 17 additions & 7 deletions src/main/java/org/fungover/storm/client/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.fungover.storm.filehandler.re.FileNotFoundException;
import org.fungover.storm.filehandler.re.FileRequestHandler;
import org.fungover.storm.filehandler.re.ResponseCode;
import org.fungover.storm.filehandler.re.Teapot;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -26,18 +27,26 @@ public ClientHandler(Socket socket, FileRequestHandler fileRequestHandler) {

@Override
public void run() {
OutputStream out;
BufferedReader in;
OutputStream out;
BufferedReader in;

try {
out = clientSocket.getOutputStream();
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

String input = in.readLine();
byte[][] response = getResponse(input);
byte[][] response;

if (Teapot.isCoffeeRequest(HttpParser.getPath(input))) {
response = Teapot.write418Response();
} else {
response = getResponse(input);
}

out.write(response[0]);
out.write(response[1]);
if (response.length > 1 && response[1] != null) {
out.write(response[1]);
}

in.close();
out.close();
Expand All @@ -54,6 +63,7 @@ private byte[][] getResponse(String input) throws IOException {
FileInfo fileInfo;
byte[][] response;
try {
LOGGER.info("INPUT: " + input);
fileInfo = fileRequestHandler.handleRequest(input);
response = fileRequestHandler.writeResponse(fileInfo);
} catch (FileNotFoundException e) {
Expand All @@ -64,13 +74,13 @@ private byte[][] getResponse(String input) throws IOException {
}

private byte[][] getFileNotFoundResponse(FileNotFoundException e) throws IOException {
FileInfo fileInfo;
FileInfo fileInfo = fileRequestHandler.handleError(e.getParsedRequest(), e.getError404FileName(), e.getResponseCode());
byte[][] response;
fileInfo = fileRequestHandler.handleError(e.getParsedRequest(), e.getError404FileName(), e.getResponseCode());
if (Files.exists(fileInfo.getPath())) {
response = fileRequestHandler.writeResponse(fileInfo, e.getResponseCode());
} else
} else {
response = fileRequestHandler.writeResponse(e.getResponseCode());
}
return response;
}
}
20 changes: 18 additions & 2 deletions src/main/java/org/fungover/storm/client/HttpParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class HttpParser {
private HttpParser() {
}


public static Map<String, String> getRequestHeaders(String headers) {
List<String> lines = headers.lines().toList();
String[] firstLine = lines.get(0).split(" ");
Expand All @@ -20,6 +21,18 @@ public static Map<String, String> getRequestHeaders(String headers) {
return Map.of();
}


public static String getPath(String requestLine) {
String[] properties = requestLine.split(" ");
if (validRequest(properties)) {
return properties[1];
} else {
return "";
}
}



private static Map<String, String> requestHeaders(List<String> lines, String[] firstLine) {
Map<String, String> requestHeaders = new HashMap<>(parseFirstLine(firstLine));
lines.stream().skip(1).map(HttpParser::parseHeader).forEach(header -> requestHeaders.put(header[0], header[1]));
Expand All @@ -40,7 +53,10 @@ private static boolean validRequest(String[] properties) {

private static String[] parseHeader(String header) {
String[] properties = header.split(":");
return new String[]{properties[0].trim(), properties[1].trim()};
if (properties.length >= 2) {
return new String[]{properties[0].trim(), properties[1].trim()};
} else {
return new String[]{properties[0].trim(), ""};
}
}

}
18 changes: 18 additions & 0 deletions src/main/java/org/fungover/storm/filehandler/re/Teapot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.fungover.storm.filehandler.re;

public class Teapot {


public static boolean isCoffeeRequest(String requestPath) {
return "/coffee".equals(requestPath);
}


public static byte[][] write418Response() {
String response = "HTTP/1.1 418 I'm a Teapot\r\n" +
"Content-Type: text/plain\r\n" +
"Connection: close\r\n\r\n" +
"418 I'm a teapot.";
return new byte[][]{response.getBytes()};
}
}
50 changes: 50 additions & 0 deletions src/test/java/org/fungover/storm/filehandler/re/TeapotTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.fungover.storm.filehandler.re;

import org.fungover.storm.client.HttpParser;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Map;

public class TeapotTest {

@Test
public void testIsCoffeeRequest() {
assertTrue(Teapot.isCoffeeRequest("/coffee"));
assertFalse(Teapot.isCoffeeRequest("/tea"));
}

@Test
public void testIsCoffeeRequestCaseSensitivity() {
assertFalse(Teapot.isCoffeeRequest("/Coffee"), "Path matching should be case-sensitive.");
}

@Test
public void testWrite418Response() {
byte[][] response = Teapot.write418Response();
String responseString = new String(response[0]);

assertTrue(responseString.startsWith("HTTP/1.1 418 I'm a Teapot"), "Response should start with 418 status code.");
assertTrue(responseString.contains("418 I'm a teapot."), "Response should contain the teapot message.");
}

@Test
public void testResponseContentType() {
byte[][] response = Teapot.write418Response();
String responseHeaders = new String(response[0]);
assertTrue(responseHeaders.contains("Content-Type: text/plain"), "Response should specify the correct content type.");
}

@Test
public void testHttpRequestParsingForCoffee() {
String httpRequest = "GET /coffee HTTP/1.1\r\nHost: localhost\r\n\r\n";
Map<String, String> parsedHeaders = HttpParser.getRequestHeaders(httpRequest);
assertTrue(Teapot.isCoffeeRequest(parsedHeaders.get("path")), "The path should be correctly identified from an HTTP request.");
}

@Test
public void testMalformedHttpRequest() {
String malformedRequest = "GET /coffee";
Map<String, String> parsedHeaders = HttpParser.getRequestHeaders(malformedRequest);
assertTrue(parsedHeaders.isEmpty(), "Malformed requests should not produce valid headers.");
}
}

0 comments on commit 727e51a

Please sign in to comment.