Skip to content

Commit 0be694c

Browse files
karserjaviereguiluz
authored andcommitted
[Mercure] Added authorization and testing tips
1 parent 93a73ca commit 0be694c

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

mercure.rst

Lines changed: 39 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,31 @@ 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 be handled by a stub publisher::
549+
550+
// tests/Functional/Fixtures/PublisherStub.php
551+
namespace App\Tests\Functional\Fixtures;
552+
553+
use Symfony\Component\Mercure\Update;
554+
555+
class PublisherStub
556+
{
557+
public function __invoke(Update $update): string
558+
{
559+
return '';
560+
}
561+
}
562+
563+
PublisherStub decorates the default publisher service so no updates are actually sent. Here is the PublisherStub implementation::
564+
565+
#services_test.yaml
566+
App\Tests\Functional\Fixtures\PublisherStub:
567+
decorates: mercure.hub.default.publisher
568+
569+
532570
.. _`the Mercure protocol`: https://github.com/dunglas/mercure#protocol-specification
533571
.. _`Server-Sent Events (SSE)`: https://developer.mozilla.org/docs/Server-sent_events
534572
.. _`a polyfill`: https://github.com/Yaffle/EventSource

0 commit comments

Comments
 (0)