Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test for HandShakeHandler
  • Loading branch information
killtw committed Jun 11, 2019
commit 2bb52971cb10ce12d7924f4bfcbfae114b67200d
71 changes: 71 additions & 0 deletions tests/Websocket/HandShakeHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace SwooleTW\Http\Tests\Websocket;

use Mockery as m;
use Swoole\Http\Request;
use Swoole\Http\Response;
use SwooleTW\Http\Tests\TestCase;
use SwooleTW\Http\Websocket\HandShakeHandler;

class HandShakeHandlerTest extends TestCase
{
public function testHandle()
{
// arrange
$request = m::mock(Request::class);
$request->header['sec-websocket-key'] = 'Bet8DkPFq9ZxvIBvPcNy1A==';

$response = m::mock(Response::class);
$response->shouldReceive('header')->withAnyArgs()->times(4)->andReturnSelf();
$response->shouldReceive('status')->with(101)->once()->andReturnSelf();
$response->shouldReceive('end')->withAnyArgs()->once()->andReturnSelf();

$handler = new HandShakeHandler;

// act
$actual = $handler->handle($request, $response);

// assert
$this->assertTrue($actual);
}

public function testHandleReturnFalse()
{
// arrange
$request = m::mock(Request::class);
$request->header['sec-websocket-key'] = 'test';

$response = m::mock(Response::class);
$response->shouldReceive('end')->withAnyArgs()->once()->andReturnSelf();

$handler = new HandShakeHandler;

// act
$actual = $handler->handle($request, $response);

// assert
$this->assertFalse($actual);
}

public function testHandleWithSecWebsocketProtocol()
{
// arrange
$request = m::mock(Request::class);
$request->header['sec-websocket-key'] = 'Bet8DkPFq9ZxvIBvPcNy1A==';
$request->header['sec-websocket-protocol'] = 'graphql-ws';

$response = m::mock(Response::class);
$response->shouldReceive('header')->withAnyArgs()->times(5)->andReturnSelf();
$response->shouldReceive('status')->with(101)->once()->andReturnSelf();
$response->shouldReceive('end')->withAnyArgs()->once()->andReturnSelf();

$handler = new HandShakeHandler;

// act
$actual = $handler->handle($request, $response);

// assert
$this->assertTrue($actual);
}
}