ProtocolDetectorTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. use LeProxy\LeProxy\ProtocolDetector;
  3. use React\Socket\Server;
  4. use React\Socket\ConnectionInterface;
  5. class ProtocolDetectorTest extends PHPUnit_Framework_TestCase
  6. {
  7. public function testCtor()
  8. {
  9. $socket = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
  10. $detector = new ProtocolDetector($socket);
  11. }
  12. public function testForwardsSocketErrorToHttp()
  13. {
  14. $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
  15. $socket = new Server(0, $loop);
  16. $socket->close();
  17. $detector = new ProtocolDetector($socket);
  18. $n = 0;
  19. $detector->http->on('error', function (Exception $e) use (&$n) {
  20. ++$n;
  21. });
  22. $socket->emit('error', array(new \RuntimeException()));
  23. $this->assertEquals(1, $n);
  24. }
  25. public function testForwardsHttpRequestToHttp()
  26. {
  27. $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
  28. $socket = new Server(0, $loop);
  29. $socket->close();
  30. $detector = new ProtocolDetector($socket);
  31. $received = null;
  32. $detector->http->on('connection', function (ConnectionInterface $conn) use (&$received) {
  33. $received = true;
  34. $conn->on('data', function ($chunk) use (&$received) {
  35. $received = $chunk;
  36. });
  37. });
  38. $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('close'))->getMock();
  39. $socket->emit('connection', array($connection));
  40. $connection->emit('data', array("GET / HTTP/1.0\r\n\r\n"));
  41. $this->assertEquals("GET / HTTP/1.0\r\n\r\n", $received);
  42. }
  43. public function testForwardsSocksRequestToSocks()
  44. {
  45. $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
  46. $socket = new Server(0, $loop);
  47. $socket->close();
  48. $detector = new ProtocolDetector($socket);
  49. $received = null;
  50. $detector->socks->on('connection', function (ConnectionInterface $conn) use (&$received) {
  51. $received = true;
  52. $conn->on('data', function ($chunk) use (&$received) {
  53. $received = $chunk;
  54. });
  55. });
  56. $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('close'))->getMock();
  57. $socket->emit('connection', array($connection));
  58. $connection->emit('data', array("\x05test"));
  59. $this->assertEquals("\x05test", $received);
  60. }
  61. }