|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SwooleTW\Http\Tests\Websocket; |
| 4 | + |
| 5 | +use Mockery as m; |
| 6 | +use Swoole\Http\Request; |
| 7 | +use Swoole\Http\Response; |
| 8 | +use SwooleTW\Http\Tests\TestCase; |
| 9 | +use SwooleTW\Http\Websocket\HandShakeHandler; |
| 10 | + |
| 11 | +class HandShakeHandlerTest extends TestCase |
| 12 | +{ |
| 13 | + public function testHandle() |
| 14 | + { |
| 15 | + // arrange |
| 16 | + $request = m::mock(Request::class); |
| 17 | + $request->header['sec-websocket-key'] = 'Bet8DkPFq9ZxvIBvPcNy1A=='; |
| 18 | + |
| 19 | + $response = m::mock(Response::class); |
| 20 | + $response->shouldReceive('header')->withAnyArgs()->times(4)->andReturnSelf(); |
| 21 | + $response->shouldReceive('status')->with(101)->once()->andReturnSelf(); |
| 22 | + $response->shouldReceive('end')->withAnyArgs()->once()->andReturnSelf(); |
| 23 | + |
| 24 | + $handler = new HandShakeHandler; |
| 25 | + |
| 26 | + // act |
| 27 | + $actual = $handler->handle($request, $response); |
| 28 | + |
| 29 | + // assert |
| 30 | + $this->assertTrue($actual); |
| 31 | + } |
| 32 | + |
| 33 | + public function testHandleReturnFalse() |
| 34 | + { |
| 35 | + // arrange |
| 36 | + $request = m::mock(Request::class); |
| 37 | + $request->header['sec-websocket-key'] = 'test'; |
| 38 | + |
| 39 | + $response = m::mock(Response::class); |
| 40 | + $response->shouldReceive('end')->withAnyArgs()->once()->andReturnSelf(); |
| 41 | + |
| 42 | + $handler = new HandShakeHandler; |
| 43 | + |
| 44 | + // act |
| 45 | + $actual = $handler->handle($request, $response); |
| 46 | + |
| 47 | + // assert |
| 48 | + $this->assertFalse($actual); |
| 49 | + } |
| 50 | + |
| 51 | + public function testHandleWithSecWebsocketProtocol() |
| 52 | + { |
| 53 | + // arrange |
| 54 | + $request = m::mock(Request::class); |
| 55 | + $request->header['sec-websocket-key'] = 'Bet8DkPFq9ZxvIBvPcNy1A=='; |
| 56 | + $request->header['sec-websocket-protocol'] = 'graphql-ws'; |
| 57 | + |
| 58 | + $response = m::mock(Response::class); |
| 59 | + $response->shouldReceive('header')->withAnyArgs()->times(5)->andReturnSelf(); |
| 60 | + $response->shouldReceive('status')->with(101)->once()->andReturnSelf(); |
| 61 | + $response->shouldReceive('end')->withAnyArgs()->once()->andReturnSelf(); |
| 62 | + |
| 63 | + $handler = new HandShakeHandler; |
| 64 | + |
| 65 | + // act |
| 66 | + $actual = $handler->handle($request, $response); |
| 67 | + |
| 68 | + // assert |
| 69 | + $this->assertTrue($actual); |
| 70 | + } |
| 71 | +} |
0 commit comments