@@ -363,9 +363,22 @@ a JWT containing at least one target marking the update to the Hub.
363
363
To provide this JWT, the subscriber can use a cookie,
364
364
or a ``Authorization `` HTTP header.
365
365
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.
367
367
If the client is not a web browser, then using an authorization header is the way to go.
368
368
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
+
369
382
In the following example controller,
370
383
the generated cookie contains a JWT, itself containing the appropriate targets.
371
384
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.
529
542
Checkout `the dedicated API Platform documentation `_ to learn more about
530
543
its Mercure support.
531
544
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
+
532
570
.. _`the Mercure protocol` : https://github.com/dunglas/mercure#protocol-specification
533
571
.. _`Server-Sent Events (SSE)` : https://developer.mozilla.org/docs/Server-sent_events
534
572
.. _`a polyfill` : https://github.com/Yaffle/EventSource
0 commit comments