Ignore parameters when finding a LinkExtractor for a Content-Type

Closes gh-57
This commit is contained in:
Heiko Scherrer
2015-04-19 21:22:22 +02:00
committed by Andy Wilkinson
parent d8151e7b03
commit d0f0de537c
2 changed files with 25 additions and 9 deletions

View File

@@ -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;

View File

@@ -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<String, List<Link>> links = this.linkExtractor