Skip to content

Commit

Permalink
Follow Location header only on 201 response when body is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Oct 12, 2015
1 parent 4ccd0ee commit 9f4ba18
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Internal/ResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ public function createResource(
return new Resource($this->client);
}

$body = trim($this->fetchBody($request, $response));

if (201 === $response->getStatusCode() &&
'' === $body &&
$response->hasHeader('Location')) {
// Created response with Location header
return $this->client->get($response->getHeader('Location')[0]);
return $this->client->request('GET', $response->getHeader('Location')[0]);
}

if (!$this->isValidContentType($response)) {
Expand All @@ -45,7 +48,7 @@ public function createResource(
);
}

return $this->handleValidContentType($request, $response);
return $this->handleValidContentType($request, $response, $body);
}

private function isValidContentType(ResponseInterface $response)
Expand Down Expand Up @@ -85,10 +88,9 @@ private function handleInvalidContentType(

private function handleValidContentType(
RequestInterface $request,
ResponseInterface $response
ResponseInterface $response,
$body
) {
$body = $this->fetchBody($request, $response);

if ('' === $body) {
return new Resource($this->client);
}
Expand Down
55 changes: 55 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,61 @@ public function it_can_request()
$this->assertSame(['bar'], $lastRequest->getHeader('Foo'));
}

/**
* @test
*/
public function it_follows_location_for_created_response_with_empty_body()
{
$response1 = new Response(201, ['Location' => 'http://propilex.herokuapp.com/resource']);
$response2 = new Response(200, ['Content-Type' => 'application/hal+json'], '{"foo":"bar"}');

$httpClient = $this->getMock(HttpClientInterface::class);

$httpClient
->expects($this->exactly(2))
->method('send')
->will($this->onConsecutiveCalls(
$response1,
$response2
));

$client = new Client(
'http://propilex.herokuapp.com',
$httpClient
);

$resource = $client->post('');

$this->assertSame('bar', $resource->getProperty('foo'));
}

/**
* @test
*/
public function it_does_not_follow_location_for_created_response_with_non_empty_body()
{
$response = new Response(201, [
'Location' => 'http://propilex.herokuapp.com/resource',
'Content-Type' => 'application/hal+json'
], '{"foo":"bar"}');

$httpClient = $this->getMock(HttpClientInterface::class);

$httpClient
->expects($this->once())
->method('send')
->will($this->returnValue($response));

$client = new Client(
'http://propilex.herokuapp.com',
$httpClient
);

$resource = $client->post('');

$this->assertSame('bar', $resource->getProperty('foo'));
}

/**
* @test
*/
Expand Down

0 comments on commit 9f4ba18

Please sign in to comment.