Skip to content

Commit

Permalink
Enabled request bodies for DELETEs
Browse files Browse the repository at this point in the history
Fixes #1972
  • Loading branch information
jhy committed Sep 9, 2023
1 parent 4e7ea39 commit 23573ef
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<https://github.com/jhy/jsoup/issues/1987>

* Bugfix: in Jsoup.connect(), a DELETE method request did not support a request body.
<https://github.com/jhy/jsoup/issues/1972>

* 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.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jsoup/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/jsoup/integration/ConnectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/org/jsoup/integration/servlets/BaseServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions src/test/java/org/jsoup/integration/servlets/EchoServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 23573ef

Please sign in to comment.