Overview

Namespaces

  • Jyxo
    • Beholder
      • TestCase
    • Gettext
      • Parser
    • Input
      • Chain
      • Filter
      • Validator
    • Mail
      • Email
        • Attachment
      • Parser
      • Sender
    • Rpc
      • Json
      • Xml
    • Shell
    • Spl
    • Svn
    • Time
    • Webdav
  • PHP

Classes

  • FileSystem
  • HttpResponse
  • Imap
  • JsonRpc
  • Memcached
  • Mysql
  • Pgsql
  • PhpExtension
  • PhpVersion
  • PhpZend
  • Smtp
  • Webdav
  • XmlRpc
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * Jyxo PHP Library
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file license.txt.
 10:  * It is also available through the world-wide-web at this URL:
 11:  * https://github.com/jyxo/php/blob/master/license.txt
 12:  */
 13: 
 14: namespace Jyxo\Beholder\TestCase;
 15: 
 16: /**
 17:  * Common HTTP response test.
 18:  * Checks only availability in the default form, but can be easily extended with additional checks.
 19:  *
 20:  * Example:
 21:  * <code>
 22:  * new \Jyxo\Beholder\TestCase\HttpResponse('Foo', 'http://example.com/', array('body' => '/this text must be in body/m'))
 23:  * </code>
 24:  *
 25:  * @category Jyxo
 26:  * @package Jyxo\Beholder
 27:  * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
 28:  * @license https://github.com/jyxo/php/blob/master/license.txt
 29:  * @author Jan Kaštánek
 30:  */
 31: class HttpResponse extends \Jyxo\Beholder\TestCase
 32: {
 33:     /**
 34:      * Tested URL.
 35:      *
 36:      * @var string
 37:      */
 38:     private $url;
 39: 
 40:     /**
 41:      * Custom tests.
 42:      *
 43:      * @var array
 44:      */
 45:     private $tests;
 46: 
 47:     /**
 48:      * Constructor. Gets the testing URL and optional custom tests.
 49:      *
 50:      * @param string $description Test description
 51:      * @param string $url Tested URL
 52:      * @param array $tests Custom tests
 53:      */
 54:     public function __construct($description, $url, array $tests = array())
 55:     {
 56:         parent::__construct($description);
 57: 
 58:         $this->url = (string) $url;
 59:         $this->tests = $tests;
 60:     }
 61: 
 62:     /**
 63:      * Performs the test.
 64:      *
 65:      * @return \Jyxo\Beholder\Result
 66:      */
 67:     public function run()
 68:     {
 69:         // The http extension is required
 70:         if (!extension_loaded('http')) {
 71:             return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Extension http missing');
 72:         }
 73: 
 74:         $http = new \HttpRequest(
 75:             $this->url, \HttpRequest::METH_GET, array(
 76:                 'connecttimeout' => 5, 'timeout' => 10, 'useragent' => 'JyxoBeholder'
 77:             )
 78:         );
 79: 
 80:         try {
 81:             $http->send();
 82:             if (200 !== $http->getResponseCode()) {
 83:                 throw new \Exception(sprintf('Http error: %s', $http->getResponseCode()));
 84:             }
 85:             if (isset($this->tests['body'])) {
 86:                 $body = $http->getResponseBody();
 87:                 if (!preg_match($this->tests['body'], $body)) {
 88:                     $body = trim(strip_tags($body));
 89:                     throw new \Exception(sprintf('Invalid body: %s', \Jyxo\String::cut($body, 16)));
 90:                 }
 91:             }
 92: 
 93:             // OK
 94:             return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::SUCCESS);
 95: 
 96:         } catch (\HttpException $e) {
 97:             $inner = $e;
 98:             while (null !== $inner->innerException) {
 99:                 $inner = $inner->innerException;
100:             }
101:             return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, $inner->getMessage());
102: 
103:         } catch (\Exception $e) {
104:             return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, $e->getMessage());
105:         }
106:     }
107: }
108: 
Jyxo PHP Library API documentation generated by ApiGen 2.3.0