#187, #188 - Fix header handling in Traverson.

Traverson correctly applies headers when using JsonPath to evaluate GET result: accepts the media types it is configured for and preserves additional headers.
This commit is contained in:
Dietrich Schulten
2014-05-31 15:36:10 +02:00
committed by Oliver Gierke
parent 345c710ddc
commit 4f7e1cb561
2 changed files with 42 additions and 3 deletions

View File

@@ -56,6 +56,7 @@ import com.jayway.jsonpath.JsonPath;
*
* @see https://github.com/basti1302/traverson
* @author Oliver Gierke
* @author Dietrich Schulten
* @since 0.11
*/
public class Traverson {
@@ -226,7 +227,7 @@ public class Traverson {
Assert.hasText(jsonPath, "JSON path must not be null or empty!");
String forObject = template.getForObject(traverseToFinalUrl(), String.class);
String forObject = template.exchange(traverseToFinalUrl(), GET, prepareRequest(headers), String.class).getBody();
return JsonPath.read(forObject, jsonPath);
}
@@ -239,8 +240,7 @@ public class Traverson {
public <T> ResponseEntity<T> toEntity(Class<T> type) {
Assert.notNull(type, "Target type must not be null!");
return template.getForEntity(traverseToFinalUrl(), type);
return template.exchange(traverseToFinalUrl(), GET, prepareRequest(headers), type);
}
private String traverseToFinalUrl() {

View File

@@ -29,6 +29,7 @@ import org.springframework.core.ParameterizedTypeReference;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
/**
@@ -120,6 +121,44 @@ public class TraversonTests {
assertThat(result.getContent().name, is("Keanu Reaves"));
}
/**
* @see #187
*/
@Test
public void sendsConfiguredHeadersForJsonPathExpression() {
String expectedHeader = "<http://www.example.com>;rel=\"home\"";
HttpHeaders headers = new HttpHeaders();
headers.add("Link", expectedHeader);
assertThat(traverson.follow("movies", "movie", "actor").//
withHeaders(headers).<String> toObject("$.name"), is("Keanu Reaves"));
verifyThatRequest(). //
havingPathEqualTo("/actors/d95dbf62-f900-4dfa-9de8-0fc71e02ffa4"). //
havingHeader("Link", hasItem(expectedHeader));
}
/**
* @see #187
*/
@Test
public void sendsConfiguredHeadersForToEntity() {
String expectedHeader = "<http://www.example.com>;rel=\"home\"";
HttpHeaders headers = new HttpHeaders();
headers.add("Link", expectedHeader);
traverson.follow("movies", "movie", "actor").//
withHeaders(headers).toEntity(Actor.class);
verifyThatRequest(). //
havingPathEqualTo("/actors/d95dbf62-f900-4dfa-9de8-0fc71e02ffa4"). //
havingHeader("Link", hasItem(expectedHeader));
}
private void setUpActors() {
Resource<Actor> actor = new Resource<Actor>(new Actor("Keanu Reaves"));