Skip to content

Commit 7680622

Browse files
committed
Add support for 405 responses to invalid methods
1 parent 02b3cb5 commit 7680622

File tree

6 files changed

+61
-23
lines changed

6 files changed

+61
-23
lines changed

src/main/java/httpserver/Method.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package httpserver;
22

3-
import httpserver.responder.GetResponder;
4-
import httpserver.responder.PostResponder;
5-
import httpserver.responder.PutResponder;
6-
import httpserver.responder.Responder;
3+
import httpserver.responder.*;
74
import httpserver.response.Response;
85

96
import java.nio.file.Path;
@@ -14,7 +11,8 @@ public enum Method {
1411
POST(new PostResponder()),
1512
PUT(new PutResponder()),
1613
DELETE(null),
17-
OPTIONS(null);
14+
OPTIONS(null),
15+
INVALID(new InvalidMethodResponder());
1816

1917
private final Responder responder;
2018

src/main/java/httpserver/Request.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
import java.util.HashMap;
44

55
public class Request {
6-
private final Method method;
6+
private Method method;
77
private final String path;
88
private final HashMap<String, String> headers;
99

1010
public Request(String method, String path, HashMap<String, String> headers) {
11-
this.method = Method.valueOf(method);
11+
try {
12+
this.method = Method.valueOf(method);
13+
} catch (IllegalArgumentException e) {
14+
this.method = Method.INVALID;
15+
}
1216
this.path = path;
1317
this.headers = headers;
1418
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package httpserver.responder;
2+
3+
import httpserver.Request;
4+
import httpserver.response.MethodNotAllowedResponse;
5+
import httpserver.response.Response;
6+
7+
import java.nio.file.Path;
8+
9+
public class InvalidMethodResponder implements Responder {
10+
@Override
11+
public Response respond(Path root, Request request) {
12+
return new MethodNotAllowedResponse();
13+
}
14+
}

src/test/java/httpserver/RequestTest.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,20 @@
99

1010
public class RequestTest {
1111

12-
private final Request request;
13-
14-
public RequestTest() {
12+
@Test
13+
public void hasCorrectMethodPathAndHeaders() throws Exception {
1514
HashMap<String, String> headers = new HashMap<>();
1615
headers.put("test-header", "test-value");
1716

18-
this.request = new Request("GET", "example.txt", headers);
19-
}
20-
21-
@Test
22-
public void getMethodReturnsGet() throws Exception {
17+
Request request = new Request("GET", "/example.txt", headers);
2318
assertEquals(Method.GET, request.getMethod());
19+
assertEquals("/example.txt", request.getPath());
20+
assertEquals("test-value", headers.get("test-header"));
2421
}
2522

2623
@Test
27-
public void getPathReturnsPath() throws Exception {
28-
assertEquals("example.txt", request.getPath());
29-
}
30-
31-
@Test
32-
public void getHeadersReturnsHeaders() throws Exception {
33-
HashMap<String, String> headers = request.getHeaders();
34-
assertEquals("test-value", headers.get("test-header"));
24+
public void getMethodReturnsInvalidWhenBadMethod() throws Exception {
25+
Request request = new Request("ABCDEF", "/example.txt", new HashMap<>());
26+
assertEquals(Method.INVALID, request.getMethod());
3527
}
3628
}

src/test/java/httpserver/SocketHandlerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,18 @@ public void returns405ForPUT() throws Exception {
8282
String expected = "HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n";
8383
assertEquals(expected, outputStream.toString());
8484
}
85+
86+
@Test
87+
public void returns405ForUnsupportedMethod() throws Exception {
88+
Path root = tempDir();
89+
byte[] request = ("XYZABC /example HTTP/1.1\r\nHost: 127.0.0.1:5000\r\n\r\n").getBytes();
90+
InputStream inputStream = new ByteArrayInputStream(request);
91+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
92+
SocketHandler socketHandler = new SocketHandler(root);
93+
94+
socketHandler.process(inputStream, outputStream);
95+
96+
String expected = "HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n";
97+
assertEquals(expected, outputStream.toString());
98+
}
8599
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package httpserver.responder;
2+
3+
import httpserver.response.Response;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class InvalidMethodResponderTest {
9+
@Test
10+
public void returns405Response() throws Exception {
11+
InvalidMethodResponder invalidMethodResponder = new InvalidMethodResponder();
12+
13+
Response response = invalidMethodResponder.respond(null, null);
14+
assertEquals(405, response.getStatusCode());
15+
}
16+
}

0 commit comments

Comments
 (0)