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\Request\Factory
)->createRequest();
34 "connecttimeout" => 30,
40 function testClone() {
41 $this->r
->setUrl("http://dev.iworks.at/ext-http/.print_request.php");
43 $c->setUrl("http://dev.iworks.at/ext-http/.print_headers.php");
44 $this->assertNotEquals($this->r
->send(), $c->send());
47 function testObserver() {
48 $this->r
->attach(new ProgressObserver1
);
49 $this->r
->attach(new ProgressObserver2
);
53 $p = (array) $r->getProgress();
54 $this->assertArrayHasKey("started", $p);
55 $this->assertArrayHasKey("finished", $p);
56 $this->assertArrayHasKey("dlnow", $p);
57 $this->assertArrayHasKey("ulnow", $p);
58 $this->assertArrayHasKey("dltotal", $p);
59 $this->assertArrayHasKey("ultotal", $p);
60 $this->assertArrayHasKey("info", $p);
64 $this->r
->setUrl("http://dev.iworks.at/ext-http/")->send();
65 $this->assertRegexp("/(\.-)+/", $this->r
->pi
);
66 $this->assertCount(3, $this->r
->getObservers());
69 function testCookies() {
70 $this->r
->setUrl("http://dev.iworks.at/ext-http/.cookie.php")->send();
71 $this->assertNotContains("Cookie", (string) $this->r
->getRequestMessage());
73 $this->assertNotContains("Cookie", (string) $this->r
->getRequestMessage());
74 $this->r
->enableCookies()->send();
75 $this->assertNotContains("Cookie", (string) $this->r
->getRequestMessage());
77 $this->assertContains("Cookie", (string) $this->r
->getRequestMessage());
78 $this->assertCount(2, $this->r
->getResponseCookies());
81 function testResetCookies() {
82 $this->r
->setUrl("http://dev.iworks.at/ext-http/.cookie.php");
84 $this->r
->enableCookies();
87 $f = function ($a) { return $a->getCookies(); };
88 $c = array_map($f, $this->r
->getResponseCookies());
91 $this->assertEquals($c, array_map($f, $this->r
->getResponseCookies()));
93 $this->r
->resetCookies();
95 $this->assertNotEquals($c, array_map($f, $this->r
->getResponseCookies()));
98 function testHistory() {
99 $body = new http\Message\Body
;
100 $body->append("foobar");
101 $this->r
->setBody($body);
103 $this->r
->recordHistory
= true;
105 $this->r
->setMethod(http\Request\Method
::POST
);
106 $this->r
->setUrl("http://dev.iworks.at/ext-http/.print_request.php");
109 $this->assertStringMatchesFormat(<<<HTTP
110 POST /ext-http/.print_request.php HTTP/1.1
115 Content
-Type
: application
/x
-www
-form
-urlencoded
116 X
-Original
-Content
-Length
: 6
124 Content
-Type
: text
/html
125 X
-Original
-Content
-Length
: 19
130 , str_replace("\r", "", $this->r
->getHistory()->toString(true))
134 $this->assertStringMatchesFormat(<<<HTTP
135 POST /ext-http/.print_request.php HTTP/1.1
140 Content
-Type
: application
/x
-www
-form
-urlencoded
141 X
-Original
-Content
-Length
: 6
149 Content
-Type
: text
/html
150 X
-Original
-Content
-Length
: 19
154 POST
/ext
-http
/.print_request
.php HTTP
/1.1
159 Content
-Type
: application
/x
-www
-form
-urlencoded
160 X
-Original
-Content
-Length
: 6
168 Content
-Type
: text
/html
169 X
-Original
-Content
-Length
: 19
174 , str_replace("\r", "", $this->r
->getHistory()->toString(true))