Merge pull request #965 from odrotbohm
* gh-965: Polish "Support link extraction with official HAL and HAL-FORMS media types" Support link extraction with official HAL and HAL-FORMS media types Closes gh-965
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
* Copyright 2014-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,6 +30,7 @@ import org.springframework.restdocs.operation.OperationResponse;
|
||||
* content type.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
class ContentTypeLinkExtractor implements LinkExtractor {
|
||||
|
||||
@@ -37,7 +38,10 @@ class ContentTypeLinkExtractor implements LinkExtractor {
|
||||
|
||||
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) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,11 +30,16 @@ import org.springframework.http.MediaType;
|
||||
* format.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
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) {
|
||||
Map<String, List<Link>> extractedLinks = new LinkedHashMap<>();
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.restdocs.hypermedia;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -28,6 +29,7 @@ import org.springframework.http.MediaType;
|
||||
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;
|
||||
@@ -41,6 +43,8 @@ class ContentTypeLinkExtractorTests {
|
||||
|
||||
private final OperationResponseFactory responseFactory = new OperationResponseFactory();
|
||||
|
||||
private final String halBody = "{ \"_links\" : { \"someRel\" : { \"href\" : \"someHref\" }} }";
|
||||
|
||||
@Test
|
||||
void extractionFailsWithNullContentType() {
|
||||
assertThatIllegalStateException().isThrownBy(() -> new ContentTypeLinkExtractor()
|
||||
@@ -71,4 +75,22 @@ class ContentTypeLinkExtractorTests {
|
||||
verify(extractor).extractLinks(response);
|
||||
}
|
||||
|
||||
@Test
|
||||
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
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user