From 86c5f79790fb280eb62ee47632b45561b8238b37 Mon Sep 17 00:00:00 2001
From: Bugjudger <56737316+Bugjudger@users.noreply.github.com>
Date: Thu, 20 May 2021 21:02:17 +0800
Subject: [PATCH] issue 1226
---
src/main/java/spark/CustomErrorPages.java | 6 ++++--
.../java/spark/http/matching/MatcherFilter.java | 10 ++++++----
src/test/java/spark/ResponseBodyTest.java | 13 +++++++++++++
3 files changed, 23 insertions(+), 6 deletions(-)
diff --git a/src/main/java/spark/CustomErrorPages.java b/src/main/java/spark/CustomErrorPages.java
index 1c639c2336..3c62d131d8 100644
--- a/src/main/java/spark/CustomErrorPages.java
+++ b/src/main/java/spark/CustomErrorPages.java
@@ -31,6 +31,7 @@ public class CustomErrorPages {
private static final Logger LOG = LoggerFactory.getLogger(CustomErrorPages.class);
public static final String NOT_FOUND = "
404 Not found
";
+ public static final String METHOD_NOT_ALLOWED = "405 Method Not Allowed
";
public static final String INTERNAL_ERROR = "500 Internal Server Error
";
/**
@@ -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
@@ -80,7 +81,7 @@ public String getDefaultFor(int status){
String defaultPage = defaultPages.get(status);
return (defaultPage != null) ? defaultPage : "HTTP Status " + status + "
";
}
-
+
/**
* Add a custom error page as a String
* @param status
@@ -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);
diff --git a/src/main/java/spark/http/matching/MatcherFilter.java b/src/main/java/spark/http/matching/MatcherFilter.java
index 3fa05bea0a..f87bbcb41e 100644
--- a/src/main/java/spark/http/matching/MatcherFilter.java
+++ b/src/main/java/spark/http/matching/MatcherFilter.java
@@ -129,7 +129,6 @@ public void doFilter(ServletRequest servletRequest,
try {
try {
-
BeforeFilters.execute(context);
Routes.execute(context);
AfterFilters.execute(context);
@@ -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));
}
diff --git a/src/test/java/spark/ResponseBodyTest.java b/src/test/java/spark/ResponseBodyTest.java
index f8cf36db0c..e7954e864b 100644
--- a/src/test/java/spark/ResponseBodyTest.java
+++ b/src/test/java/spark/ResponseBodyTest.java
@@ -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";
@@ -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) -> {
@@ -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 {