HttpCode.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace Codeception\Util;
  3. /**
  4. * Class containing constants of HTTP Status Codes
  5. * and method to print HTTP code with its description.
  6. *
  7. * Usage:
  8. *
  9. * ```php
  10. * <?php
  11. * use \Codeception\Util\HttpCode;
  12. *
  13. * // using REST, PhpBrowser, or any Framework module
  14. * $I->seeResponseCodeIs(HttpCode::OK);
  15. * $I->dontSeeResponseCodeIs(HttpCode::NOT_FOUND);
  16. * ```
  17. *
  18. *
  19. */
  20. class HttpCode
  21. {
  22. const SWITCHING_PROTOCOLS = 101;
  23. const PROCESSING = 102; // RFC2518
  24. const OK = 200;
  25. const CREATED = 201;
  26. const ACCEPTED = 202;
  27. const NON_AUTHORITATIVE_INFORMATION = 203;
  28. const NO_CONTENT = 204;
  29. const RESET_CONTENT = 205;
  30. const PARTIAL_CONTENT = 206;
  31. const MULTI_STATUS = 207; // RFC4918
  32. const ALREADY_REPORTED = 208; // RFC5842
  33. const IM_USED = 226; // RFC3229
  34. const MULTIPLE_CHOICES = 300;
  35. const MOVED_PERMANENTLY = 301;
  36. const FOUND = 302;
  37. const SEE_OTHER = 303;
  38. const NOT_MODIFIED = 304;
  39. const USE_PROXY = 305;
  40. const RESERVED = 306;
  41. const TEMPORARY_REDIRECT = 307;
  42. const PERMANENTLY_REDIRECT = 308; // RFC7238
  43. const BAD_REQUEST = 400;
  44. const UNAUTHORIZED = 401;
  45. const PAYMENT_REQUIRED = 402;
  46. const FORBIDDEN = 403;
  47. const NOT_FOUND = 404;
  48. const METHOD_NOT_ALLOWED = 405;
  49. const NOT_ACCEPTABLE = 406;
  50. const PROXY_AUTHENTICATION_REQUIRED = 407;
  51. const REQUEST_TIMEOUT = 408;
  52. const CONFLICT = 409;
  53. const GONE = 410;
  54. const LENGTH_REQUIRED = 411;
  55. const PRECONDITION_FAILED = 412;
  56. const REQUEST_ENTITY_TOO_LARGE = 413;
  57. const REQUEST_URI_TOO_LONG = 414;
  58. const UNSUPPORTED_MEDIA_TYPE = 415;
  59. const REQUESTED_RANGE_NOT_SATISFIABLE = 416;
  60. const EXPECTATION_FAILED = 417;
  61. const I_AM_A_TEAPOT = 418; // RFC2324
  62. const UNPROCESSABLE_ENTITY = 422; // RFC4918
  63. const LOCKED = 423; // RFC4918
  64. const FAILED_DEPENDENCY = 424; // RFC4918
  65. const RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL = 425; // RFC2817
  66. const UPGRADE_REQUIRED = 426; // RFC2817
  67. const PRECONDITION_REQUIRED = 428; // RFC6585
  68. const TOO_MANY_REQUESTS = 429; // RFC6585
  69. const REQUEST_HEADER_FIELDS_TOO_LARGE = 431; // RFC6585
  70. const INTERNAL_SERVER_ERROR = 500;
  71. const NOT_IMPLEMENTED = 501;
  72. const BAD_GATEWAY = 502;
  73. const SERVICE_UNAVAILABLE = 503;
  74. const GATEWAY_TIMEOUT = 504;
  75. const VERSION_NOT_SUPPORTED = 505;
  76. const VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506; // RFC2295
  77. const INSUFFICIENT_STORAGE = 507; // RFC4918
  78. const LOOP_DETECTED = 508; // RFC5842
  79. const NOT_EXTENDED = 510; // RFC2774
  80. const NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585
  81. private static $codes = [
  82. 100 => 'Continue',
  83. 102 => 'Processing',
  84. 200 => 'OK',
  85. 201 => 'Created',
  86. 202 => 'Accepted',
  87. 203 => 'Non-Authoritative Information',
  88. 204 => 'No Content',
  89. 205 => 'Reset Content',
  90. 206 => 'Partial Content',
  91. 207 => 'Multi-Status',
  92. 208 => 'Already Reported',
  93. 226 => 'IM Used',
  94. 300 => 'Multiple Choices',
  95. 301 => 'Moved Permanently',
  96. 302 => 'Found',
  97. 303 => 'See Other',
  98. 304 => 'Not Modified',
  99. 305 => 'Use Proxy',
  100. 307 => 'Temporary Redirect',
  101. 308 => 'Permanent Redirect',
  102. 400 => 'Bad Request',
  103. 401 => 'Unauthorized',
  104. 402 => 'Payment Required',
  105. 403 => 'Forbidden',
  106. 404 => 'Not Found',
  107. 405 => 'Method Not Allowed',
  108. 406 => 'Not Acceptable',
  109. 407 => 'Proxy Authentication Required',
  110. 408 => 'Request Timeout',
  111. 409 => 'Conflict',
  112. 410 => 'Gone',
  113. 411 => 'Length Required',
  114. 412 => 'Precondition Failed',
  115. 413 => 'Request Entity Too Large',
  116. 414 => 'Request-URI Too Long',
  117. 415 => 'Unsupported Media Type',
  118. 416 => 'Requested Range Not Satisfiable',
  119. 417 => 'Expectation Failed',
  120. 421 => 'Misdirected Request',
  121. 422 => 'Unprocessable Entity',
  122. 423 => 'Locked',
  123. 424 => 'Failed Dependency',
  124. 426 => 'Upgrade Required',
  125. 428 => 'Precondition Required',
  126. 429 => 'Too Many Requests',
  127. 431 => 'Request Header Fields Too Large',
  128. 500 => 'Internal Server Error',
  129. 501 => 'Not Implemented',
  130. 502 => 'Bad Gateway',
  131. 503 => 'Service Unavailable',
  132. 504 => 'Gateway Timeout',
  133. 505 => 'HTTP Version Not Supported',
  134. 506 => 'Variant Also Negotiates',
  135. 507 => 'Insufficient Storage',
  136. 508 => 'Loop Detected',
  137. 510 => 'Not Extended',
  138. 511 => 'Network Authentication Required'
  139. ];
  140. /**
  141. * Returns string with HTTP code and its description
  142. *
  143. * ```php
  144. * <?php
  145. * HttpCode::getDescription(200); // '200 (OK)'
  146. * HttpCode::getDescription(401); // '401 (Unauthorized)'
  147. * ```
  148. *
  149. * @param $code
  150. * @return mixed
  151. */
  152. public static function getDescription($code)
  153. {
  154. if (isset(self::$codes[$code])) {
  155. return sprintf('%d (%s)', $code, self::$codes[$code]);
  156. }
  157. return $code;
  158. }
  159. }