protected function setCookieRequest($cookies) { $options = array('cookies' => $cookies); $response = Requests::get(httpbin('/cookies/set'), array(), $options); $data = json_decode($response->body, true); $this->assertInternalType('array', $data); $this->assertArrayHasKey('cookies', $data); return $data['cookies']; }
public function testSharedCookies() { $session = new Requests_Session(httpbin('/')); $options = array('follow_redirects' => false); $response = $session->get('/cookies/set?requests-testcookie=testvalue', array(), $options); $this->assertEquals(302, $response->status_code); // Check the cookies $response = $session->get('/cookies'); $this->assertTrue($response->success); // Check the response $data = json_decode($response->body, true); $this->assertNotNull($data); $this->assertArrayHasKey('cookies', $data); $cookies = array('requests-testcookie' => 'testvalue'); $this->assertEquals($cookies, $data['cookies']); }
/** * @dataProvider transportProvider */ public function testPOSTUsingInstantiation($transport) { if (!call_user_func(array($transport, 'test'))) { $this->markTestSkipped($transport . ' is not available'); return; } $options = array('auth' => new Requests_Auth_Basic(array('user', 'passwd')), 'transport' => $transport); $data = 'test'; $request = Requests::post(httpbin('/post'), array(), $data, $options); $this->assertEquals(200, $request->status_code); $result = json_decode($request->body); $auth = $result->headers->Authorization; $auth = explode(' ', $auth); $this->assertEquals(base64_encode('user:passwd'), $auth[1]); $this->assertEquals('test', $result->data); }
public function testMultipleToFile() { $requests = array('get' => array('url' => httpbin('/get'), 'options' => array('filename' => tempnam(sys_get_temp_dir(), 'RLT'))), 'post' => array('url' => httpbin('/post'), 'type' => Requests::POST, 'data' => 'test', 'options' => array('filename' => tempnam(sys_get_temp_dir(), 'RLT')))); $responses = Requests::request_multiple($requests, $this->getOptions()); // GET request $contents = file_get_contents($requests['get']['options']['filename']); $result = json_decode($contents, true); $this->assertEquals(httpbin('/get'), $result['url']); $this->assertEmpty($result['args']); unlink($requests['get']['options']['filename']); // POST request $contents = file_get_contents($requests['post']['options']['filename']); $result = json_decode($contents, true); $this->assertEquals(httpbin('/post'), $result['url']); $this->assertEquals('test', $result['data']); unlink($requests['post']['options']['filename']); }
public function testProgressCallback() { $mock = $this->getMockBuilder('stdClass')->setMethods(array('progress'))->getMock(); $mock->expects($this->atLeastOnce())->method('progress'); $hooks = new Requests_Hooks(); $hooks->register('request.progress', array($mock, 'progress')); $options = array('hooks' => $hooks); $options = $this->getOptions($options); $response = Requests::get(httpbin('/get'), array(), $options); }
/** * @dataProvider transportProvider */ public function testConnectWithInvalidAuth($transport) { $this->checkProxyAvailable('auth'); $options = array('proxy' => array(REQUESTS_HTTP_PROXY_AUTH, REQUESTS_HTTP_PROXY_AUTH_USER . '!', REQUESTS_HTTP_PROXY_AUTH_PASS . '!'), 'transport' => $transport); $response = Requests::get(httpbin('/get'), array(), $options); $this->assertEquals(407, $response->status_code); }
public function testBodyDataFormat() { $data = array('test' => 'true', 'test2' => 'test'); $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions(array('data_format' => 'body'))); $this->assertEquals(200, $request->status_code); $result = json_decode($request->body, true); $this->assertEquals(httpbin('/post'), $result['url']); $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']); }
/** * @expectedException Requests_Exception */ public function testTimeoutException() { $options = array('timeout' => 0.5); $response = Requests::get(httpbin('/delay/3'), array(), $options); }
/** * @depends testSendingCookie */ public function testCookieExpiration() { $options = array('follow_redirects' => true); $url = httpbin('/cookies/set/testcookie/testvalue'); $url .= '?expiry=1'; $response = Requests::get($url, array(), $options); $response->throw_for_status(); $data = json_decode($response->body, true); $this->assertEmpty($data['cookies']); }