/** * @covers ::getMethodsWhereRequestBodyIsAllowed() */ public function testGetMethodsWhereRequestBodyIsAllowed() { $result = HttpMethods::getMethodsWhereRequestBodyIsAllowed(); $http_methods_expected = array(HttpMethods::POST, HttpMethods::PUT); foreach ($http_methods_expected as $http_method_expected) { $this->assertContains($http_method_expected, $result, 'HTTP method ' . $http_method_expected . ' is not present in getAllMethods()'); } }
/** * This is the main method every webserver must have called * * @throws Exception Will be thrown if there's a severe problem with the underlying PHP * @throws RestException Will be thrown if there's a formal error with the client request */ public function handle() { // Access-Control-Allow-Origin header should always be set $this->header_manager->addHeader("Access-Control-Allow-Origin", join(', ', $this->cors_allowed_origin)); $http_methods_allowed = HttpMethods::getMethodsWhereRequestBodyIsAllowed(); $http_methods_allowed[] = HttpMethods::GET; if (in_array($this->getMethod(), $http_methods_allowed)) { try { $this->data = $this->getData(); } catch (RestException $e) { $this->handleError($e->getCode(), $e->getMessage()); } } list($obj, $method, $params, $this->params, $keys) = $this->findUrl(); if ($obj) { if (is_string($obj)) { if (class_exists($obj)) { $obj = new $obj(); } else { throw new Exception("Class {$obj} does not exist"); } } $obj->server = $this; try { if (method_exists($obj, 'init')) { $obj->init(); } if (empty($keys['noAuth'])) { if (method_exists($this, 'doServerWideAuthorization')) { if (!$this->doServerWideAuthorization()) { $this->unauthorized(false); } } elseif (method_exists($obj, 'authorize')) { // Standard behaviour if (!$obj->authorize()) { $this->unauthorized(false); } } } $accept_language_header = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : ''; $language = new Language($this->supported_languages, $this->default_language, $accept_language_header); $params = $this->injectLanguageIntoMethodParameters($language, $obj, $method, $params); $result = call_user_func_array(array($obj, $method), $params); $this->automaticContentLanguageHeaderDispatch($language); } catch (RestException $e) { $this->handleError($e->getCode(), $e->getMessage()); } if (!empty($result)) { $this->sendData($result); } } elseif (!isset($obj) && $this->getMethod() == HttpMethods::OPTIONS) { $this->handleCorsPreflightRequest(); } else { $this->handleError(HttpStatusCodes::NOT_FOUND); } }