public function testConfigureExposurePullRequestWithExplicitExpose() { $routeMetaData = new RouteMetaData(); $explicit_expose = array('username', 'email'); $routeMetaData->setExpose($explicit_expose); $expose = ExposeFields::create($routeMetaData); $request = new Request(); $request->setPost('expose', 'username|address'); $expose->configurePullRequest(array(Configuration::EXPOSE_REQUEST_PARAM_POST => 'expose'), $request); $this->assertEquals($explicit_expose, $expose->toArray()); }
/** * Get the request object * @return Request $request */ public function getRequest() { if (!$this->request instanceof Request) { $this->request = Request::create(); } return $this->request; }
public function testGetPathWithAdditions() { $symRequest = \Symfony\Component\HttpFoundation\Request::create('/users?a=1#t12', 'GET'); $request = Request::create($symRequest); $this->assertEquals('/users', $request->getPath()); $symRequest = \Symfony\Component\HttpFoundation\Request::create('/users#t12', 'GET'); $request = Request::create($symRequest); $this->assertEquals('/users', $request->getPath()); }
/** * Does this request match the route pattern * @param Request $request * @param boolean $matchVerb - Whether you want to match the route using the request HTTP verb * - useful for OPTIONS requests to provide route info * @param string $basePath - add a base path to the route pattern * @return boolean $result */ public function matches(Request $request, $matchVerb = true, $basePath = null) { // If we're matching the verb and we've defined them, ensure the method used is in our list of registered verbs if ($matchVerb && $this->routeMetaData->usesHttpVerbs() && !$this->methodIsInOurListOfAllowedVerbs($request->getHttpMethod())) { return false; } $patternAsRegex = $this->getMatchRegexPattern($basePath); //Cache URL params' names and values if this route matches the current HTTP request if (!preg_match('#^' . $patternAsRegex . '$#', $request->getPath(), $paramValues)) { return false; } // Process the param names and save them on the route params $this->processRouteParams($paramValues); // Check the route conditions if (!$this->routeConditionsAreValid()) { return false; } return true; }
public function testIsExpectedContentFromParams() { $representation = Json::createFromString($this->getJsonString()); $symRequest = \Symfony\Component\HttpFoundation\Request::create('/users', 'GET', array('format' => 'json')); $request1 = Request::create($symRequest); $this->assertTrue($representation->isExpectedContent(array(3 => 'format'), $request1)); $symRequest = \Symfony\Component\HttpFoundation\Request::create('/users', 'GET'); $request2 = Request::create($symRequest); $this->assertFalse($representation->isExpectedContent(array(3 => 'format'), $request2)); }
/** * Uses configuration options to determine whether this writer instance is the media type expected by the client * @param array $configOptions - configuration options for content detection * @param Request $request - request object * @return boolean $result */ public final function isExpectedContent(array $configOptions, Request $request) { foreach ($configOptions as $detectContentOption => $detectContentValue) { switch ($detectContentOption) { case 1: // HTTP Header $headers = explode(',', $request->getHeaders($detectContentValue)); foreach ($headers as $headerEntry) { if (false !== ($pos = strpos($headerEntry, ';'))) { $headerEntry = substr($headerEntry, 0, $pos); } // See if the header matches for this writer if (in_array(trim($headerEntry), $this->getMatchableAcceptHeaders())) { return true; } } break; case 2: // Extension // See if an extension has been supplied $ext = $request->getExtension(); if (!empty($ext) && in_array($request->getExtension(), $this->getMatchableExtensions())) { return true; } break; case 3: // Parameter // Inspect the request object for a "format" parameter if (in_array($request->getQuery($detectContentValue), $this->getMatchableFormatParams())) { return true; } break; } } return false; }
/** * Determine the representation by inspecting the HTTP method * @param AbstractRepresentation $representation * @param array $detectContentOptions - Eg array(self::DETECT_CONTENT_HEADER => 'Accept') * @return AbstractRepresentation|null */ protected function determineRepresentationByHttpMethod(AbstractRepresentation $representation, array $detectContentOptions = []) { switch ($this->request->getHttpMethod()) { // Match on content option case Request::METHOD_GET: // This representation matches the required media type requested by the client if ($representation->isExpectedContent($detectContentOptions, $this->request)) { return $representation; } break; // Match on content-type // Match on content-type case Request::METHOD_POST: case Request::METHOD_PUT: case Request::METHOD_PATCH: if ($representation->getContentType() === $this->request->getHeaders('Content-Type')) { return $representation; } break; } return null; }
/** * Does this request matches the route pattern * @param Request $request * @param boolean $matchVerb - Whether you want to match the route using the request HTTP verb * - useful for OPTIONS requests * @param string $basePath - add a base path to the route pattern * @return boolean $result */ public function matches(Request $request, $matchVerb = true, $basePath = null) { if ($matchVerb && $this->usesHttpVerbs()) { try { $method = $request->getHttpMethod(); if (!in_array($method, $this->verbs)) { return false; } } catch (DrestException $e) { return false; } } //Convert URL params into regex patterns, construct a regex for this route, init params $routePattern = is_null($basePath) ? (string) $this->route_pattern : '/' . $basePath . '/' . ltrim((string) $this->route_pattern, '/'); $patternAsRegex = preg_replace_callback('#:([\\w]+)\\+?#', array($this, 'matchesCallback'), str_replace(')', ')?', $routePattern)); if (substr($this->route_pattern, -1) === '/') { $patternAsRegex .= '?'; } //Cache URL params' names and values if this route matches the current HTTP request if (!preg_match('#^' . $patternAsRegex . '$#', $request->getPath(), $paramValues)) { return false; } foreach ($this->param_names as $name) { if (isset($paramValues[$name])) { if (isset($this->param_names_path[$name])) { $parts = explode('/', urldecode($paramValues[$name])); $this->route_params[$name] = array_shift($parts); $this->unmapped_route_params = $parts; } else { $this->route_params[$name] = urldecode($paramValues[$name]); } } } // Check the route conditions foreach ($this->route_conditions as $key => $condition) { if (!preg_match('/^' . $condition . '$/', $this->route_params[$key])) { $this->param_names_path = $this->route_params = $this->unmapped_route_params = array(); return false; } } return true; }
/** * Configure the expose object to filter out fields that have been explicitly requested by the client. * This is only applicable for a HTTP pull (GET) call. For configuring * @param array $requestOptions * @param Request $request * @return ExposeFields $this object instance */ public function configurePullRequest(array $requestOptions, Request $request) { if (empty($this->route_expose)) { $exposeString = ''; foreach ($requestOptions as $requestOption => $requestValue) { switch ($requestOption) { case Configuration::EXPOSE_REQUEST_HEADER: $exposeString = $request->getHeaders($requestValue); break; case Configuration::EXPOSE_REQUEST_PARAM: $exposeString = $request->getParams($requestValue); break; case Configuration::EXPOSE_REQUEST_PARAM_GET: $exposeString = $request->getQuery($requestValue); break; case Configuration::EXPOSE_REQUEST_PARAM_POST: $exposeString = $request->getPost($requestValue); break; } } if (!empty($exposeString)) { $requestedExposure = $this->parseExposeString($exposeString); $this->filterRequestedExpose($requestedExposure, $this->fields); $this->fields = $requestedExposure; } } return $this; }
/** * Get the request object * @param $fwRequest - constructed using a fw adapted object * @return Request $request */ public function getRequest($fwRequest = null) { if (!$this->request instanceof Request) { $this->request = Request::create($fwRequest, $this->config->getRegisteredRequestAdapterClasses()); } return $this->request; }
/** * @expectedException \DrestCommon\Request\RequestException */ public function testUnknownHttpVerb() { $symRequest = \Symfony\Component\HttpFoundation\Request::create('/users', 'CUSTOM'); $request = Request::create($symRequest); $request->getHttpMethod(); }
public function testCanFetchBody() { $body = '<span>This is the body string</span>'; $httpString = "GET /foo HTTP/1.1\r\nAccept: */*\r\n\r\n{$body}"; $zfRequest = Http\Request::fromString($httpString); $request = Request::create($zfRequest, array('DrestCommon\\Request\\Adapter\\ZendFramework2')); $this->assertEquals($body, $request->getBody()); }