Skip to content

Commit d9e7d7f

Browse files
committed
bug #52 Fix request token event (ajgarlag)
This PR was squashed before being merged into the 0.1-dev branch. Discussion ---------- Fix request token event I think #49 was merged too soon. This PR tries to fix the following flaws: 1. The test expectation is wrong. 2. The event listener in the test is being added after the event dispatch. 3. The response set to the event is being discarded by the controller. Commits ------- f14c02d Fix request token event
2 parents 464dfa8 + f14c02d commit d9e7d7f

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/Controller/TokenController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ public function indexAction(Request $request): Response
7070
$renderedResponse = $this->httpFoundationFactory->createResponse($response);
7171

7272
/** @var TokenRequestResolveEvent $event */
73-
$this->eventDispatcher->dispatch(
73+
$event = $this->eventDispatcher->dispatch(
7474
new TokenRequestResolveEvent($renderedResponse),
7575
OAuth2Events::TOKEN_REQUEST_RESOLVE
7676
);
7777

78-
return $renderedResponse;
78+
return $event->getResponse();
7979
}
8080
}

tests/Acceptance/TokenEndpointTest.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,32 @@ public function testSuccessfulRefreshTokenRequest(): void
105105
->get(RefreshTokenManagerInterface::class)
106106
->find(FixtureFactory::FIXTURE_REFRESH_TOKEN);
107107

108-
$this->client->request('POST', '/token', [
109-
'client_id' => 'foo',
110-
'client_secret' => 'secret',
111-
'grant_type' => 'refresh_token',
112-
'refresh_token' => TestHelper::generateEncryptedPayload($refreshToken),
113-
]);
114-
115108
$this->client
116109
->getContainer()
117110
->get('event_dispatcher')
118111
->addListener(OAuth2Events::TOKEN_REQUEST_RESOLVE, static function (TokenRequestResolveEvent $event): void {
119112
$event->getResponse()->headers->set('foo', 'bar');
120113
});
121114

115+
$this->client
116+
->getContainer()
117+
->get('event_dispatcher')
118+
->addListener(OAuth2Events::TOKEN_REQUEST_RESOLVE, static function (TokenRequestResolveEvent $event): void {
119+
if ('bar' === $event->getResponse()->headers->get('foo')) {
120+
$newResponse = clone $event->getResponse();
121+
$newResponse->headers->remove('foo');
122+
$newResponse->headers->set('baz', 'qux');
123+
$event->setResponse($newResponse);
124+
}
125+
}, -1);
126+
127+
$this->client->request('POST', '/token', [
128+
'client_id' => 'foo',
129+
'client_secret' => 'secret',
130+
'grant_type' => 'refresh_token',
131+
'refresh_token' => TestHelper::generateEncryptedPayload($refreshToken),
132+
]);
133+
122134
$response = $this->client->getResponse();
123135

124136
$this->assertSame(200, $response->getStatusCode());
@@ -131,7 +143,8 @@ public function testSuccessfulRefreshTokenRequest(): void
131143
$this->assertGreaterThan(0, $jsonResponse['expires_in']);
132144
$this->assertNotEmpty($jsonResponse['access_token']);
133145
$this->assertNotEmpty($jsonResponse['refresh_token']);
134-
$this->assertEmpty($response->headers->get('foo'), 'bar');
146+
$this->assertFalse($response->headers->has('foo'));
147+
$this->assertSame($response->headers->get('baz'), 'qux');
135148
}
136149

137150
public function testSuccessfulAuthorizationCodeRequest(): void

0 commit comments

Comments
 (0)