From a22dad0503d584230475acded986d3e7f30312a4 Mon Sep 17 00:00:00 2001 From: Nicolas Eeckeloo Date: Fri, 24 Jan 2014 12:38:11 +0100 Subject: [PATCH] Use response listener only for non-ajax http requests --- src/NewRelic/Listener/ResponseListener.php | 9 +- .../Listener/ResponseListenerTest.php | 104 +++++++++++++----- 2 files changed, 85 insertions(+), 28 deletions(-) diff --git a/src/NewRelic/Listener/ResponseListener.php b/src/NewRelic/Listener/ResponseListener.php index c8f037e..9cb840b 100644 --- a/src/NewRelic/Listener/ResponseListener.php +++ b/src/NewRelic/Listener/ResponseListener.php @@ -2,6 +2,7 @@ namespace NewRelic\Listener; use Zend\EventManager\EventManagerInterface as Events; +use Zend\Http\Request as HttpRequest; use Zend\Mvc\MvcEvent; class ResponseListener extends AbstractListener @@ -21,7 +22,13 @@ public function attach(Events $events) */ public function onResponse(MvcEvent $e) { - if (!$this->options->getBrowserTimingEnabled()) { + $request = $e->getRequest(); + + if ( + !$this->options->getBrowserTimingEnabled() + || !$request instanceof HttpRequest + || $request->isXmlHttpRequest() + ) { $this->client->disableAutorum(); return; } diff --git a/tests/NewRelic/Listener/ResponseListenerTest.php b/tests/NewRelic/Listener/ResponseListenerTest.php index 1bc19ba..30bc96e 100644 --- a/tests/NewRelic/Listener/ResponseListenerTest.php +++ b/tests/NewRelic/Listener/ResponseListenerTest.php @@ -35,20 +35,6 @@ public function setUp() $this->moduleOptions = new ModuleOptions(); $this->listener->setModuleOptions($this->moduleOptions); - $client = $this->getMockBuilder('NewRelic\Client') - ->disableOriginalConstructor() - ->getMock(); - - $client - ->expects($this->any()) - ->method('getBrowserTimingHeader') - ->will($this->returnValue('
')); - $client - ->expects($this->any()) - ->method('getBrowserTimingFooter') - ->will($this->returnValue('')); - $this->listener->setClient($client); - $this->event = new MvcEvent(); } @@ -58,14 +44,26 @@ public function testOnResponseWithoutAutoInstrument() ->setBrowserTimingEnabled(true) ->setBrowserTimingAutoInstrument(false); + $client = $this->getMock('NewRelic\Client'); + $this->listener->setClient($client); + + $client + ->expects($this->once()) + ->method('getBrowserTimingHeader') + ->will($this->returnValue('
')); + $client + ->expects($this->once()) + ->method('getBrowserTimingFooter') + ->will($this->returnValue('')); + + $request = new \Zend\Http\Request(); + $this->event->setRequest($request); + $response = new \Zend\Stdlib\Response(); $response->setContent(''); $this->event->setResponse($response); $this->listener->onResponse($this->event); - $response = $this->event->getResponse(); - $this->assertInstanceOf('Zend\Stdlib\Response', $response); - $content = $response->getContent(); $this->assertContains('
', $content); $this->assertContains('', $content); @@ -73,20 +71,72 @@ public function testOnResponseWithoutAutoInstrument() public function testOnResponseWithBrowserTimingDisabled() { - $this->moduleOptions - ->setBrowserTimingEnabled(false); + $this->moduleOptions->setBrowserTimingEnabled(false); + + $client = $this->getMock('NewRelic\Client'); + $this->listener->setClient($client); + + $client + ->expects($this->once()) + ->method('disableAutorum'); + $client + ->expects($this->never()) + ->method('getBrowserTimingHeader'); + $client + ->expects($this->never()) + ->method('getBrowserTimingFooter'); - $response = new \Zend\Stdlib\Response(); - $response->setContent(''); - $this->event->setResponse($response); $this->listener->onResponse($this->event); + } - $response = $this->event->getResponse(); - $this->assertInstanceOf('Zend\Stdlib\Response', $response); + public function testOnResponseNotInHttpRequestContext() + { + $this->moduleOptions->setBrowserTimingEnabled(true); - $content = $response->getContent(); - $this->assertNotContains('
', $content); - $this->assertNotContains('', $content); + $client = $this->getMock('NewRelic\Client'); + $this->listener->setClient($client); + + $client + ->expects($this->once()) + ->method('disableAutorum'); + $client + ->expects($this->never()) + ->method('getBrowserTimingHeader'); + $client + ->expects($this->never()) + ->method('getBrowserTimingFooter'); + + $request = new \Zend\Stdlib\Request(); + $this->event->setRequest($request); + + $this->listener->onResponse($this->event); + } + + public function testOnResponseInAjaxHttpRequestContext() + { + $this->moduleOptions->setBrowserTimingEnabled(true); + + $client = $this->getMock('NewRelic\Client'); + $this->listener->setClient($client); + + $client + ->expects($this->once()) + ->method('disableAutorum'); + $client + ->expects($this->never()) + ->method('getBrowserTimingHeader'); + $client + ->expects($this->never()) + ->method('getBrowserTimingFooter'); + + $request = $this->getMock('Zend\Http\Request'); + $request + ->expects($this->once()) + ->method('isXmlHttpRequest') + ->will($this->returnValue(true)); + $this->event->setRequest($request); + + $this->listener->onResponse($this->event); } public function testAttachAndDetachListener()