From 23573ef31ce1320fa2bf57fb6aaf5e51006d69e7 Mon Sep 17 00:00:00 2001 From: Jonathan Hedley Date: Sat, 9 Sep 2023 11:08:18 +1000 Subject: [PATCH] Enabled request bodies for DELETEs Fixes #1972 --- CHANGES | 3 +++ src/main/java/org/jsoup/Connection.java | 2 +- .../org/jsoup/integration/ConnectTest.java | 26 +++++++++++++++++++ .../integration/servlets/BaseServlet.java | 5 ++++ .../integration/servlets/EchoServlet.java | 5 ++++ 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index e24b64adff..f8c1966db9 100644 --- a/CHANGES +++ b/CHANGES @@ -44,6 +44,9 @@ Release 1.16.2 [PENDING] * Bugfix: when tracking HTML source positions, the closing tags for H1...H6 elements were not tracked correctly. + * Bugfix: in Jsoup.connect(), a DELETE method request did not support a request body. + + * Change: removed previously deprecated methods Document#normalise, Element#forEach(org.jsoup.helper.Consumer<>), Node#forEach(org.jsoup.helper.Consumer<>), and the org.jsoup.helper.Consumer interface; the latter being a previously required compatibility shim prior to Android's de-sugaring support. diff --git a/src/main/java/org/jsoup/Connection.java b/src/main/java/org/jsoup/Connection.java index ef64faa431..c6afd7f469 100644 --- a/src/main/java/org/jsoup/Connection.java +++ b/src/main/java/org/jsoup/Connection.java @@ -43,7 +43,7 @@ public interface Connection { * GET and POST http methods. */ enum Method { - GET(false), POST(true), PUT(true), DELETE(false), PATCH(true), HEAD(false), OPTIONS(false), TRACE(false); + GET(false), POST(true), PUT(true), DELETE(true), PATCH(true), HEAD(false), OPTIONS(false), TRACE(false); private final boolean hasBody; diff --git a/src/test/java/org/jsoup/integration/ConnectTest.java b/src/test/java/org/jsoup/integration/ConnectTest.java index 07f9d2ebc2..f02840a0cb 100644 --- a/src/test/java/org/jsoup/integration/ConnectTest.java +++ b/src/test/java/org/jsoup/integration/ConnectTest.java @@ -3,6 +3,7 @@ import org.jsoup.Connection; import org.jsoup.HttpStatusException; import org.jsoup.Jsoup; +import org.jsoup.Connection.Method; import org.jsoup.helper.DataUtil; import org.jsoup.helper.W3CDom; import org.jsoup.integration.servlets.*; @@ -254,6 +255,31 @@ public void doesPut() throws IOException { assertEquals("auth=token", ihVal("Cookie", doc)); } + @Test + public void doesDeleteWithBody() throws IOException { + // https://github.com/jhy/jsoup/issues/1972 + String body = "some body"; + Connection.Response res = Jsoup.connect(echoUrl) + .requestBody(body) + .method(Method.DELETE) + .execute(); + + Document doc = res.parse(); + assertEquals("DELETE", ihVal("Method", doc)); + assertEquals(body, ihVal("Post Data", doc)); + } + + @Test + public void doesDeleteWithoutBody() throws IOException { + Connection.Response res = Jsoup.connect(echoUrl) + .method(Method.DELETE) + .execute(); + + Document doc = res.parse(); + assertEquals("DELETE", ihVal("Method", doc)); + assertEquals(null, ihVal("Post Data", doc)); + } + /** * Tests upload of content to a remote service. */ diff --git a/src/test/java/org/jsoup/integration/servlets/BaseServlet.java b/src/test/java/org/jsoup/integration/servlets/BaseServlet.java index b1bf5cca76..46d8e477e0 100644 --- a/src/test/java/org/jsoup/integration/servlets/BaseServlet.java +++ b/src/test/java/org/jsoup/integration/servlets/BaseServlet.java @@ -24,4 +24,9 @@ protected void doPost(HttpServletRequest req, HttpServletResponse res) throws Se protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { super.doPut(req, res); } + + @Override + protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { + super.doPut(req, res); + } } diff --git a/src/test/java/org/jsoup/integration/servlets/EchoServlet.java b/src/test/java/org/jsoup/integration/servlets/EchoServlet.java index f5bf2ce3ed..c3a9c44a3f 100644 --- a/src/test/java/org/jsoup/integration/servlets/EchoServlet.java +++ b/src/test/java/org/jsoup/integration/servlets/EchoServlet.java @@ -40,6 +40,11 @@ protected void doPut(HttpServletRequest req, HttpServletResponse res) throws Ser doIt(req, res); } + @Override + protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { + doIt(req, res); + } + private void doIt(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { int intCode = DefaultCode; String code = req.getHeader(CodeParam);