PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/libraries/SolrPhpClient/tests/Apache/Solr/HttpTransport/AbstractTest.php

https://bitbucket.org/Mitsuroseba/test-drupal-project
PHP | 208 lines | 109 code | 42 blank | 57 comment | 0 complexity | bfa14bda65406011bace7e164a2ba49a MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Copyright (c) 2007-2011, Servigistics, Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * - Neither the name of Servigistics, Inc. nor the names of
  15. * its contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
  31. * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
  32. *
  33. * @package Apache
  34. * @subpackage Solr
  35. * @author Donovan Jimenez <djimenez@conduit-it.com>
  36. */
  37. /**
  38. * Apache_Solr_HttpTransport_Abstract Unit Tests
  39. */
  40. abstract class Apache_Solr_HttpTransport_AbstractTest extends PHPUnit_Framework_TestCase
  41. {
  42. const TIMEOUT = 2;
  43. // request our copyright file from googlecode for GET and HEAD
  44. const GET_URL = "http://solr-php-client.googlecode.com/svn/trunk/COPYING";
  45. const GET_RESPONSE_MIME_TYPE = 'text/plain';
  46. const GET_RESPONSE_ENCODING = 'UTF-8';
  47. const GET_RESPONSE_MATCH = 'Copyright (c) ';
  48. // post to the issue list page with a search for 'meh'
  49. const POST_URL = "http://code.google.com/p/solr-php-client/issues/list";
  50. const POST_DATA = "can=2&q=meh&colspec=ID+Type+Status+Priority+Milestone+Owner+Summary&cells=tiles";
  51. const POST_REQUEST_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=UTF-8';
  52. const POST_RESPONSE_MIME_TYPE = 'text/html';
  53. const POST_RESPONSE_ENCODING = 'UTF-8';
  54. //const POST_RESPONSE_MATCH = 'not sure';
  55. abstract public function getFixture();
  56. public function testGetDefaultTimeoutWithDefaultConstructor()
  57. {
  58. $fixture = $this->getFixture();
  59. $timeout = $fixture->getDefaultTimeout();
  60. $this->assertGreaterThan(0, $timeout);
  61. }
  62. public function testGetDefaultTimeoutSetToSixtyForBadValues()
  63. {
  64. // first set our default_socket_timeout ini setting
  65. $previousValue = ini_get('default_socket_timeout');
  66. ini_set('default_socket_timeout', 0);
  67. $fixture = $this->getFixture();
  68. $timeout = $fixture->getDefaultTimeout();
  69. // reset timeout
  70. ini_set('default_socket_timeout', $previousValue);
  71. $this->assertEquals(60, $timeout);
  72. }
  73. public function testSetDefaultTimeout()
  74. {
  75. $newTimeout = 1234;
  76. $fixture = $this->getFixture();
  77. $fixture->setDefaultTimeout($newTimeout);
  78. $timeout = $fixture->getDefaultTimeout();
  79. $this->assertEquals($newTimeout, $timeout);
  80. }
  81. public function testPerformGetRequest()
  82. {
  83. $fixture = $this->getFixture();
  84. $fixture->setDefaultTimeout(self::TIMEOUT);
  85. $response = $fixture->performGetRequest(self::GET_URL);
  86. $this->assertType('Apache_Solr_HttpTransport_Response', $response);
  87. $this->assertEquals(200, $response->getStatusCode(), 'Status code was not 200');
  88. $this->assertEquals(self::GET_RESPONSE_MIME_TYPE, $response->getMimeType(), 'mimetype was not correct');
  89. $this->assertEquals(self::GET_RESPONSE_ENCODING, $response->getEncoding(), 'character encoding was not correct');
  90. $this->assertStringStartsWith(self::GET_RESPONSE_MATCH, $response->getBody(), 'body did not start with match text');
  91. }
  92. public function testPerformGetRequestWithTimeout()
  93. {
  94. $fixture = $this->getFixture();
  95. $response = $fixture->performGetRequest(self::GET_URL, self::TIMEOUT);
  96. $this->assertType('Apache_Solr_HttpTransport_Response', $response);
  97. $this->assertEquals(200, $response->getStatusCode(), 'Status code was not 200');
  98. $this->assertEquals(self::GET_RESPONSE_MIME_TYPE, $response->getMimeType(), 'mimetype was not correct');
  99. $this->assertEquals(self::GET_RESPONSE_ENCODING, $response->getEncoding(), 'character encoding was not correct');
  100. $this->assertStringStartsWith(self::GET_RESPONSE_MATCH, $response->getBody(), 'body did not start with match text');
  101. }
  102. public function testPerformHeadRequest()
  103. {
  104. $fixture = $this->getFixture();
  105. $fixture->setDefaultTimeout(self::TIMEOUT);
  106. $response = $fixture->performHeadRequest(self::GET_URL);
  107. // we should get everything the same as a get, except the body
  108. $this->assertType('Apache_Solr_HttpTransport_Response', $response);
  109. $this->assertEquals(200, $response->getStatusCode(), 'Status code was not 200');
  110. $this->assertEquals(self::GET_RESPONSE_MIME_TYPE, $response->getMimeType(), 'mimetype was not correct');
  111. $this->assertEquals(self::GET_RESPONSE_ENCODING, $response->getEncoding(), 'character encoding was not correct');
  112. $this->assertEquals("", $response->getBody(), 'body was not empty');
  113. }
  114. public function testPerformHeadRequestWithTimeout()
  115. {
  116. $fixture = $this->getFixture();
  117. $response = $fixture->performHeadRequest(self::GET_URL, self::TIMEOUT);
  118. // we should get everything the same as a get, except the body
  119. $this->assertType('Apache_Solr_HttpTransport_Response', $response);
  120. $this->assertEquals(200, $response->getStatusCode(), 'Status code was not 200');
  121. $this->assertEquals(self::GET_RESPONSE_MIME_TYPE, $response->getMimeType(), 'mimetype was not correct');
  122. $this->assertEquals(self::GET_RESPONSE_ENCODING, $response->getEncoding(), 'character encoding was not correct');
  123. $this->assertEquals("", $response->getBody(), 'body was not empty');
  124. }
  125. public function testPerformPostRequest()
  126. {
  127. $fixture = $this->getFixture();
  128. $fixture->setDefaultTimeout(self::TIMEOUT);
  129. $response = $fixture->performPostRequest(self::POST_URL, self::POST_DATA, self::POST_REQUEST_CONTENT_TYPE);
  130. $this->assertType('Apache_Solr_HttpTransport_Response', $response);
  131. $this->assertEquals(200, $response->getStatusCode(), 'Status code was not 200');
  132. $this->assertEquals(self::POST_RESPONSE_MIME_TYPE, $response->getMimeType(), 'mimetype was not correct');
  133. $this->assertEquals(self::POST_RESPONSE_ENCODING, $response->getEncoding(), 'character encoding was not correct');
  134. //$this->assertStringStartsWith(self::POST_RESPONSE_MATCH, $response->getBody(), 'body did not start with match text');
  135. }
  136. public function testPerformPostRequestWithTimeout()
  137. {
  138. $fixture = $this->getFixture();
  139. $response = $fixture->performPostRequest(self::POST_URL, self::POST_DATA, self::POST_REQUEST_CONTENT_TYPE, self::TIMEOUT);
  140. $this->assertType('Apache_Solr_HttpTransport_Response', $response);
  141. $this->assertEquals(200, $response->getStatusCode(), 'Status code was not 200');
  142. $this->assertEquals(self::POST_RESPONSE_MIME_TYPE, $response->getMimeType(), 'mimetype was not correct');
  143. $this->assertEquals(self::POST_RESPONSE_ENCODING, $response->getEncoding(), 'character encoding was not correct');
  144. //$this->assertStringStartsWith(self::POST_RESPONSE_MATCH, $response->getBody(), 'body did not start with match text');
  145. }
  146. /**
  147. * Test one session doing multiple requests in multiple orders
  148. */
  149. public function testMultipleRequests()
  150. {
  151. // initial get request
  152. $this->testPerformGetRequest();
  153. // head following get
  154. $this->testPerformHeadRequest();
  155. // post following head
  156. $this->testPerformPostRequest();
  157. // get following post
  158. $this->testPerformGetRequest();
  159. // post following get
  160. $this->testPerformPostRequest();
  161. // head following post
  162. $this->testPerformHeadRequest();
  163. // get following post
  164. $this->testPerformGetRequest();
  165. }
  166. }