Skip to content

Commit f7ee105

Browse files
author
Kis Attila Balint
committed
tests/Transpot: add pool requests tests
1 parent 857a140 commit f7ee105

1 file changed

Lines changed: 129 additions & 0 deletions

File tree

tests/Transport/cURL.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,135 @@ public function testExpiredHTTPS() {
1919
parent::testExpiredHTTPS();
2020
}
2121

22+
public function testPoolMultiple() {
23+
$requests = array(
24+
'test1' => array(
25+
'url' => httpbin('/get'),
26+
),
27+
'test2' => array(
28+
'url' => httpbin('/get'),
29+
),
30+
);
31+
$responses = Requests::request_pool($requests, $this->getOptions());
32+
33+
// test1
34+
$this->assertNotEmpty($responses['test1']);
35+
$this->assertInstanceOf('Requests_Response', $responses['test1']);
36+
$this->assertSame(200, $responses['test1']->status_code);
37+
38+
$result = json_decode($responses['test1']->body, true);
39+
$this->assertSame(httpbin('/get'), $result['url']);
40+
$this->assertEmpty($result['args']);
41+
42+
// test2
43+
$this->assertNotEmpty($responses['test2']);
44+
$this->assertInstanceOf('Requests_Response', $responses['test2']);
45+
$this->assertSame(200, $responses['test2']->status_code);
46+
47+
$result = json_decode($responses['test2']->body, true);
48+
$this->assertSame(httpbin('/get'), $result['url']);
49+
$this->assertEmpty($result['args']);
50+
}
51+
52+
public function testPoolWithDifferingMethods() {
53+
$requests = array(
54+
'get' => array(
55+
'url' => httpbin('/get'),
56+
),
57+
'post' => array(
58+
'url' => httpbin('/post'),
59+
'type' => Requests::POST,
60+
'data' => 'test',
61+
),
62+
);
63+
$responses = Requests::request_pool($requests, $this->getOptions());
64+
65+
// get
66+
$this->assertSame(200, $responses['get']->status_code);
67+
68+
// post
69+
$this->assertSame(200, $responses['post']->status_code);
70+
$result = json_decode($responses['post']->body, true);
71+
$this->assertSame('test', $result['data']);
72+
}
73+
74+
public function testPoolUsingCallbackAndFailure() {
75+
$requests = array(
76+
'success' => array(
77+
'url' => httpbin('/get'),
78+
),
79+
'timeout' => array(
80+
'url' => httpbin('/delay/10'),
81+
'options' => array(
82+
'timeout' => 1,
83+
),
84+
),
85+
);
86+
$this->completed = array();
87+
$options = array(
88+
'complete' => array($this, 'completeCallback'),
89+
);
90+
$responses = Requests::request_pool($requests, $this->getOptions($options));
91+
92+
$this->assertSame($this->completed, $responses);
93+
$this->completed = array();
94+
}
95+
96+
public function testPoolUsingCallback() {
97+
$requests = array(
98+
'get' => array(
99+
'url' => httpbin('/get'),
100+
),
101+
'post' => array(
102+
'url' => httpbin('/post'),
103+
'type' => Requests::POST,
104+
'data' => 'test',
105+
),
106+
);
107+
$this->completed = array();
108+
$options = array(
109+
'complete' => array($this, 'completeCallback'),
110+
);
111+
$responses = Requests::request_pool($requests, $this->getOptions($options));
112+
113+
$this->assertSame($this->completed, $responses);
114+
$this->completed = array();
115+
}
116+
117+
public function testPoolToFile() {
118+
$requests = array(
119+
'get' => array(
120+
'url' => httpbin('/get'),
121+
'options' => array(
122+
'filename' => tempnam(sys_get_temp_dir(), 'RLT'), // RequestsLibraryTest
123+
),
124+
),
125+
'post' => array(
126+
'url' => httpbin('/post'),
127+
'type' => Requests::POST,
128+
'data' => 'test',
129+
'options' => array(
130+
'filename' => tempnam(sys_get_temp_dir(), 'RLT'), // RequestsLibraryTest
131+
),
132+
),
133+
);
134+
Requests::request_pool($requests, $this->getOptions());
135+
136+
// GET request
137+
$contents = file_get_contents($requests['get']['options']['filename']);
138+
$result = json_decode($contents, true);
139+
$this->assertSame(httpbin('/get'), $result['url']);
140+
$this->assertEmpty($result['args']);
141+
unlink($requests['get']['options']['filename']);
142+
143+
// POST request
144+
$contents = file_get_contents($requests['post']['options']['filename']);
145+
$result = json_decode($contents, true);
146+
$this->assertSame(httpbin('/post'), $result['url']);
147+
$this->assertSame('test', $result['data']);
148+
unlink($requests['post']['options']['filename']);
149+
}
150+
22151
/**
23152
* @expectedException Requests_Exception
24153
* @expectedExceptionMessage certificate subject name

0 commit comments

Comments
 (0)