Skip to content

Commit

Permalink
Merge pull request #35 from chadicus/fea/content-type
Browse files Browse the repository at this point in the history
Add Content-Type header to response
  • Loading branch information
chadicus authored Jan 4, 2017
2 parents 359a78a + c24aa92 commit 3fd1a2a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ public function __construct(OAuth2\Server $server)
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $arguments = [])
{
return ResponseBridge::fromOAuth2(
$this->server->handleTokenRequest(
RequestBridge::toOAuth2($request)
)
$response = ResponseBridge::fromOAuth2(
$this->server->handleTokenRequest(RequestBridge::toOAuth2($request))
);

if ($response->hasHeader('Content-Type')) {
return $response;
}

return $response->withHeader('Content-Type', 'application/json');
}
}
69 changes: 69 additions & 0 deletions tests/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,73 @@ public function invoke()
$actual
);
}

/**
* Verify Content-Type header is added to response
*
* @test
* @covers ::__invoke
*
* @return void
*/
public function invokeAddsContentType()
{
$storage = new Storage\Memory(
[
'client_credentials' => [
'testClientId' => [
'client_id' => 'testClientId',
'client_secret' => 'testClientSecret',
],
],
]
);

$server = new OAuth2\Server(
$storage,
[
'access_lifetime' => 3600,
],
[
new GrantType\ClientCredentials($storage),
]
);

$uri = 'localhost:8888/token';

$headers = ['Content-Type' => ['application/json']];

$body = [
'client_id' => 'testClientId',
'client_secret' => 'testClientSecret',
'grant_type' => 'client_credentials',
];
$request = new ServerRequest(['REQUEST_METHOD' => 'POST'], [], $uri, 'POST', 'php://input', $headers, [], [], $body);

$route = new Token($server);

$response = $route($request, new Response());

$this->assertSame('application/json', $response->getHeaderLine('Content-Type'));
}

/**
* Verify Content-Type header remains unchanged if OAuth2 response contains the header.
*
* @test
* @covers ::__invoke
*
* @return void
*/
public function invokeRetainsContentType()
{
$oauth2ServerMock = $this->getMockBuilder('\\OAuth2\\Server')->disableOriginalConstructor()->getMock();
$oauth2ServerMock->method('handleTokenRequest')->willReturn(
new OAuth2\Response([], 200, ['Content-Type' => 'text/html'])
);

$route = new Token($oauth2ServerMock);
$response = $route(new ServerRequest(), new Response());
$this->assertSame('text/html', $response->getHeaderLine('Content-Type'));
}
}

0 comments on commit 3fd1a2a

Please sign in to comment.