Skip to content

Commit

Permalink
story #8206 Add Location header
Browse files Browse the repository at this point in the history
Other tools that interact with Tuleap need Location HTTP header
in order to know the full path of some REST resources

Change-Id: I5fe55c3e47dbdfd4151e259ccc30cbfcf51505bf
  • Loading branch information
yannis-rossetto committed Jul 24, 2015
1 parent 8b952b4 commit 0a6ebda
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
20 changes: 16 additions & 4 deletions plugins/tracker/include/REST/v1/ArtifactsResource.class.php
Expand Up @@ -45,6 +45,7 @@
use \Tuleap\Tracker\REST\Artifact\ArtifactReference;
use \Tracker_URLVerification;
use \Tracker_Artifact_Changeset as Changeset;
use \Tuleap\Tracker\REST\Artifact\ArtifactRepresentation;

class ArtifactsResource extends AuthenticatedResource {
const MAX_LIMIT = 50;
Expand Down Expand Up @@ -145,12 +146,16 @@ public function getId($id, $values_format = self::VALUES_DEFAULT) {
$this->sendETagHeader($artifact);

if ($values_format === self::VALUES_DEFAULT || $values_format === self::VALUES_FORMAT_COLLECTION) {
return $this->builder->getArtifactRepresentationWithFieldValues($user, $artifact);
$representation = $this->builder->getArtifactRepresentationWithFieldValues($user, $artifact);
} elseif ($values_format === self::VALUES_FORMAT_BY_FIELD) {
return $this->builder->getArtifactRepresentationWithFieldValuesByFieldValues($user, $artifact);
$representation = $this->builder->getArtifactRepresentationWithFieldValuesByFieldValues($user, $artifact);
} elseif ($values_format === self::VALUES_FORMAT_ALL) {
return $this->builder->getArtifactRepresentationWithFieldValuesInBothFormat($user, $artifact);
$representation = $this->builder->getArtifactRepresentationWithFieldValuesInBothFormat($user, $artifact);
}

$this->sendLocationHeader($representation->uri);

return $representation;
}

/**
Expand Down Expand Up @@ -353,6 +358,7 @@ protected function post(TrackerReference $tracker, array $values = array(), arra

$this->sendLastModifiedHeader($artifact_reference->getArtifact());
$this->sendETagHeader($artifact_reference->getArtifact());
$this->sendLocationHeader($artifact_reference->uri);
return $artifact_reference;
} catch (Tracker_FormElement_InvalidFieldException $exception) {
throw new RestException(400, $exception->getMessage());
Expand Down Expand Up @@ -403,4 +409,10 @@ private function sendLastModifiedHeader(Tracker_Artifact $artifact) {
private function sendETagHeader(Tracker_Artifact $artifact) {
Header::eTag($artifact->getVersionIdentifier());
}
}

private function sendLocationHeader($uri) {
$uri_with_api_version = '/api/v1/' . $uri;

Header::Location($uri_with_api_version);
}
}
9 changes: 9 additions & 0 deletions src/common/REST/Header.class.php
Expand Up @@ -19,6 +19,8 @@

namespace Tuleap\REST;

use ForgeConfig;

class Header {
const GET = 'GET';
const OPTIONS = 'OPTIONS';
Expand All @@ -31,6 +33,7 @@ class Header {
const ALLOW = 'Allow';
const LAST_MODIFIED = 'Last-Modified';
const ETAG = 'Etag';
const LOCATION = 'Location';

const X_PAGINATION_LIMIT = 'X-PAGINATION-LIMIT';
const X_PAGINATION_OFFSET = 'X-PAGINATION-OFFSET';
Expand All @@ -47,6 +50,12 @@ public static function ETag($hash) {
self::sendHeader(self::ETAG, $hash);
}

public static function Location($uri) {
$route = 'https://' . ForgeConfig::get('sys_default_domain') . $uri;

self::sendHeader(self::LOCATION, $route);
}

public static function allowOptions() {
self::sendAllowHeaders(array(self::OPTIONS));
}
Expand Down
6 changes: 6 additions & 0 deletions tests/rest/ArtifactsTest.php
Expand Up @@ -60,6 +60,8 @@ public function testPostArtifact() {
$this->assertEquals($response->getStatusCode(), 200);
$this->assertNotNull($response->getHeader('Last-Modified'));
$this->assertNotNull($response->getHeader('Etag'));
$this->assertNotNull($response->getHeader('Location'));

$artifact_reference = $response->json();
$this->assertGreaterThan(0, $artifact_reference['id']);

Expand All @@ -78,6 +80,10 @@ public function testGetArtifact() {
$response = $this->getResponse($this->client->get("artifacts/$artifact_id"));
$artifact = $response->json();

$this->assertNotNull($response->getHeader('Last-Modified'));
$this->assertNotNull($response->getHeader('Etag'));
$this->assertNotNull($response->getHeader('Location'));

$fields = $artifact['values'];

foreach($fields as $field) {
Expand Down

0 comments on commit 0a6ebda

Please sign in to comment.