Skip to content

Commit

Permalink
Merge pull request #33 from tedgeat/delete_with_body
Browse files Browse the repository at this point in the history
allow sending of body on delete calls
  • Loading branch information
shaunstorey authored Jul 24, 2023
2 parents 723043b + e804216 commit 769c431
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,33 @@ public <T> Response<T> delete(Class<T> 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<JSONObject> 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 <T> Response<T> delete(Body body, Class<T> returnType) {
return traverseAndPerform(Method.DELETE, body, returnType);
}

/**
* Navigate the path and post the body to the resource
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<JSONObject> 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};
Expand Down

0 comments on commit 769c431

Please sign in to comment.