Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Support for HTTP DELETE
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Oliver committed Oct 23, 2017
1 parent 64e9f30 commit dceb935
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
[Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Support for HTTP DELETE

## [1.8.0] - 2017-10-12

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static javax.ws.rs.client.Entity.entity;
import static javax.ws.rs.core.HttpHeaders.ACCEPT;
import static javax.ws.rs.core.HttpHeaders.CONTENT_TYPE;
import static uk.gov.justice.services.test.utils.core.rest.ResteasyClientBuilderFactory.clientBuilder;

import javax.ws.rs.client.Entity;
Expand Down Expand Up @@ -137,4 +138,33 @@ public Response query(final String url, final String contentTypes, final Multiva

return response;
}

/**
* Sends a DELETE command to the specified URL.
*
* @param url - the URL to post the command to.
* @param contentType - the content type of the command.
* @param headers - headers to be sent in the request.
* @return the Response from the command being issued.
*/
public Response deleteCommand(final String url, final String contentType, final MultivaluedMap<String, Object> headers) {

if (LOGGER.isInfoEnabled()) {
LOGGER.info("Making DELETE request to '{}' with Content Type '{}'", url, contentType);
}

final Response response = clientBuilder().build()
.target(url)
.request()
.headers(headers)
.header(CONTENT_TYPE, contentType)
.delete();

if (LOGGER.isInfoEnabled()) {
final Response.StatusType statusType = response.getStatusInfo();
LOGGER.info("Received response status '{}' '{}'", statusType.getStatusCode(), statusType.getReasonPhrase());
}

return response;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.justice.services.test.utils.core.rest;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
Expand All @@ -9,6 +10,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static javax.ws.rs.core.HttpHeaders.ACCEPT;
import static javax.ws.rs.core.HttpHeaders.CONTENT_TYPE;
import static javax.ws.rs.core.Response.Status.NO_CONTENT;
import static javax.ws.rs.core.Response.Status.OK;
import static org.junit.Assert.assertThat;

Expand All @@ -17,6 +19,7 @@
import javax.ws.rs.core.Response;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -29,6 +32,7 @@ public class RestClientTest {
private static final String REQUEST_BODY = "<request>body</request>";
private static final String RESOURCE_PATH = "/test";
private static final int OK_STATUS_CODE = OK.getStatusCode();
private static final int NO_CONTENT_CODE = NO_CONTENT.getStatusCode();
private static final String HEADER = "Header";
private static final String HEADER_VALUE = "HeaderValue";

Expand All @@ -50,9 +54,9 @@ public void shouldSendCommand() {
.withRequestBody(matching(REQUEST_BODY))
.willReturn(aResponse().withStatus(OK_STATUS_CODE)));

Response response = restClient.postCommand(URL, CONTENT_TYPE_VALUE, REQUEST_BODY);
final Response response = restClient.postCommand(URL, CONTENT_TYPE_VALUE, REQUEST_BODY);

assertThat(response.getStatus(), org.hamcrest.CoreMatchers.equalTo(OK_STATUS_CODE));
assertThat(response.getStatus(), CoreMatchers.equalTo(OK_STATUS_CODE));
}

@Test
Expand All @@ -64,7 +68,7 @@ public void shouldSendCommandWithHeaders() {
.withRequestBody(matching(REQUEST_BODY))
.willReturn(aResponse().withStatus(OK_STATUS_CODE)));

Response response = restClient.postCommand(URL, CONTENT_TYPE_VALUE, REQUEST_BODY, getHeaders());
final Response response = restClient.postCommand(URL, CONTENT_TYPE_VALUE, REQUEST_BODY, getHeaders());

assertThat(response.getStatus(), org.hamcrest.CoreMatchers.equalTo(OK_STATUS_CODE));
}
Expand All @@ -76,7 +80,7 @@ public void shouldSendQuery() {
.withHeader(ACCEPT, equalTo(CONTENT_TYPE_VALUE))
.willReturn(aResponse().withStatus(OK_STATUS_CODE)));

Response response = restClient.query(URL, CONTENT_TYPE_VALUE);
final Response response = restClient.query(URL, CONTENT_TYPE_VALUE);

assertThat(response.getStatus(), org.hamcrest.CoreMatchers.equalTo(OK_STATUS_CODE));
}
Expand All @@ -89,11 +93,24 @@ public void shouldSendQueryWithHeaders() {
.withHeader(HEADER, equalTo(HEADER_VALUE))
.willReturn(aResponse().withStatus(OK_STATUS_CODE)));

Response response = restClient.query(URL, CONTENT_TYPE_VALUE, getHeaders());
final Response response = restClient.query(URL, CONTENT_TYPE_VALUE, getHeaders());

assertThat(response.getStatus(), org.hamcrest.CoreMatchers.equalTo(OK_STATUS_CODE));
}

@Test
public void shouldSendDelete() {

stubFor(delete(urlEqualTo(RESOURCE_PATH))
.withHeader(CONTENT_TYPE, equalTo(CONTENT_TYPE_VALUE))
.withHeader(HEADER, equalTo(HEADER_VALUE))
.willReturn(aResponse().withStatus(NO_CONTENT_CODE)));

final Response response = restClient.deleteCommand(URL, CONTENT_TYPE_VALUE, getHeaders());

assertThat(response.getStatus(), org.hamcrest.CoreMatchers.equalTo(NO_CONTENT_CODE));
}

private MultivaluedMap<String, Object> getHeaders() {
final MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
headers.putSingle(HEADER, HEADER_VALUE);
Expand Down

0 comments on commit dceb935

Please sign in to comment.