Skip to content

Commit

Permalink
tests/Transpot: add pool requests tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kis Attila Balint committed Jun 22, 2021
1 parent 857a140 commit 8994c31
Showing 1 changed file with 129 additions and 0 deletions.
129 changes: 129 additions & 0 deletions tests/Transport/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,36 @@ public function testTimeout() {
Requests::get(httpbin('/delay/10'), array(), $this->getOptions($options));
}

public function testPoolMultiple() {
$requests = array(
'test1' => array(
'url' => httpbin('/get'),
),
'test2' => array(
'url' => httpbin('/get'),
),
);
$responses = Requests::request_pool($requests, $this->getOptions());

// test1
$this->assertNotEmpty($responses['test1']);
$this->assertInstanceOf('Requests_Response', $responses['test1']);
$this->assertSame(200, $responses['test1']->status_code);

$result = json_decode($responses['test1']->body, true);
$this->assertSame(httpbin('/get'), $result['url']);
$this->assertEmpty($result['args']);

// test2
$this->assertNotEmpty($responses['test2']);
$this->assertInstanceOf('Requests_Response', $responses['test2']);
$this->assertSame(200, $responses['test2']->status_code);

$result = json_decode($responses['test2']->body, true);
$this->assertSame(httpbin('/get'), $result['url']);
$this->assertEmpty($result['args']);
}

public function testMultiple() {
$requests = array(
'test1' => array(
Expand Down Expand Up @@ -726,6 +756,28 @@ public function testMultipleWithDifferingMethods() {
$this->assertSame('test', $result['data']);
}

public function testPoolWithDifferingMethods() {
$requests = array(
'get' => array(
'url' => httpbin('/get'),
),
'post' => array(
'url' => httpbin('/post'),
'type' => Requests::POST,
'data' => 'test',
),
);
$responses = Requests::request_pool($requests, $this->getOptions());

// get
$this->assertSame(200, $responses['get']->status_code);

// post
$this->assertSame(200, $responses['post']->status_code);
$result = json_decode($responses['post']->body, true);
$this->assertSame('test', $result['data']);
}

/**
* @depends testTimeout
*/
Expand Down Expand Up @@ -767,6 +819,27 @@ public function testMultipleUsingCallback() {
$this->completed = array();
}

public function testPoolUsingCallback() {
$requests = array(
'get' => array(
'url' => httpbin('/get'),
),
'post' => array(
'url' => httpbin('/post'),
'type' => Requests::POST,
'data' => 'test',
),
);
$this->completed = array();
$options = array(
'complete' => array($this, 'completeCallback'),
);
$responses = Requests::request_pool($requests, $this->getOptions($options));

$this->assertSame($this->completed, $responses);
$this->completed = array();
}

public function testMultipleUsingCallbackAndFailure() {
$requests = array(
'success' => array(
Expand All @@ -789,6 +862,28 @@ public function testMultipleUsingCallbackAndFailure() {
$this->completed = array();
}

public function testPoolUsingCallbackAndFailure() {
$requests = array(
'success' => array(
'url' => httpbin('/get'),
),
'timeout' => array(
'url' => httpbin('/delay/10'),
'options' => array(
'timeout' => 1,
),
),
);
$this->completed = array();
$options = array(
'complete' => array($this, 'completeCallback'),
);
$responses = Requests::request_pool($requests, $this->getOptions($options));

$this->assertSame($this->completed, $responses);
$this->completed = array();
}

public function completeCallback($response, $key) {
$this->completed[$key] = $response;
}
Expand Down Expand Up @@ -827,6 +922,40 @@ public function testMultipleToFile() {
unlink($requests['post']['options']['filename']);
}

public function testPoolToFile() {
$requests = array(
'get' => array(
'url' => httpbin('/get'),
'options' => array(
'filename' => tempnam(sys_get_temp_dir(), 'RLT'), // RequestsLibraryTest
),
),
'post' => array(
'url' => httpbin('/post'),
'type' => Requests::POST,
'data' => 'test',
'options' => array(
'filename' => tempnam(sys_get_temp_dir(), 'RLT'), // RequestsLibraryTest
),
),
);
Requests::request_pool($requests, $this->getOptions());

// GET request
$contents = file_get_contents($requests['get']['options']['filename']);
$result = json_decode($contents, true);
$this->assertSame(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->assertSame(httpbin('/post'), $result['url']);
$this->assertSame('test', $result['data']);
unlink($requests['post']['options']['filename']);
}

public function testAlternatePort() {
try {
$request = Requests::get('http://portquiz.net:8080/', array(), $this->getOptions());
Expand Down

0 comments on commit 8994c31

Please sign in to comment.