@@ -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,32 @@ 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
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
+
532
571
.. _`the Mercure protocol` : https://github.com/dunglas/mercure#protocol-specification
533
572
.. _`Server-Sent Events (SSE)` : https://developer.mozilla.org/docs/Server-sent_events
534
573
.. _`a polyfill` : https://github.com/Yaffle/EventSource
0 commit comments