tests
[m6w6/ext-http] / phpunit / RequestTest.php
1 <?php
2
3 class ProgressObserver1 implements SplObserver {
4 function update(SplSubject $r) {
5 if ($r->getProgress()) $r->pi .= "-";
6 }
7 }
8 class ProgressObserver2 implements SplObserver {
9 function update(SplSubject $r) {
10 if ($r->getProgress()) $r->pi .= ".";
11 }
12 }
13 class CallbackObserver implements SplObserver {
14 public $callback;
15 function __construct($callback) {
16 $this->callback = $callback;
17 }
18 function update(SplSubject $r) {
19 call_user_func($this->callback, $r);
20 }
21 }
22
23 class RequestTest extends PHPUnit_Framework_TestCase
24 {
25 /**
26 * @var http\Request
27 */
28 protected $r;
29
30 function setUp() {
31 $this->r = (new http\Request\Factory)->createRequest();
32 $this->r->setOptions(
33 array(
34 "connecttimeout" => 30,
35 "timeout" => 300,
36 )
37 );
38 }
39
40 function testClone() {
41 $this->r->setUrl("http://dev.iworks.at/ext-http/.print_request.php");
42 $c = clone $this->r;
43 $c->setUrl("http://dev.iworks.at/ext-http/.print_headers.php");
44 $this->assertNotEquals($this->r->send(), $c->send());
45 }
46
47 function testObserver() {
48 $this->r->attach(new ProgressObserver1);
49 $this->r->attach(new ProgressObserver2);
50 $this->r->attach(
51 new CallbackObserver(
52 function ($r) {
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);
61 }
62 )
63 );
64 $this->r->setUrl("http://dev.iworks.at/ext-http/")->send();
65 $this->assertRegexp("/(\.-)+/", $this->r->pi);
66 $this->assertCount(3, $this->r->getObservers());
67 }
68
69 function testCookies() {
70 $this->r->setUrl("http://dev.iworks.at/ext-http/.cookie.php")->send();
71 $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
72 $this->r->send();
73 $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
74 $this->r->enableCookies()->send();
75 $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
76 $this->r->send();
77 $this->assertContains("Cookie", (string) $this->r->getRequestMessage());
78 $this->assertCount(2, $this->r->getResponseCookies());
79 }
80
81 function testResetCookies() {
82 $this->r->setUrl("http://dev.iworks.at/ext-http/.cookie.php");
83
84 $this->r->enableCookies();
85 $this->r->send();
86
87 $f = function ($a) { return $a->getCookies(); };
88 $c = array_map($f, $this->r->getResponseCookies());
89
90 $this->r->send();
91 $this->assertEquals($c, array_map($f, $this->r->getResponseCookies()));
92
93 $this->r->resetCookies();
94 $this->r->send();
95 $this->assertNotEquals($c, array_map($f, $this->r->getResponseCookies()));
96 }
97
98 function testHistory() {
99 $body = new http\Message\Body;
100 $body->append("foobar");
101 $this->r->setBody($body);
102
103 $this->r->recordHistory = true;
104
105 $this->r->setMethod(http\Request\Method::POST);
106 $this->r->setUrl("http://dev.iworks.at/ext-http/.print_request.php");
107
108 $this->r->send();
109 $this->assertStringMatchesFormat(<<<HTTP
110 POST /ext-http/.print_request.php HTTP/1.1
111 User-Agent: %s
112 Host: dev.iworks.at
113 Accept: */*
114 Content-Length: 6
115 Content-Type: application/x-www-form-urlencoded
116 X-Original-Content-Length: 6
117
118 foobar
119 HTTP/1.1 200 OK
120 Date: %s
121 Server: %s
122 Vary: %s
123 Content-Length: 19
124 Content-Type: text/html
125 X-Original-Content-Length: 19
126
127 string(6) "foobar"
128
129 HTTP
130 , str_replace("\r", "", $this->r->getHistory()->toString(true))
131 );
132
133 $this->r->send();
134 $this->assertStringMatchesFormat(<<<HTTP
135 POST /ext-http/.print_request.php HTTP/1.1
136 User-Agent: %s
137 Host: dev.iworks.at
138 Accept: */*
139 Content-Length: 6
140 Content-Type: application/x-www-form-urlencoded
141 X-Original-Content-Length: 6
142
143 foobar
144 HTTP/1.1 200 OK
145 Date: %s
146 Server: %s
147 Vary: %s
148 Content-Length: 19
149 Content-Type: text/html
150 X-Original-Content-Length: 19
151
152 string(6) "foobar"
153
154 POST /ext-http/.print_request.php HTTP/1.1
155 User-Agent: %s
156 Host: dev.iworks.at
157 Accept: */*
158 Content-Length: 6
159 Content-Type: application/x-www-form-urlencoded
160 X-Original-Content-Length: 6
161
162 foobar
163 HTTP/1.1 200 OK
164 Date: %s
165 Server: %s
166 Vary: %s
167 Content-Length: 19
168 Content-Type: text/html
169 X-Original-Content-Length: 19
170
171 string(6) "foobar"
172
173 HTTP
174 , str_replace("\r", "", $this->r->getHistory()->toString(true))
175 );
176 }
177 }
178