ResponseTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_Response Unit Tests
  39. */
  40. class Apache_Solr_HttpTransport_ResponseTest extends PHPUnit_Framework_TestCase
  41. {
  42. // generated with the following query string: select?q=solr&wt=json
  43. const STATUS_CODE_200 = 200;
  44. const STATUS_MESSAGE_200 = "OK";
  45. const BODY_200 = '{"responseHeader":{"status":0,"QTime":0,"params":{"q":"solr","wt":"json"}},"response":{"numFound":0,"start":0,"docs":[]}}';
  46. const BODY_200_WITH_DOCUMENTS = '{"responseHeader":{"status":0,"QTime":0,"params":{"q":"*:*","wt":"json"}},"response":{"numFound":1,"start":0,"docs":[{"guid":"dev/2/products/45410/1236981","cit_domain":"products","cit_client":"2","cit_instance":"dev","cit_timestamp":"2010-10-06T18:16:51.573Z","product_code_t":["235784"],"product_id":[1236981],"dealer_id":[45410],"category_id":[1030],"manufacturer_id":[0],"vendor_id":[472],"catalog_id":[202]}]}}';
  47. const CONTENT_TYPE_200 = "text/plain; charset=utf-8";
  48. const MIME_TYPE_200 = "text/plain";
  49. const ENCODING_200 = "utf-8";
  50. // generated with the following query string: select?qt=standad&q=solr&wt=json
  51. // NOTE: the intentional mispelling of the standard in the qt parameter
  52. const STATUS_CODE_400 = 400;
  53. const STATUS_MESSAGE_400 = "Bad Request";
  54. const BODY_400 = '<html><head><title>Apache Tomcat/6.0.24 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 400 - unknown handler: standad</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>unknown handler: standad</u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect (unknown handler: standad).</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.24</h3></body></html>';
  55. const CONTENT_TYPE_400 = "text/html; charset=utf-8";
  56. const MIME_TYPE_400 = "text/html";
  57. const ENCODING_400 = "utf-8";
  58. // generated with the following query string: select?q=solr&wt=json on a core that does not exist
  59. const STATUS_CODE_404 = 404;
  60. const STATUS_MESSAGE_404 = "Not Found";
  61. const BODY_404 = '<html><head><title>Apache Tomcat/6.0.24 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 404 - /solr/doesnotexist/select</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>/solr/doesnotexist/select</u></p><p><b>description</b> <u>The requested resource (/solr/doesnotexist/select) is not available.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.24</h3></body></html>';
  62. const CONTENT_TYPE_404 = "text/html; charset=utf-8";
  63. const MIME_TYPE_404 = "text/html";
  64. const ENCODING_404 = "utf-8";
  65. public static function get0Response()
  66. {
  67. return new Apache_Solr_HttpTransport_Response(null, null, null);
  68. }
  69. public static function get200Response()
  70. {
  71. return new Apache_Solr_HttpTransport_Response(self::STATUS_CODE_200, self::CONTENT_TYPE_200, self::BODY_200);
  72. }
  73. public static function get200ResponseWithDocuments()
  74. {
  75. return new Apache_Solr_HttpTransport_Response(self::STATUS_CODE_200, self::CONTENT_TYPE_200, self::BODY_200_WITH_DOCUMENTS);
  76. }
  77. public static function get400Response()
  78. {
  79. return new Apache_Solr_HttpTransport_Response(self::STATUS_CODE_400, self::CONTENT_TYPE_400, self::BODY_400);
  80. }
  81. public static function get404Response()
  82. {
  83. return new Apache_Solr_HttpTransport_Response(self::STATUS_CODE_404, self::CONTENT_TYPE_404, self::BODY_404);
  84. }
  85. public function testGetStatusCode()
  86. {
  87. $fixture = self::get200Response();
  88. $statusCode = $fixture->getStatusCode();
  89. $this->assertEquals(self::STATUS_CODE_200, $statusCode);
  90. }
  91. public function testGetStatusMessage()
  92. {
  93. $fixture = self::get200Response();
  94. $statusMessage = $fixture->getStatusMessage();
  95. $this->assertEquals(self::STATUS_MESSAGE_200, $statusMessage);
  96. }
  97. public function testGetStatusMessageWithUnknownCode()
  98. {
  99. $fixture = new Apache_Solr_HttpTransport_Response(499, null, null);
  100. $statusMessage = $fixture->getStatusMessage();
  101. $this->assertEquals("Unknown Status", $statusMessage);
  102. }
  103. public function testGetBody()
  104. {
  105. $fixture = self::get200Response();
  106. $body = $fixture->getBody();
  107. $this->assertEquals(self::BODY_200, $body);
  108. }
  109. public function testGetMimeType()
  110. {
  111. $fixture = self::get200Response();
  112. $mimeType = $fixture->getMimeType();
  113. $this->assertEquals(self::MIME_TYPE_200, $mimeType);
  114. }
  115. public function testGetEncoding()
  116. {
  117. $fixture = self::get200Response();
  118. $encoding = $fixture->getEncoding();
  119. $this->assertEquals(self::ENCODING_200, $encoding);
  120. }
  121. public function testGetStatusMessageWhenNotProvided()
  122. {
  123. // test 4 of the most common status code responses, probably don't need
  124. // to test all the codes we have
  125. $fixture = new Apache_Solr_HttpTransport_Response(null, null, null, null, null);
  126. $this->assertEquals("Communication Error", $fixture->getStatusMessage(), 'Did not get correct default status message for status code 0');
  127. $fixture = new Apache_Solr_HttpTransport_Response(200, null, null, null, null);
  128. $this->assertEquals("OK", $fixture->getStatusMessage(), 'Did not get correct default status message for status code 200');
  129. $fixture = new Apache_Solr_HttpTransport_Response(400, null, null, null, null);
  130. $this->assertEquals("Bad Request", $fixture->getStatusMessage(), 'Did not get correct default status message for status code 400');
  131. $fixture = new Apache_Solr_HttpTransport_Response(404, null, null, null, null);
  132. $this->assertEquals("Not Found", $fixture->getStatusMessage(), 'Did not get correct default status message for status code 404');
  133. }
  134. }