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
6 changes: 4 additions & 2 deletions src/main/java/spark/CustomErrorPages.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class CustomErrorPages {

private static final Logger LOG = LoggerFactory.getLogger(CustomErrorPages.class);
public static final String NOT_FOUND = "<html><body><h2>404 Not found</h2></body></html>";
public static final String METHOD_NOT_ALLOWED = "<html><body><h2>405 Method Not Allowed</h2></body></html>";
public static final String INTERNAL_ERROR = "<html><body><h2>500 Internal Server Error</h2></body></html>";

/**
Expand All @@ -45,7 +46,7 @@ public static boolean existsFor(int status) {
/**
* Gets the custom error page for a given status code. If the custom
* error page is a route, the output of its handle method is returned.
* If the custom error page is a String, it is returned as an Object.
* If the custom error page is a String, it is returned as an Object.
* @param status
* @param request
* @param response
Expand Down Expand Up @@ -80,7 +81,7 @@ public String getDefaultFor(int status){
String defaultPage = defaultPages.get(status);
return (defaultPage != null) ? defaultPage : "<html><body><h2>HTTP Status " + status + "</h2></body></html>";
}

/**
* Add a custom error page as a String
* @param status
Expand All @@ -106,6 +107,7 @@ static void add(int status, Route route) {

private CustomErrorPages() {
customPages = new HashMap<>();
customPages.put(405, METHOD_NOT_ALLOWED);
defaultPages = new HashMap<>();
defaultPages.put(404, NOT_FOUND);
defaultPages.put(500, INTERNAL_ERROR);
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/spark/http/matching/MatcherFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ public void doFilter(ServletRequest servletRequest,

try {
try {

BeforeFilters.execute(context);
Routes.execute(context);
AfterFilters.execute(context);
Expand Down Expand Up @@ -164,14 +163,17 @@ public void doFilter(ServletRequest servletRequest,
}

if (body.notSet()) {
int returnStatus;
if(httpMethodStr.equals("put") && response.status() == 200) returnStatus = HttpServletResponse.SC_METHOD_NOT_ALLOWED;
else returnStatus = HttpServletResponse.SC_NOT_FOUND;
LOG.info("The requested route [{}] has not been mapped in Spark for {}: [{}]",
uri, ACCEPT_TYPE_REQUEST_MIME_HEADER, acceptType);
httpResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
httpResponse.setStatus(returnStatus);

if (CustomErrorPages.existsFor(404)) {
if (CustomErrorPages.existsFor(returnStatus)) {
requestWrapper.setDelegate(RequestResponseFactory.create(httpRequest));
responseWrapper.setDelegate(RequestResponseFactory.create(httpResponse));
body.set(CustomErrorPages.getFor(404, requestWrapper, responseWrapper));
body.set(CustomErrorPages.getFor(returnStatus, requestWrapper, responseWrapper));
} else {
body.set(String.format(CustomErrorPages.NOT_FOUND));
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/spark/ResponseBodyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
public class ResponseBodyTest {

public static final String HELLO = "/hello";
public static final String METHOD_NOT_ALLOWED = "/405";
public static final String SPECIAL = "/special";
public static final String PORAKATIKAOKAO = "/porakatikaokao";
public static final String MAXIME = "/maxime";
Expand All @@ -54,6 +55,8 @@ public static void tearDown() {
public static void setup() throws IOException {
http = new SparkTestUtil(4567);

get(METHOD_NOT_ALLOWED, (q, a) -> HELLO_WORLD);

get(HELLO, (q, a) -> HELLO_WORLD);

after(HELLO, (q, a) -> {
Expand Down Expand Up @@ -105,6 +108,16 @@ public void testHELLO() {
}
}

@Test
public void testMethodNotAllowed() {
try {
SparkTestUtil.UrlResponse response = http.doMethod("PUT",METHOD_NOT_ALLOWED,"");
assertEquals(405, response.status);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

@Test
public void testSPECIAL() {
try {
Expand Down