diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinkExtractors.java b/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinkExtractors.java index 4b1551ae..72a8a4a9 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinkExtractors.java +++ b/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinkExtractors.java @@ -24,11 +24,10 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletResponse; -import com.fasterxml.jackson.databind.ObjectMapper; - /** * Static factory methods providing a selection of {@link LinkExtractor link extractors} * for use when documentating a hypermedia-based API. @@ -65,15 +64,15 @@ public abstract class LinkExtractors { /** * Returns the {@code LinkExtractor} for the given {@code contentType} or {@code null} * if there is no extractor for the content type. - * - * @param contentType The content type + * + * @param contentType The content type, may include parameters * @return The extractor for the content type, or {@code null} */ public static LinkExtractor extractorForContentType(String contentType) { - if (MediaType.APPLICATION_JSON_VALUE.equals(contentType)) { + if (MediaType.parseMediaType(contentType).isCompatibleWith(MediaType.APPLICATION_JSON)) { return atomLinks(); } - else if ("application/hal+json".equals(contentType)) { + else if (MediaType.parseMediaType(contentType).isCompatibleWith(new MediaType("application","hal+json"))) { return halLinks(); } return null; diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsTests.java b/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsTests.java index 8af0ba5b..d5a8302e 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsTests.java +++ b/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsTests.java @@ -16,7 +16,9 @@ package org.springframework.restdocs.hypermedia; +import static org.hamcrest.core.IsNull.notNullValue; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import java.io.File; import java.io.FileReader; @@ -33,10 +35,8 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; +import org.springframework.http.InvalidMediaTypeException; import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.restdocs.hypermedia.Link; -import org.springframework.restdocs.hypermedia.LinkExtractor; -import org.springframework.restdocs.hypermedia.LinkExtractors; import org.springframework.util.FileCopyUtils; /** @@ -62,6 +62,23 @@ public class LinkExtractorsTests { this.linkType = linkType; } + @Test(expected = InvalidMediaTypeException.class) + public void emptyContentType() { + LinkExtractors.extractorForContentType(null); + } + + @Test + public void combinedContentTypeMatches() { + LinkExtractor linkExtractor = LinkExtractors.extractorForContentType("application/json;charset=UTF-8"); + assertThat(linkExtractor, notNullValue()); + } + + @Test + public void notDefinedMediaTypesMatches() { + LinkExtractor linkExtractor = LinkExtractors.extractorForContentType("application/hal+json;charset=UTF-8"); + assertThat(linkExtractor, notNullValue()); + } + @Test public void singleLink() throws IOException { Map> links = this.linkExtractor