LoggingConnectorTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. use LeProxy\LeProxy\LoggingConnector;
  3. use React\Promise;
  4. class LoggingConnectorTest extends PHPUnit_Framework_TestCase
  5. {
  6. public function testConnectPendingDoesNotPrintAnything()
  7. {
  8. $promise = new Promise\Promise(function () { });
  9. $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  10. $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($promise);
  11. $logger = $this->getMockBuilder('LeProxy\LeProxy\Logger')->getMock();
  12. $logger->expects($this->never())->method('logConnection');
  13. $logger->expects($this->never())->method('logFailConnection');
  14. $connector = new LoggingConnector($connector, $logger);
  15. $connector->connect('example.com:80');
  16. }
  17. public function testConnectErrorWithoutSourcePrintsErrorWithoutSource()
  18. {
  19. $promise = Promise\reject(new RuntimeException('error'));
  20. $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  21. $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($promise);
  22. $logger = $this->getMockBuilder('LeProxy\LeProxy\Logger')->getMock();
  23. $logger->expects($this->once())->method('logFailConnection')->with(null, 'example.com:80', 'error');
  24. $connector = new LoggingConnector($connector, $logger);
  25. $connector->connect('example.com:80');
  26. }
  27. public function testConnectErrorWithSourcePrintsErrorWithSource()
  28. {
  29. $promise = Promise\reject(new RuntimeException('error'));
  30. $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  31. $connector->expects($this->once())->method('connect')->with($this->stringStartsWith('example.com:80?source='))->willReturn($promise);
  32. $logger = $this->getMockBuilder('LeProxy\LeProxy\Logger')->getMock();
  33. $logger->expects($this->once())->method('logFailConnection')->with('http://user:pass@host:8080', $this->stringStartsWith('example.com:80?source='), 'error');
  34. $connector = new LoggingConnector($connector, $logger);
  35. $connector->connect('example.com:80?source=' . rawurlencode('http://user:pass@host:8080'));
  36. }
  37. public function testConnectErrorWithoutSourcePrintsErrorMessageWithoutConnectionTargetRepeated()
  38. {
  39. $promise = Promise\reject(new RuntimeException('Connection to X failed: reasons'));
  40. $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  41. $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($promise);
  42. $logger = $this->getMockBuilder('LeProxy\LeProxy\Logger')->getMock();
  43. $logger->expects($this->once())->method('logFailConnection')->with(null, 'example.com:80', 'failed: reasons');
  44. $connector = new LoggingConnector($connector, $logger);
  45. $connector->connect('example.com:80');
  46. }
  47. public function testConnectSuccessWithoutSourcePrintsSuccessWithoutSource()
  48. {
  49. $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
  50. $promise = Promise\resolve($connection);
  51. $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  52. $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($promise);
  53. $logger = $this->getMockBuilder('LeProxy\LeProxy\Logger')->getMock();
  54. $logger->expects($this->once())->method('logConnection')->with(null, 'example.com:80', null);
  55. $connector = new LoggingConnector($connector, $logger);
  56. $connector->connect('example.com:80');
  57. }
  58. public function testConnectSuccessWithSourcePrintsSuccessWithSource()
  59. {
  60. $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
  61. $promise = Promise\resolve($connection);
  62. $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  63. $connector->expects($this->once())->method('connect')->with($this->stringStartsWith('example.com:80?source='))->willReturn($promise);
  64. $logger = $this->getMockBuilder('LeProxy\LeProxy\Logger')->getMock();
  65. $logger->expects($this->once())->method('logConnection')->with('http://user:pass@host:8080', $this->stringStartsWith('example.com:80?source='), null);
  66. $connector = new LoggingConnector($connector, $logger);
  67. $connector->connect('example.com:80?source=' . rawurlencode('http://user:pass@host:8080'));
  68. }
  69. public function testConnectSuccessWithoutRemoteIpPrintsSuccessWithDestinationAndIp()
  70. {
  71. $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
  72. $connection->expects($this->once())->method('getRemoteAddress')->willReturn('1.2.3.4:5060');
  73. $promise = Promise\resolve($connection);
  74. $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  75. $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($promise);
  76. $logger = $this->getMockBuilder('LeProxy\LeProxy\Logger')->getMock();
  77. $logger->expects($this->once())->method('logConnection')->with(null, 'example.com:80', '1.2.3.4:5060');
  78. $connector = new LoggingConnector($connector, $logger);
  79. $connector->connect('example.com:80');
  80. }
  81. }