/** * Test asynchronous request through socketPerform() and socketSelect() * * @return void */ public function testRequestAsynchronous() { $test = $this; $req = new cURL\Request(); $req->getOptions()->set(CURLOPT_URL, $this->okTestUrl)->set(CURLOPT_RETURNTRANSFER, true); $req->addListener('complete', function ($event) use($test) { $test->validateSuccesfulResponse('/', $event->response); }); $n = 0; while ($req->socketPerform()) { $n++; $req->socketSelect(); } try { $req->socketPerform(); } catch (cURL\Exception $e) { } $this->assertInstanceOf('cURL\\Exception', $e); $this->assertGreaterThan(0, $n); $req = new cURL\Request(); $req->getOptions()->set(CURLOPT_URL, $this->errorTestUrl)->set(CURLOPT_TIMEOUT, 1)->set(CURLOPT_RETURNTRANSFER, true); $req->addListener('complete', function ($event) use($test) { $test->validateTimeoutedResponse($event->response); }); while ($req->socketPerform()) { $req->socketSelect(); } }
/** * @param array $matches * @param ChannelContext $context */ public function processMessage(array $matches, ChannelContext $context) { $uri = $matches[1]; $req = new cURL\Request('http://ws.spotify.com/lookup/1/.json?uri=spotify:track:' . $uri); $req->addListener('complete', function (cURL\Event $event) use($context) { $this->onRequestComplete($event, $context); }); $this->sendRequest($req); }
/** * @param array $matches * @param ChannelContext $context */ public function processMessage(array $matches, ChannelContext $context) { $videoId = $matches[1]; if ($item = $this->getCacheDriver()->fetch($videoId)) { $context->getLogger()->debug("Using cached entry for YouTube video {$videoId}."); $this->generateResponse($item, $context); return; } $query = http_build_query(['id' => $videoId, 'part' => 'snippet,contentDetails,statistics', 'key' => $this->apiKey]); $req = new cURL\Request('https://www.googleapis.com/youtube/v3/videos?' . $query); $req->addListener('complete', function (cURL\Event $event) use($videoId, $context) { $this->onRequestComplete($event, $videoId, $context); }); $this->sendRequest($req); }
/** * Tests whether 'complete' event on individual Request has not been fired * when Request::send() was used. */ public function testRequestCompleteEventSynchronous() { $eventFired = 0; $req = new cURL\Request(); $req->getOptions()->set(CURLOPT_URL, $this->createRequestUrl())->set(CURLOPT_RETURNTRANSFER, true); $req->addListener('complete', function (cURL\Event $event) use(&$eventFired) { $eventFired++; }); $req->send(); $this->assertEquals(0, $eventFired); }
/** * Requests which fail very quickly might cause infinite loop and return no response. * http://curl.haxx.se/libcurl/c/curl_multi_perform.html * "If an added handle fails very quickly, it may never be counted as a running_handle." * This test ensures that it won't loop and will return properly error code. */ public function testErrorCode() { $request = new cURL\Request(''); $request->addListener('complete', function (cURL\Event $e) { $this->assertEquals('<url> malformed', $e->response->getError()->getMessage()); }); $queue = new cURL\RequestsQueue(); $queue->attach($request); $queue->send(); }
/** * Tests whether 'complete' event on individual Request has been fired * when using RequestsQueue */ public function testRequestCompleteEvent() { $eventFired = 0; $req = new cURL\Request(); $req->getOptions()->set(CURLOPT_URL, $this->createRequestUrl())->set(CURLOPT_RETURNTRANSFER, true); $req->addListener('complete', function ($event) use(&$eventFired) { $this->validateSuccesfulResponse($event->response); $eventFired++; }); $queue = new cURL\RequestsQueue(); $queue->attach($req); $queue->send(); $this->assertEquals(1, $eventFired); }