getHeadersMap() {
+ return headersMap;
+ }
+
+ public BufferedReader getHeaderReader() {
+ return reader;
+ }
+}
diff --git a/src/test/java/org/example/http/HttpResponseBuilderTest.java b/src/test/java/org/example/http/HttpResponseBuilderTest.java
new file mode 100644
index 00000000..8b80a7f8
--- /dev/null
+++ b/src/test/java/org/example/http/HttpResponseBuilderTest.java
@@ -0,0 +1,45 @@
+package org.example.http;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+
+ class HttpResponseBuilderTest {
+
+ /**
+ * Verifies that build produces a valid HTTP response string!
+ * Status line is present
+ * Content-Length header is generated
+ * The response body is included
+ */
+
+ @Test
+ void build_returnsValidHttpResponse() {
+
+ HttpResponseBuilder builder = new HttpResponseBuilder();
+
+ builder.setBody("Hello");
+
+ String result = builder.build();
+
+ assertThat(result).contains("HTTP/1.1 200 OK");
+ assertThat(result).contains("Content-Length: 5");
+ assertThat(result).contains("\r\n\r\n");
+ assertThat(result).contains("Hello");
+
+ }
+
+ // Verifies that Content-Length is calculated using UTF-8 byte length!
+ //
+ @Test
+ void build_handlesUtf8ContentLength() {
+ HttpResponseBuilder builder = new HttpResponseBuilder();
+
+ builder.setBody("å");
+
+ String result = builder.build();
+
+ assertThat(result).contains("Content-Length: 2");
+
+ }
+}
diff --git a/src/test/java/org/example/httpparser/HttpParseRequestLineTest.java b/src/test/java/org/example/httpparser/HttpParseRequestLineTest.java
new file mode 100644
index 00000000..8ff289f3
--- /dev/null
+++ b/src/test/java/org/example/httpparser/HttpParseRequestLineTest.java
@@ -0,0 +1,73 @@
+package org.example.httpparser;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.*;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
+import static org.junit.jupiter.api.Assertions.*;
+
+class HttpParseRequestLineTest {
+ private HttpParser httpParseRequestLine;
+
+ @BeforeEach
+ void setUp() {
+ httpParseRequestLine = new HttpParser();
+ }
+
+ @Test
+ void testParserWithTestRequestLine() throws IOException {
+ String testString = "GET / HTTP/1.1";
+
+ InputStream in = new ByteArrayInputStream(testString.getBytes());
+ httpParseRequestLine.setReader(in);
+ httpParseRequestLine.parseRequest();
+
+ assertThat(httpParseRequestLine.getMethod()).isEqualTo("GET");
+ assertThat(httpParseRequestLine.getUri()).isEqualTo("/");
+ assertThat(httpParseRequestLine.getVersion()).isEqualTo("HTTP/1.1");
+ }
+
+ @Test
+ void testParserThrowErrorWhenNull(){
+ assertThatThrownBy(() -> httpParseRequestLine.setReader(null)).isInstanceOf(NullPointerException.class);
+ }
+
+
+ @Test
+ void testParserThrowErrorWhenEmpty(){
+ InputStream in = new ByteArrayInputStream("".getBytes());
+ httpParseRequestLine.setReader(in);
+ Exception exception = assertThrows(
+ IOException.class, () -> httpParseRequestLine.parseRequest()
+ );
+
+ assertThat(exception.getMessage()).isEqualTo("HTTP Request Line is Null or Empty");
+ }
+
+ @Test
+ void testParserThrowErrorWhenMethodIsInvalid(){
+ String testString = "get / HTTP/1.1";
+ InputStream in = new ByteArrayInputStream(testString.getBytes());
+ httpParseRequestLine.setReader(in);
+ Exception exception = assertThrows(
+ IOException.class, () -> httpParseRequestLine.parseRequest()
+ );
+ assertThat(exception.getMessage()).isEqualTo("Invalid HTTP method");
+ }
+
+ @Test
+ void testParserThrowErrorWhenArrayLengthLessOrEqualsTwo(){
+ String testString = "GET / ";
+ InputStream in = new ByteArrayInputStream(testString.getBytes());
+ httpParseRequestLine.setReader(in);
+ Exception exception = assertThrows(
+ IOException.class, () -> httpParseRequestLine.parseRequest()
+ );
+
+ assertThat(exception.getMessage()).isEqualTo("HTTP Request Line is not long enough");
+ }
+
+}
diff --git a/src/test/java/org/example/httpparser/HttpParserTest.java b/src/test/java/org/example/httpparser/HttpParserTest.java
new file mode 100644
index 00000000..a09b7e22
--- /dev/null
+++ b/src/test/java/org/example/httpparser/HttpParserTest.java
@@ -0,0 +1,53 @@
+package org.example.httpparser;
+
+import org.junit.jupiter.api.Test;
+
+import java.io.*;
+import java.nio.charset.StandardCharsets;
+
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.*;
+
+class HttpParserTest {
+ private HttpParser httpParser = new HttpParser();
+
+ @Test
+ void TestHttpParserForHeaders() throws IOException {
+ String testInput = "GET /index.html HTTP/1.1\r\nHost: localhost\r\nContent-Type: text/plain\r\nUser-Agent: JUnit5\r\n\r\n";
+ InputStream in = new ByteArrayInputStream(testInput.getBytes(StandardCharsets.UTF_8));
+
+ httpParser.setReader(in);
+ httpParser.parseHttp();
+
+ assertNotNull(httpParser.getHeadersMap());
+ assertThat(httpParser.getHeadersMap().size()).isEqualTo(3);
+ assertThat(httpParser.getHeadersMap().get("Host")).contains("localhost");
+ assertThat(httpParser.getHeadersMap().get("Content-Type")).contains("text/plain");
+ assertThat(httpParser.getHeadersMap().get("User-Agent")).contains("JUnit5");
+ }
+
+ @Test
+ void testParseHttp_EmptyInput() throws IOException {
+ InputStream in = new ByteArrayInputStream("".getBytes());
+ httpParser.setReader(in);
+ httpParser.parseHttp();
+
+ assertTrue(httpParser.getHeadersMap().isEmpty());
+ }
+
+ @Test
+ void testParseHttp_InvalidHeaderLine() throws IOException {
+ String rawInput = "Host: localhost\r\n InvalidLineWithoutColon\r\n Accept: */*\r\n\r\n";
+
+ InputStream in = new ByteArrayInputStream(rawInput.getBytes(StandardCharsets.UTF_8));
+ httpParser.setReader(in);
+ httpParser.parseHttp();
+
+ assertEquals(2, httpParser.getHeadersMap().size());
+ assertEquals("localhost", httpParser.getHeadersMap().get("Host"));
+ assertEquals("*/*", httpParser.getHeadersMap().get("Accept"));
+ }
+
+
+}
diff --git a/www/index.html b/www/index.html
new file mode 100644
index 00000000..8293c074
--- /dev/null
+++ b/www/index.html
@@ -0,0 +1,12 @@
+
+
+
+
+ Welcome
+
+
+Website works!
+Greetings from StaticFileHandler.
+
+
+