Skip to content

Commit

Permalink
Merge pull request #20 from StyleT/feature/gzip-fix
Browse files Browse the repository at this point in the history
Work with gzip requests fixed
  • Loading branch information
bakura10 committed Jul 24, 2015
2 parents ad8a08a + a1ae146 commit c620418
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 89 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: php
php:
- 5.4
- 5.5
- 5.6

before_script:
- composer self-update
Expand Down
28 changes: 1 addition & 27 deletions src/ZfrPrerender/Mvc/PrerenderListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,7 @@ public function prerenderPage(MvcEvent $event)
$prerenderEvent = new PrerenderEvent($request, $response);
$eventManager->trigger(PrerenderEvent::EVENT_PRERENDER_POST, $prerenderEvent);

$originalRequestHeaders = $originalRequest->getHeaders();

if ($originalRequestHeaders->has('Accept-Encoding')
&& $originalRequestHeaders->get('Accept-Encoding')->getFieldValue() === 'gzip'
) {
return $this->decompressResponse($prerenderEvent->getResponse());
} else {
return $prerenderEvent->getResponse();
}
return $prerenderEvent->getResponse();
}

/**
Expand Down Expand Up @@ -267,24 +259,6 @@ protected function isBlacklisted($uri, $referer, array $blacklistUrls)
return false;
}

/**
* Decompress the response if it contains Content-Encoding to gzip
*
* @param HttpResponse $response
* @return HttpResponse
*/
private function decompressResponse(HttpResponse $response)
{
$headers = $response->getHeaders();
$decode = gzdecode($response->getBody());

$response->setContent($decode);
$headers->addHeaderLine('Content-Length', strlen($decode));
$headers->removeHeader($headers->get('Content-Encoding'));

return $response;
}

/**
* {@inheritDoc}
*/
Expand Down
62 changes: 0 additions & 62 deletions tests/ZfrPrerenderTest/Mvc/PrerenderListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,66 +404,4 @@ public function testAddHeaderIfTokenIsSpecified()
$moduleOptions->getPrerenderToken()
);
}

public function testCanUncompressGzipResponses()
{
$mvcEvent = new MvcEvent();
$request = new HttpRequest();

$request->setUri('http://www.example.com');
$request->getHeaders()->addHeaderLine('User-Agent', 'Baiduspider+(+http://www.baidu.com/search/spider.htm)');
$request->getHeaders()->addHeaderLine('Accept-Encoding', 'gzip');
$mvcEvent->setRequest($request);

/** @var \ZfrPrerender\Options\ModuleOptions $moduleOptions */
$moduleOptions = ServiceManagerFactory::getServiceManager()->get('ZfrPrerender\Options\ModuleOptions');

$prerenderRequest = new HttpRequest();
$listener = new PrerenderListener($moduleOptions);

$response = $this->getMock('Zend\Http\Response');

// Mock the client
$clientMock = $this->getMock('Zend\Http\Client');
$clientMock->expects($this->once())
->method('setUri')
->with($moduleOptions->getPrerenderUrl() . '/' . $request->getUriString())
->will($this->returnValue($clientMock));

$clientMock->expects($this->once())
->method('setMethod')
->with('GET');

$clientMock->expects($this->once())
->method('send')
->will($this->returnValue($response));

$clientMock->expects($this->once())
->method('getRequest')
->will($this->returnValue($prerenderRequest));

$headers = new Headers();
$headers->addHeaderLine('Content-Encoding', 'gzip');

$response->expects($this->once())
->method('getHeaders')
->will($this->returnValue($headers));

$response->expects($this->once())
->method('getBody')
->will($this->returnValue(gzencode('original value')));

$response->expects($this->once())
->method('setContent')
->with('original value');

$listener->setHttpClient($clientMock);

$result = $listener->prerenderPage($mvcEvent);

$this->assertInstanceOf('Zend\Http\Response', $result);
$this->assertFalse($headers->has('Content-Encoding'), 'Ensure header has been removed');
$this->assertTrue($headers->has('Content-Length'), 'Ensure content length has been added');
$this->assertEquals(strlen('original value'), $headers->get('Content-Length')->getFieldValue());
}
}

0 comments on commit c620418

Please sign in to comment.