3 class ProgressObserver1
implements SplObserver
{
4 function update(SplSubject
$r) {
5 if ($r->getProgress()) $r->pi
.= "-";
8 class ProgressObserver2
implements SplObserver
{
9 function update(SplSubject
$r) {
10 if ($r->getProgress()) $r->pi
.= ".";
13 class CallbackObserver
implements SplObserver
{
15 function __construct($callback) {
16 $this->callback
= $callback;
18 function update(SplSubject
$r) {
19 call_user_func($this->callback
, $r);
23 class RequestTest
extends PHPUnit_Framework_TestCase
31 $this->r
= (new http\Client\Factory
)->createClient();
34 "connecttimeout" => 30,
40 function testClone() {
42 $this->assertNotSame($this->r
, $c);
45 function testObserver() {
46 $this->r
->attach(new ProgressObserver1
);
47 $this->r
->attach(new ProgressObserver2
);
51 $p = (array) $r->getProgress();
52 $this->assertArrayHasKey("started", $p);
53 $this->assertArrayHasKey("finished", $p);
54 $this->assertArrayHasKey("dlnow", $p);
55 $this->assertArrayHasKey("ulnow", $p);
56 $this->assertArrayHasKey("dltotal", $p);
57 $this->assertArrayHasKey("ultotal", $p);
58 $this->assertArrayHasKey("info", $p);
62 $this->r
->setRequest(new http\Client\
Request("GET", "http://dev.iworks.at/ext-http/"))->send(null);
63 $this->assertRegexp("/(\.-)+/", $this->r
->pi
);
64 $this->assertCount(3, $this->r
->getObservers());
67 function testCookies() {
68 $this->r
->setRequest(new http\Client\
Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"))->send(null);
69 $this->assertNotContains("Cookie", (string) $this->r
->getRequestMessage());
71 $this->assertNotContains("Cookie", (string) $this->r
->getRequestMessage());
72 $this->r
->enableCookies()->send(null);
73 $this->assertNotContains("Cookie", (string) $this->r
->getRequestMessage());
75 $this->assertContains("Cookie", (string) $this->r
->getRequestMessage());
76 $this->assertCount(2, $this->r
->getResponseMessage()->getCookies());
79 function testResetCookies() {
80 $this->r
->setRequest(new http\Client\
Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"));
82 $this->r
->enableCookies();
85 $f = function ($a) { return $a->getCookies(); };
86 $c = array_map($f, $this->r
->getResponseMessage()->getCookies());
89 $this->assertEquals($c, array_map($f, $this->r
->getResponseMessage()->getCookies()));
91 $this->r
->resetCookies();
93 $this->assertNotEquals($c, array_map($f, $this->r
->getResponseMessage()->getCookies()));
96 function testHistory() {
97 $body = new http\Message\Body
;
98 $body->append("foobar");
100 $request = new http\Client\Request
;
101 $request->setBody($body);
102 $request->setRequestMethod("POST");
103 $request->setRequestUrl("http://dev.iworks.at/ext-http/.print_request.php");
105 $this->r
->recordHistory
= true;
106 $this->r
->send($request);
108 $this->assertStringMatchesFormat(<<<HTTP
109 POST /ext-http/.print_request.php HTTP/1.1
114 Content
-Type
: application
/x
-www
-form
-urlencoded
115 X
-Original
-Content
-Length
: 6
123 Content
-Type
: text
/html
124 X
-Original
-Content
-Length
: 19
129 , str_replace("\r", "", $this->r
->getHistory()->toString(true))
133 $this->r
->send($request);
135 $this->assertStringMatchesFormat(<<<HTTP
136 POST /ext-http/.print_request.php HTTP/1.1
141 Content
-Type
: application
/x
-www
-form
-urlencoded
142 X
-Original
-Content
-Length
: 6
150 Content
-Type
: text
/html
151 X
-Original
-Content
-Length
: 19
155 POST
/ext
-http
/.print_request
.php HTTP
/1.1
160 Content
-Type
: application
/x
-www
-form
-urlencoded
161 X
-Original
-Content
-Length
: 6
169 Content
-Type
: text
/html
170 X
-Original
-Content
-Length
: 19
175 , str_replace("\r", "", $this->r
->getHistory()->toString(true))