PageRenderTime 99ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/min_unit_tests/test_environment.php

http://github.com/mrclay/minify
PHP | 138 lines | 122 code | 12 blank | 4 comment | 20 complexity | 8b4e8faf89f719fec31aa35408bff32c MD5 | raw file
  1. <?php
  2. //phpinfo(); exit();
  3. if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
  4. // called directly
  5. if (isset($_GET['getOutputCompression'])) {
  6. echo (int)ini_get('zlib.output_compression');
  7. exit();
  8. }
  9. if (isset($_GET['hello'])) {
  10. // try to disable (may not work)
  11. ini_set('zlib.output_compression', '0');
  12. $type = ($_GET['hello'] == 'js')
  13. ? 'application/x-javascript'
  14. : "text/{$_GET['hello']}";
  15. header("Content-Type: {$type}");
  16. echo 'World!';
  17. exit();
  18. }
  19. }
  20. require_once '_inc.php';
  21. function test_environment()
  22. {
  23. global $thisDir;
  24. // check DOCROOT
  25. $noSlash = assertTrue(
  26. 0 === preg_match('@[\\\\/]$@', $_SERVER['DOCUMENT_ROOT'])
  27. ,'environment : DOCUMENT_ROOT should not end in trailing slash'
  28. );
  29. $isRealPath = assertTrue(false !== realpath($_SERVER['DOCUMENT_ROOT'])
  30. ,'environment : DOCUMENT_ROOT should pass realpath()'
  31. );
  32. $containsThisFile = assertTrue(
  33. 0 === strpos(realpath(__FILE__), realpath($_SERVER['DOCUMENT_ROOT']))
  34. ,'environment : DOCUMENT_ROOT should contain this test file'
  35. );
  36. if (! $noSlash || ! $isRealPath || ! $containsThisFile) {
  37. echo "\nDOCUMENT_ROOT is set to: '{$_SERVER['DOCUMENT_ROOT']}'. If you "
  38. . "cannot modify this, consider setting \$min_documentRoot in config.php\n\n";
  39. }
  40. if (isset($_SERVER['SUBDOMAIN_DOCUMENT_ROOT'])) {
  41. echo "\n environment : \$_SERVER['SUBDOMAIN_DOCUMENT_ROOT'] is set. "
  42. . "You may need to set \$min_documentRoot to this in config.php\n";
  43. }
  44. if (realpath(__FILE__) !== realpath($_SERVER['DOCUMENT_ROOT'] . '/min_unit_tests/test_environment.php')) {
  45. echo " environment : /min_unit_tests/ is not directly inside DOCUMENT_ROOT\n";
  46. }
  47. $thisUrl = 'http://'
  48. . $_SERVER['HTTP_HOST'] // avoid redirects when SERVER_NAME doesn't match
  49. . ('80' === $_SERVER['SERVER_PORT'] ? '' : ":{$_SERVER['SERVER_PORT']}")
  50. . dirname($_SERVER['REQUEST_URI'])
  51. . '/test_environment.php';
  52. $oc = @file_get_contents($thisUrl . '?getOutputCompression=1');
  53. if (false === $oc || ! preg_match('/^[01]$/', $oc)) {
  54. echo "!---: environment : Local HTTP request failed. Testing cannot continue.\n";
  55. return;
  56. }
  57. if ('1' === $oc) {
  58. echo "!---: environment : zlib.output_compression is enabled in php.ini"
  59. . " or .htaccess.\n";
  60. }
  61. $testJs = _test_environment_getHello($thisUrl . '?hello=js');
  62. $passed = assertTrue(
  63. $testJs['length'] == 6
  64. ,'environment : PHP/server should not auto-encode application/x-javascript output'
  65. );
  66. $testCss = _test_environment_getHello($thisUrl . '?hello=css');
  67. $passed = $passed && assertTrue(
  68. $testCss['length'] == 6
  69. ,'environment : PHP/server should not auto-encode text/css output'
  70. );
  71. $testHtml = _test_environment_getHello($thisUrl . '?hello=html');
  72. $passed = $passed && assertTrue(
  73. $testHtml['length'] == 6
  74. ,'environment : PHP/server should not auto-encode text/html output'
  75. );
  76. if (! $passed) {
  77. $testFake = _test_environment_getHello($thisUrl . '?hello=faketype');
  78. if ($testFake['length'] == 6) {
  79. echo " environment : Server does not auto-encode arbitrary types. This\n"
  80. . " may indicate that the auto-encoding is caused by Apache's\n"
  81. . " AddOutputFilterByType.";
  82. }
  83. }
  84. }
  85. function _test_environment_getHello($url)
  86. {
  87. $fp = fopen($url, 'r', false, stream_context_create(array(
  88. 'http' => array(
  89. 'method' => "GET",
  90. 'timeout' => '10',
  91. 'header' => "Accept-Encoding: deflate, gzip\r\n",
  92. )
  93. )));
  94. $meta = stream_get_meta_data($fp);
  95. $encoding = '';
  96. $length = 0;
  97. foreach ($meta['wrapper_data'] as $i => $header) {
  98. if (preg_match('@^Content-Length:\\s*(\\d+)$@i', $header, $m)) {
  99. $length = $m[1];
  100. } elseif (preg_match('@^Content-Encoding:\\s*(\\S+)$@i', $header, $m)) {
  101. if ($m[1] !== 'identity') {
  102. $encoding = $m[1];
  103. }
  104. }
  105. }
  106. $streamContents = stream_get_contents($fp);
  107. fclose($fp);
  108. if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
  109. if ($length != 6) {
  110. echo "\nReturned content should be 6 bytes and not HTTP encoded.\n"
  111. . "Headers returned by: {$url}\n\n";
  112. var_export($meta['wrapper_data']);
  113. echo "\n\n";
  114. }
  115. }
  116. return array(
  117. 'length' => $length
  118. ,'encoding' => $encoding
  119. ,'bytes' => $streamContents
  120. );
  121. }
  122. test_environment();