Skip to content

Support for official HAL and HAL-FORMS media type in link extraction #965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@
* content type.
*
* @author Andy Wilkinson
* @author Oliver Drotbohm
*/
class ContentTypeLinkExtractor implements LinkExtractor {

private Map<MediaType, LinkExtractor> linkExtractors = new HashMap<>();

ContentTypeLinkExtractor() {
this.linkExtractors.put(MediaType.APPLICATION_JSON, new AtomLinkExtractor());
this.linkExtractors.put(HalLinkExtractor.HAL_MEDIA_TYPE, new HalLinkExtractor());

LinkExtractor halLinkExtractor = new HalLinkExtractor();
this.linkExtractors.put(HalLinkExtractor.HAL_MEDIA_TYPE, halLinkExtractor);
this.linkExtractors.put(HalLinkExtractor.VND_HAL_MEDIA_TYPE, halLinkExtractor);
this.linkExtractors.put(HalLinkExtractor.HAL_FORMS_MEDIA_TYPE, halLinkExtractor);
}

ContentTypeLinkExtractor(Map<MediaType, LinkExtractor> linkExtractors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
class HalLinkExtractor extends AbstractJsonLinkExtractor {

static final MediaType HAL_MEDIA_TYPE = new MediaType("application", "hal+json");
static final MediaType VND_HAL_MEDIA_TYPE = new MediaType("application", "vnd.hal+json");
static final MediaType HAL_FORMS_MEDIA_TYPE = new MediaType("application", "prs.hal-forms+json");

@Override
public Map<String, List<Link>> extractLinks(Map<String, Object> json) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
Expand All @@ -28,6 +29,7 @@
import org.springframework.restdocs.operation.OperationResponse;
import org.springframework.restdocs.operation.OperationResponseFactory;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand All @@ -41,6 +43,8 @@ public class ContentTypeLinkExtractorTests {

private final OperationResponseFactory responseFactory = new OperationResponseFactory();

private final String halBody = "{ \"_links\" : { \"someRel\" : { \"href\" : \"someHref\" }} }";

@Test
public void extractionFailsWithNullContentType() {
assertThatIllegalStateException().isThrownBy(() -> new ContentTypeLinkExtractor()
Expand Down Expand Up @@ -71,4 +75,22 @@ public void extractorCalledWithCompatibleContextType() throws IOException {
verify(extractor).extractLinks(response);
}

@Test
public void extractsLinksFromVndHalMediaType() throws IOException {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.parseMediaType("application/vnd.hal+json"));
OperationResponse response = this.responseFactory.create(HttpStatus.OK, httpHeaders, this.halBody.getBytes());
Map<String, List<Link>> links = new ContentTypeLinkExtractor().extractLinks(response);
assertThat(links).containsKey("someRel");
}

@Test
public void extractsLinksFromHalFormsMediaType() throws IOException {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.parseMediaType("application/prs.hal-forms+json"));
OperationResponse response = this.responseFactory.create(HttpStatus.OK, httpHeaders, this.halBody.getBytes());
Map<String, List<Link>> links = new ContentTypeLinkExtractor().extractLinks(response);
assertThat(links).containsKey("someRel");
}

}
Loading