55use BeyondCode \LaravelWebSockets \Apps \App ;
66use BeyondCode \LaravelWebSockets \Tests \Mocks \Message ;
77use BeyondCode \LaravelWebSockets \WebSockets \Exceptions \ConnectionsOverCapacity ;
8+ use BeyondCode \LaravelWebSockets \WebSockets \Exceptions \OriginNotAllowed ;
89use BeyondCode \LaravelWebSockets \WebSockets \Exceptions \UnknownAppKey ;
910
1011class ConnectionTest extends TestCase
@@ -14,7 +15,7 @@ public function unknown_app_keys_can_not_connect()
1415 {
1516 $ this ->expectException (UnknownAppKey::class);
1617
17- $ this ->pusherServer ->onOpen ($ this ->getWebSocketConnection ('/?appKey= test ' ));
18+ $ this ->pusherServer ->onOpen ($ this ->getWebSocketConnection ('test ' ));
1819 }
1920
2021 /** @test */
@@ -65,4 +66,38 @@ public function ping_returns_pong()
6566
6667 $ connection ->assertSentEvent ('pusher:pong ' );
6768 }
69+
70+ /** @test */
71+ public function origin_validation_should_fail_for_no_origin ()
72+ {
73+ $ this ->expectException (OriginNotAllowed::class);
74+
75+ $ connection = $ this ->getWebSocketConnection ('TestOrigin ' );
76+
77+ $ this ->pusherServer ->onOpen ($ connection );
78+
79+ $ connection ->assertSentEvent ('pusher:connection_established ' );
80+ }
81+
82+ /** @test */
83+ public function origin_validation_should_fail_for_wrong_origin ()
84+ {
85+ $ this ->expectException (OriginNotAllowed::class);
86+
87+ $ connection = $ this ->getWebSocketConnection ('TestOrigin ' , ['Origin ' => 'https://google.ro ' ]);
88+
89+ $ this ->pusherServer ->onOpen ($ connection );
90+
91+ $ connection ->assertSentEvent ('pusher:connection_established ' );
92+ }
93+
94+ /** @test */
95+ public function origin_validation_should_pass_for_the_right_origin ()
96+ {
97+ $ connection = $ this ->getWebSocketConnection ('TestOrigin ' , ['Origin ' => 'https://test.origin.com ' ]);
98+
99+ $ this ->pusherServer ->onOpen ($ connection );
100+
101+ $ connection ->assertSentEvent ('pusher:connection_established ' );
102+ }
68103}
0 commit comments