Skip to content

Commit 1a731d4

Browse files
committed
Merge branch '4.3' into 4.4
* 4.3: [Mercure] Added authorization and testing tips
2 parents 4c43f31 + c0b3256 commit 1a731d4

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

mercure.rst

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,22 @@ a JWT containing at least one target marking the update to the Hub.
363363
To provide this JWT, the subscriber can use a cookie,
364364
or a ``Authorization`` HTTP header.
365365
Cookies are automatically sent by the browsers when opening an ``EventSource`` connection.
366-
They are the most secure and preferred way when the client is a web browser.
366+
Using cookies is the most secure and preferred way when the client is a web browser.
367367
If the client is not a web browser, then using an authorization header is the way to go.
368368

369+
.. tip::
370+
371+
The native implementation of EventSource doesn't allow specifying headers.
372+
For example, authorization using Bearer token. In order to achieve that, use `a polyfill`_
373+
374+
.. code-block:: javascript
375+
376+
const es = new EventSourcePolyfill(url, {
377+
headers: {
378+
'Authorization': 'Bearer ' + token,
379+
}
380+
});
381+
369382
In the following example controller,
370383
the generated cookie contains a JWT, itself containing the appropriate targets.
371384
This cookie will be automatically sent by the web browser when connecting to the Hub.
@@ -529,6 +542,32 @@ These applications will render the content of Mercure updates in real-time.
529542
Checkout `the dedicated API Platform documentation`_ to learn more about
530543
its Mercure support.
531544

545+
Testing
546+
--------
547+
548+
During functional testing there is no need to send updates to Mercure. They will
549+
be handled by a stub publisher::
550+
551+
// tests/Functional/Fixtures/PublisherStub.php
552+
namespace App\Tests\Functional\Fixtures;
553+
554+
use Symfony\Component\Mercure\Update;
555+
556+
class PublisherStub
557+
{
558+
public function __invoke(Update $update): string
559+
{
560+
return '';
561+
}
562+
}
563+
564+
PublisherStub decorates the default publisher service so no updates are actually
565+
sent. Here is the PublisherStub implementation::
566+
567+
# config/services_test.yaml
568+
App\Tests\Functional\Fixtures\PublisherStub:
569+
decorates: mercure.hub.default.publisher
570+
532571
.. _`the Mercure protocol`: https://github.com/dunglas/mercure#protocol-specification
533572
.. _`Server-Sent Events (SSE)`: https://developer.mozilla.org/docs/Server-sent_events
534573
.. _`a polyfill`: https://github.com/Yaffle/EventSource

0 commit comments

Comments
 (0)