PageRenderTime 30ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/api/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Log/LogPluginTest.php

https://gitlab.com/x33n/respond
PHP | 95 lines | 75 code | 15 blank | 5 comment | 0 complexity | 48ef41335496ea5d223e3ad3c1341c36 MD5 | raw file
  1. <?php
  2. namespace Guzzle\Tests\Plugin\Log;
  3. use Guzzle\Log\ClosureLogAdapter;
  4. use Guzzle\Http\Client;
  5. use Guzzle\Http\EntityBody;
  6. use Guzzle\Http\Message\Request;
  7. use Guzzle\Http\Message\Response;
  8. use Guzzle\Plugin\Log\LogPlugin;
  9. use Guzzle\Common\Event;
  10. /**
  11. * @group server
  12. * @covers Guzzle\Plugin\Log\LogPlugin
  13. */
  14. class LogPluginTest extends \Guzzle\Tests\GuzzleTestCase
  15. {
  16. protected $adapter;
  17. public function setUp()
  18. {
  19. $this->adapter = new ClosureLogAdapter(function ($message) {
  20. echo $message;
  21. });
  22. }
  23. public function testIgnoresCurlEventsWhenNotWiringBodies()
  24. {
  25. $p = new LogPlugin($this->adapter);
  26. $this->assertNotEmpty($p->getSubscribedEvents());
  27. $event = new Event(array('request' => new Request('GET', 'http://foo.com')));
  28. $p->onCurlRead($event);
  29. $p->onCurlWrite($event);
  30. $p->onRequestBeforeSend($event);
  31. }
  32. public function testLogsWhenComplete()
  33. {
  34. $output = '';
  35. $p = new LogPlugin(new ClosureLogAdapter(function ($message) use (&$output) {
  36. $output = $message;
  37. }), '{method} {resource} | {code} {res_body}');
  38. $p->onRequestSent(new Event(array(
  39. 'request' => new Request('GET', 'http://foo.com'),
  40. 'response' => new Response(200, array(), 'Foo')
  41. )));
  42. $this->assertEquals('GET / | 200 Foo', $output);
  43. }
  44. public function testWiresBodiesWhenNeeded()
  45. {
  46. $client = new Client($this->getServer()->getUrl());
  47. $plugin = new LogPlugin($this->adapter, '{req_body} | {res_body}', true);
  48. $client->getEventDispatcher()->addSubscriber($plugin);
  49. $request = $client->put();
  50. // Send the response from the dummy server as the request body
  51. $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nsend");
  52. $stream = fopen($this->getServer()->getUrl(), 'r');
  53. $request->setBody(EntityBody::factory($stream, 4));
  54. $tmpFile = tempnam(sys_get_temp_dir(), 'non_repeatable');
  55. $request->setResponseBody(EntityBody::factory(fopen($tmpFile, 'w')));
  56. $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 8\r\n\r\nresponse");
  57. ob_start();
  58. $request->send();
  59. $message = ob_get_clean();
  60. unlink($tmpFile);
  61. $this->assertContains("send", $message);
  62. $this->assertContains("response", $message);
  63. }
  64. public function testHasHelpfulStaticFactoryMethod()
  65. {
  66. $s = fopen('php://temp', 'r+');
  67. $client = new Client();
  68. $client->addSubscriber(LogPlugin::getDebugPlugin(true, $s));
  69. $request = $client->put('http://foo.com', array('Content-Type' => 'Foo'), 'Bar');
  70. $request->setresponse(new Response(200), true);
  71. $request->send();
  72. rewind($s);
  73. $contents = stream_get_contents($s);
  74. $this->assertContains('# Request:', $contents);
  75. $this->assertContainsIns('PUT / HTTP/1.1', $contents);
  76. $this->assertContains('# Response:', $contents);
  77. $this->assertContainsIns('HTTP/1.1 200 OK', $contents);
  78. $this->assertContains('# Errors:', $contents);
  79. }
  80. }