Skip to content

Commit

Permalink
Use response listener only for non-ajax http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
neeckeloo committed Jan 24, 2014
1 parent c64f570 commit a22dad0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 28 deletions.
9 changes: 8 additions & 1 deletion src/NewRelic/Listener/ResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
104 changes: 77 additions & 27 deletions tests/NewRelic/Listener/ResponseListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div class="browser-timing-header"></div>'));
$client
->expects($this->any())
->method('getBrowserTimingFooter')
->will($this->returnValue('<div class="browser-timing-footer"></div>'));
$this->listener->setClient($client);

$this->event = new MvcEvent();
}

Expand All @@ -58,35 +44,99 @@ 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('<div class="browser-timing-header"></div>'));
$client
->expects($this->once())
->method('getBrowserTimingFooter')
->will($this->returnValue('<div class="browser-timing-footer"></div>'));

$request = new \Zend\Http\Request();
$this->event->setRequest($request);

$response = new \Zend\Stdlib\Response();
$response->setContent('<html><head></head><body></body></html>');
$this->event->setResponse($response);
$this->listener->onResponse($this->event);

$response = $this->event->getResponse();
$this->assertInstanceOf('Zend\Stdlib\Response', $response);

$content = $response->getContent();
$this->assertContains('<head><div class="browser-timing-header"></div></head>', $content);
$this->assertContains('<body><div class="browser-timing-footer"></div></body>', $content);
}

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('<html><head></head><body></body></html>');
$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('<head><div class="browser-timing-header"></div></head>', $content);
$this->assertNotContains('<body><div class="browser-timing-footer"></div></body>', $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()
Expand Down

0 comments on commit a22dad0

Please sign in to comment.