-
Notifications
You must be signed in to change notification settings - Fork 470
Add HAL JSON UTF-8 variant to MediaTypes #470
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,23 +67,22 @@ public class JsonPathLinkDiscoverer implements LinkDiscoverer { | |
} | ||
|
||
private final String pathTemplate; | ||
private final MediaType mediaType; | ||
private final List<MediaType> mediaTypes; | ||
|
||
/** | ||
* Creates a new {@link JsonPathLinkDiscoverer} using the given path template supporting the given {@link MediaType}. | ||
* The template has to contain a single {@code %s} placeholder which will be replaced by the relation type. | ||
* | ||
* @param pathTemplate must not be {@literal null} or empty and contain a single placeholder. | ||
* @param mediaType the {@link MediaType} to support. | ||
* @param mediaTypes the {@link MediaType}s to support. | ||
*/ | ||
public JsonPathLinkDiscoverer(String pathTemplate, MediaType mediaType) { | ||
|
||
public JsonPathLinkDiscoverer(String pathTemplate, MediaType... mediaTypes) { | ||
Assert.hasText(pathTemplate, "Path template must not be null!"); | ||
Assert.isTrue(StringUtils.countOccurrencesOf(pathTemplate, "%s") == 1, | ||
"Path template must contain a single placeholder!"); | ||
|
||
this.pathTemplate = pathTemplate; | ||
this.mediaType = mediaType; | ||
this.mediaTypes = Arrays.asList(mediaTypes); | ||
} | ||
|
||
/* | ||
|
@@ -178,6 +177,16 @@ private List<Link> createLinksFrom(Object parseResult, String rel) { | |
*/ | ||
@Override | ||
public boolean supports(MediaType delimiter) { | ||
return this.mediaType == null ? true : this.mediaType.isCompatibleWith(delimiter); | ||
boolean supports = false; | ||
if (this.mediaTypes == null) { | ||
return true; | ||
} else { | ||
for (MediaType mediaType : this.mediaTypes) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wasn't sure what to do here either with the |
||
if (mediaType.isCompatibleWith(delimiter)) { | ||
supports = true; | ||
} | ||
} | ||
} | ||
return supports; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
|
||
import static org.hamcrest.Matchers.*; | ||
import static org.junit.Assert.*; | ||
import static org.springframework.test.web.client.ExpectedCount.times; | ||
import static org.springframework.test.web.client.MockRestServiceServer.*; | ||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*; | ||
import static org.springframework.test.web.client.response.MockRestResponseCreators.*; | ||
|
@@ -82,12 +83,17 @@ public void setUp() { | |
@Test | ||
public void usesResourceTypeReference() { | ||
|
||
server.expect(requestTo("/resource")).andRespond(withSuccess(RESOURCE, MediaTypes.HAL_JSON)); | ||
server.expect(times(1), requestTo("/resource")).andRespond(withSuccess(RESOURCE, MediaTypes.HAL_JSON)); | ||
server.expect(times(1), requestTo("/resource")).andRespond(withSuccess(RESOURCE, MediaTypes.HAL_JSON_UTF8)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicated server expect with |
||
|
||
ResponseEntity<Resource<User>> response = template.exchange("/resource", HttpMethod.GET, null, | ||
new ResourceType<User>() {}); | ||
|
||
assertExpectedUserResource(response.getBody()); | ||
|
||
response = template.exchange("/resource", HttpMethod.GET, null, new ResourceType<User>() {}); | ||
|
||
assertExpectedUserResource(response.getBody()); | ||
} | ||
|
||
/** | ||
|
@@ -96,7 +102,8 @@ public void usesResourceTypeReference() { | |
@Test | ||
public void usesResourcesTypeReference() { | ||
|
||
server.expect(requestTo("/resources")).andRespond(withSuccess(RESOURCES_OF_USER, MediaTypes.HAL_JSON)); | ||
server.expect(times(1), requestTo("/resources")).andRespond(withSuccess(RESOURCES_OF_USER, MediaTypes.HAL_JSON)); | ||
server.expect(times(1), requestTo("/resources")).andRespond(withSuccess(RESOURCES_OF_USER, MediaTypes.HAL_JSON_UTF8)); | ||
|
||
ResponseEntity<Resources<User>> response = template.exchange("/resources", HttpMethod.GET, null, | ||
new ResourcesType<User>() {}); | ||
|
@@ -108,6 +115,16 @@ public void usesResourcesTypeReference() { | |
|
||
assertThat(nested, hasSize(1)); | ||
assertExpectedUser(nested.iterator().next()); | ||
|
||
response = template.exchange("/resources", HttpMethod.GET, null, new ResourcesType<User>() {}); | ||
body = response.getBody(); | ||
|
||
assertThat(body.hasLink("self"), is(true)); | ||
|
||
nested = body.getContent(); | ||
|
||
assertThat(nested, hasSize(1)); | ||
assertExpectedUser(nested.iterator().next()); | ||
} | ||
|
||
/** | ||
|
@@ -116,7 +133,8 @@ public void usesResourcesTypeReference() { | |
@Test | ||
public void usesResourcesOfResourceTypeReference() { | ||
|
||
server.expect(requestTo("/resources")).andRespond(withSuccess(RESOURCES_OF_RESOURCE, MediaTypes.HAL_JSON)); | ||
server.expect(times(1), requestTo("/resources")).andRespond(withSuccess(RESOURCES_OF_RESOURCE, MediaTypes.HAL_JSON)); | ||
server.expect(times(1), requestTo("/resources")).andRespond(withSuccess(RESOURCES_OF_RESOURCE, MediaTypes.HAL_JSON_UTF8)); | ||
|
||
ResponseEntity<Resources<Resource<User>>> response = template.exchange("/resources", HttpMethod.GET, null, | ||
new ResourcesType<Resource<User>>() {}); | ||
|
@@ -128,6 +146,16 @@ public void usesResourcesOfResourceTypeReference() { | |
|
||
assertThat(nested, hasSize(1)); | ||
assertExpectedUserResource(nested.iterator().next()); | ||
|
||
response = template.exchange("/resources", HttpMethod.GET, null, new ResourcesType<Resource<User>>() {}); | ||
body = response.getBody(); | ||
|
||
assertThat(body.hasLink("self"), is(true)); | ||
|
||
nested = body.getContent(); | ||
|
||
assertThat(nested, hasSize(1)); | ||
assertExpectedUserResource(nested.iterator().next()); | ||
} | ||
|
||
private static void assertExpectedUserResource(Resource<User> user) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't quite sure what to do with this constructor. It seems that the
MediaType
is nullable so I made it varargs. I thought about another constructor with aList
ofMediaType
...