From b433eff4b8a33387bde81f116e251f40fba64284 Mon Sep 17 00:00:00 2001 From: tom-edge Date: Mon, 24 Jul 2023 10:08:06 +0100 Subject: [PATCH 1/2] allow sending of body on delete calls --- .../traverson/TraversonBuilder.java | 27 +++++++++++++++++++ .../traverson/TraversonBuilderTest.java | 24 +++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/traverson4j-core/src/main/java/uk/co/autotrader/traverson/TraversonBuilder.java b/traverson4j-core/src/main/java/uk/co/autotrader/traverson/TraversonBuilder.java index 1e4235e..03845c8 100644 --- a/traverson4j-core/src/main/java/uk/co/autotrader/traverson/TraversonBuilder.java +++ b/traverson4j-core/src/main/java/uk/co/autotrader/traverson/TraversonBuilder.java @@ -182,6 +182,33 @@ public Response delete(Class returnType) { return traverseAndPerform(Method.DELETE, null, returnType); } + /** + * Navigate the path and delete the resource + * + * @param body request body to send + * @return Response representing the http response + * @throws uk.co.autotrader.traverson.exception.UnknownRelException When navigating a path, a given rel cannot be found + * @throws uk.co.autotrader.traverson.exception.IllegalHttpStatusException When a non 2xx response is returned part way through traversing + * @throws uk.co.autotrader.traverson.exception.HttpException When the underlying http client experiences an issue with a request. This could be an intermittent issue + */ + public Response delete(Body body) { + return traverseAndPerform(Method.DELETE, body, JSONObject.class); + } + + /** + * Navigate the path and delete the resource + * + * @param body request body to send + * @param returnType Class of return type. + * @return Response representing the http response + * @throws uk.co.autotrader.traverson.exception.UnknownRelException When navigating a path, a given rel cannot be found + * @throws uk.co.autotrader.traverson.exception.IllegalHttpStatusException When a non 2xx response is returned part way through traversing + * @throws uk.co.autotrader.traverson.exception.HttpException When the underlying http client experiences an issue with a request. This could be an intermittent issue + */ + public Response delete(Body body, Class returnType) { + return traverseAndPerform(Method.DELETE, body, returnType); + } + /** * Navigate the path and post the body to the resource * diff --git a/traverson4j-core/src/test/java/uk/co/autotrader/traverson/TraversonBuilderTest.java b/traverson4j-core/src/test/java/uk/co/autotrader/traverson/TraversonBuilderTest.java index aa48962..206ca50 100644 --- a/traverson4j-core/src/test/java/uk/co/autotrader/traverson/TraversonBuilderTest.java +++ b/traverson4j-core/src/test/java/uk/co/autotrader/traverson/TraversonBuilderTest.java @@ -356,6 +356,30 @@ void delete_GivenInputsAndReturnType_BuildsRequestAndExecutes() throws Exception assertThat(request.getMethod()).isEqualTo(Method.DELETE); } + @Test + void delete_GivenInputsBodyAndReturnType_BuildsRequestAndExecutes() throws Exception { + when(client.execute(any(Request.class), eq(String.class))).thenReturn(stringResponse); + + Response response = builder.delete(body, String.class); + + Request request = reflectionGetRequest(); + assertThat(response).isEqualTo(stringResponse); + assertThat(request.getBody()).isEqualTo(body); + assertThat(request.getMethod()).isEqualTo(Method.DELETE); + } + + @Test + void delete_GivenInputsAndBody_BuildsRequestAndExecutes() throws Exception { + when(client.execute(any(Request.class), eq(JSONObject.class))).thenReturn(firstResponse); + + Response response = builder.delete(body); + + Request request = reflectionGetRequest(); + assertThat(response).isEqualTo(firstResponse); + assertThat(request.getBody()).isEqualTo(body); + assertThat(request.getMethod()).isEqualTo(Method.DELETE); + } + @Test void get_GivenRelToFollow_NavigatesToLastRelThenPerformsMethod() throws Exception { when(firstResponse.isSuccessful()).thenReturn(true); From e8042164c2bfe9607453e9a1d9339fd68fe6bb10 Mon Sep 17 00:00:00 2001 From: tom-edge Date: Mon, 24 Jul 2023 10:47:57 +0100 Subject: [PATCH 2/2] integration test sending of delete with a body --- .../co/autotrader/traverson/http/IntegrationTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/traverson4j-hc5/src/test/java/uk/co/autotrader/traverson/http/IntegrationTest.java b/traverson4j-hc5/src/test/java/uk/co/autotrader/traverson/http/IntegrationTest.java index 1ec16ea..a469321 100644 --- a/traverson4j-hc5/src/test/java/uk/co/autotrader/traverson/http/IntegrationTest.java +++ b/traverson4j-hc5/src/test/java/uk/co/autotrader/traverson/http/IntegrationTest.java @@ -76,6 +76,17 @@ void requestBody_SimpleTextBodyIsSerializedAndPostedCorrectly() { assertThat(response.getStatusCode()).isEqualTo(202); } + @Test + void requestBody_DeleteWithBodyIsSentCorrectly() { + wireMockServer.stubFor(delete(urlEqualTo("/records/1")) + .willReturn(WireMock.status(204))); + Response response = traverson.from("http://localhost:8089/records/1") + .delete(new TextBody("{\"key\":123}", "application/json", StandardCharsets.UTF_8)); + + wireMockServer.verify(1, deleteRequestedFor(urlEqualTo("/records/1")).withRequestBody(equalToJson("{\"key\":123}"))); + assertThat(response.getStatusCode()).isEqualTo(204); + } + @Test void requestBody_MultipartBodyIsSerializedAndPostedCorrectly() { byte[] data = new byte[]{0x00, 0x01, 0x02};