public function testRequestFromStringFactoryCreatesValidRequest() { $string = "GET /foo HTTP/1.1\r\n\r\nSome Content"; $request = Request::fromString($string); $this->assertEquals(Request::METHOD_GET, $request->getMethod()); $this->assertEquals('/foo', $request->getUri()); $this->assertEquals(Request::VERSION_11, $request->getVersion()); $this->assertEquals('Some Content', $request->getContent()); }
/** * Transforms an HttpFoundation Request object into a Zend\Http\Request one. * * @param HttpFoundationRequest $request HttpFoundation Request * * @return ZendRequest */ public static function toZendRequest(HttpFoundationRequest $request = null) { if (null === $request) { $requestStr = HttpFoundationRequest::createFromGlobals()->__toString(); } else { $requestStr = $request->__toString(); } $requestStr = preg_replace('/\\:(\\s{2,}+)/', ': ', $requestStr); return ZendRequest::fromString($requestStr); }
public function testReceiveValidRequestTriggersEvents() { $services = $this->getApplicationServiceLocator(); $emitter = $services->get('NetglueSendgrid\\Service\\EventEmitter'); $request = HttpRequest::fromString(file_get_contents(__DIR__ . '/../../data/ValidSendGridRequest.txt')); $em = $emitter->getEventManager(); $sm = $em->getSharedManager(); $sm->attach('NetglueSendgrid\\Service\\EventEmitter', '*', [$this, 'checkValidEvents']); $this->assertSame(0, $this->eventsFired); $emitter->receiveRequest($request); $this->assertSame(11, $this->eventsFired); }
public function testAcceptEncodingHeaderWorksProperly() { $method = new \ReflectionMethod('\\Zend\\Http\\Client', 'prepareHeaders'); $method->setAccessible(true); $requestString = "GET http://www.domain.com/index.php HTTP/1.1\r\nHost: domain.com\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:16.0) Gecko/20100101 Firefox/16.0\r\nAccept: */*\r\nAccept-Language: en-US,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\n"; $request = Request::fromString($requestString); $adapter = new \Zend\Http\Client\Adapter\Test(); $client = new \Zend\Http\Client('http://www.domain.com/'); $client->setAdapter($adapter); $client->setRequest($request); $rawHeaders = "HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-Encoding: gzip, deflate\r\nContent-Type: application/javascript\r\nDate: Sun, 18 Nov 2012 16:16:08 GMT\r\nServer: nginx/1.1.19\r\nVary: Accept-Encoding\r\nX-Powered-By: PHP/5.3.10-1ubuntu3.4\r\nConnection: keep-alive\r\n"; $response = Response::fromString($rawHeaders); $client->getAdapter()->setResponse($response); $headers = $method->invoke($client, $requestString, $client->getUri()); $this->assertEquals('gzip, deflate', $headers['Accept-Encoding']); }
/** * @param ConnectionInterface $socket * @param $data * @param LoggerInterface $logger * @return WebSocketTransportFlash|WebSocketTransportHixie|WebSocketTransportHybi */ public static function fromSocketData(ConnectionInterface $socket, $data, LoggerInterface $logger) { // Check whether we have a Adobe Flash Policy file Request if (strpos($data, '<policy-file-request/>') === 0) { $s = new WebSocketTransportFlash($socket, $data); $s->setLogger($logger); return $s; } $request = Request::fromString($data); if ($request->getHeader('Sec-Websocket-Key1')) { $s = new WebSocketTransportHixie($socket, $request, $data); $s->setLogger($logger); } else { $s = new WebSocketTransportHybi($socket, $request); $s->setLogger($logger); } return $s; }
public function provideIdenticalGetRequestsInDifferentFormats() { $data = array(); $textRequest = $this->loadRequest('example.com'); $guzzleRequest = $this->getRequestFactory()->fromMessage($textRequest); $buzzRequest = new BuzzRequest($guzzleRequest->getMethod(), $guzzleRequest->getPath()); $buzzRequest->setHeaders($guzzleRequest->getHeaderLines()); $zendRequest = ZendRequest::fromString($textRequest); $requests = array($buzzRequest, $guzzleRequest, $textRequest, $zendRequest); foreach ($requests as $request1) { foreach ($requests as $request2) { // Don't compare a request with itself if ($request1 !== $request2) { continue; } $data[] = array($request1, $request2); } } return $data; }
public function establishConnection($data) { $this->_transport = WebSocketTransportFactory::fromSocketData($this, $data, $this->logger); $myself = $this; $this->_transport->on("handshake", function (Handshake $request) use($myself) { $myself->emit("handshake", array($request)); }); $this->_transport->on("connect", function () use($myself) { $myself->emit("connect", array($myself)); }); $this->_transport->on("message", function ($message) use($myself) { $myself->emit("message", array("message" => $message)); }); $this->_transport->on("flashXmlRequest", function ($message) use($myself) { $myself->emit("flashXmlRequest"); }); if ($this->_transport instanceof WebSocketTransportFlash) { return; } $request = Request::fromString($data); $this->_transport->respondTo($request); }
/** * @group ZF2-78 * @dataProvider parameterArrayProvider */ public function testContentTypeAdditionlInfo($params) { $content_type = 'application/x-www-form-urlencoded; charset=UTF-8'; $this->client->setUri($this->baseuri . 'testPostData.php'); $this->client->setHeaders(array('Content-Type' => $content_type)); $this->client->setMethod(\Zend\Http\Request::METHOD_POST); $this->client->setParameterPost($params); $this->client->send(); $request = Request::fromString($this->client->getLastRawRequest()); $this->assertEquals($content_type, $request->getHeaders()->get('Content-Type')->getFieldValue()); }
/** * @group 4893 */ public function testRequestsWithoutHttpVersionAreOK() { $requestString = "GET http://www.domain.com/index.php"; $request = Request::fromString($requestString); $this->assertEquals($request::METHOD_GET, $request->getMethod()); }
/** * Resume authentication process. * * This function resumes the authentication process after the user has * entered his or her credentials. * * @param array &$state The authentication state. */ public static function resume() { $request = Request::fromString($_SERVER['REQUEST_METHOD'] . ' ' . self::requesturi()); if (!($stateId = $request->getQuery('state'))) { throw new SimpleSAML_Error_BadRequest('Missing "state" parameter.'); } $state = SimpleSAML_Auth_State::loadState($stateId, 'openidconnect:Connect'); /* * Now we have the $state-array, and can use it to locate the authentication * source. */ $source = SimpleSAML_Auth_Source::getById($state['openidconnect:AuthID']); if ($source === NULL) { /* * The only way this should fail is if we remove or rename the authentication source * while the user is at the login page. */ throw new SimpleSAML_Error_Exception('Could not find authentication source.'); } /* * Make sure that we haven't switched the source type while the * user was at the authentication page. This can only happen if we * change config/authsources.php while an user is logging in. */ if (!$source instanceof self) { throw new SimpleSAML_Error_Exception('Authentication source type changed.'); } // The library has its own state manager but we're using SSP's. // We've already validated the state, so let's get the token. $tokenDispatcher = new Dispatcher(); $tokenRequest = new TokenRequest(); $clientInfo = new ClientInfo(); $clientInfo->fromArray(reset($source->getConfig())); $tokenRequest->setClientInfo($clientInfo); $tokenRequest->setCode($request->getQuery('code')); $tokenRequest->setGrantType('authorization_code'); $tokenDispatcher->setOptions(['http_options' => ['sslcapath' => $source->sslcapath]]); $tokenResponse = $tokenDispatcher->sendTokenRequest($tokenRequest); $userDispatcher = new InfoDispatcher(); $userDispatcher->setOptions(['http_options' => ['sslcapath' => $source->sslcapath]]); $infoRequest = new InfoRequest(); $infoRequest->setClientInfo($clientInfo); $infoRequest->setAccessToken($tokenResponse->getAccessToken()); try { $infoResponse = $userDispatcher->sendUserInfoRequest($infoRequest); $user = $infoResponse->getClaims(); } catch (Exception $e) { /* * The user isn't authenticated. * * Here we simply throw an exception, but we could also redirect the user back to the * login page. */ throw new SimpleSAML_Error_Exception('User not authenticated after login attempt.', $e->getCode(), $e); } /* * So, we have a valid user. Time to resume the authentication process where we * paused it in the authenticate()-function above. */ $state['Attributes'] = self::getAttributes($user); SimpleSAML_Auth_Source::completeAuth($state); /* * The completeAuth-function never returns, so we never get this far. */ assert('FALSE'); }
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()); }
public static function resume() { $request = Request::fromString($_SERVER['REQUEST_METHOD'] . ' ' . self::requesturi()); if (!($state_id = $request->getQuery('state'))) { throw new SimpleSAML_Error_BadRequest('Missing "state" parameter.'); } $state = SimpleSAML_Auth_State::loadState($state_id, 'dataportenoauth2:Connect'); /* * Now we have the $state-array, and can use it to locate the authentication * source. */ $source = SimpleSAML_Auth_Source::getById($state['dataportenoauth2:AuthID']); if ($source === NULL) { /* * The only way this should fail is if we remove or rename the authentication source * while the user is at the login page. */ throw new SimpleSAML_Error_Exception('Could not find authentication source.'); } if (!($code = $request->getQuery('code'))) { /** * Throwing error if no code is being sent. */ throw new SimpleSAML_Error_Exception('No code was sent from origin.'); } /* * Make sure that we haven't switched the source type while the * user was at the authentication page. This can only happen if we * change config/authsources.php while an user is logging in. */ if (!$source instanceof self) { throw new SimpleSAML_Error_Exception('Authentication source type changed.'); } $oauth_client = new OAuth2($source->getConfig()); $access_token = $oauth_client->get_access_token($state_id, $code); $identity = $oauth_client->get_identity($access_token, self::$user_endpoint); if (count($identity) < 1) { /** * The user isn't authenticated */ throw new SimpleSAML_Error_Exception('User not authenticated after login attempt.', $e->getCode(), $e); } $state['Attributes'] = self::getAttributes($identity); SimpleSAML_Auth_Source::completeAuth($state); /* * The completeAuth-function never returns, so we never get this far. */ assert('FALSE'); }