release dev10
[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\Client\Factory)->createClient();
32 $this->r->setOptions(
33 array(
34 "connecttimeout" => 30,
35 "timeout" => 300,
36 )
37 );
38 }
39
40 function testClone() {
41 $c = clone $this->r;
42 $this->assertNotSame($this->r, $c);
43 }
44
45 function testObserver() {
46 $this->r->attach(new ProgressObserver1);
47 $this->r->attach(new ProgressObserver2);
48 $this->r->attach(
49 new CallbackObserver(
50 function ($r) {
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);
59 }
60 )
61 );
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());
65 }
66
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());
70 $this->r->send(null);
71 $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
72 $this->r->enableCookies()->send(null);
73 $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
74 $this->r->send(null);
75 $this->assertContains("Cookie", (string) $this->r->getRequestMessage());
76 $this->assertCount(2, $this->r->getResponseMessage()->getCookies());
77 }
78
79 function testResetCookies() {
80 $this->r->setRequest(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"));
81
82 $this->r->enableCookies();
83 $this->r->send(null);
84
85 $f = function ($a) { return $a->getCookies(); };
86 $c = array_map($f, $this->r->getResponseMessage()->getCookies());
87
88 $this->r->send(null);
89 $this->assertEquals($c, array_map($f, $this->r->getResponseMessage()->getCookies()));
90
91 $this->r->resetCookies();
92 $this->r->send(null);
93 $this->assertNotEquals($c, array_map($f, $this->r->getResponseMessage()->getCookies()));
94 }
95
96 function testHistory() {
97 $body = new http\Message\Body;
98 $body->append("foobar");
99
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");
104
105 $this->r->recordHistory = true;
106 $this->r->send($request);
107
108 $this->assertStringMatchesFormat(<<<HTTP
109 POST /ext-http/.print_request.php HTTP/1.1
110 User-Agent: %s
111 Host: dev.iworks.at
112 Accept: */*
113 Content-Length: 6
114 Content-Type: application/x-www-form-urlencoded
115 X-Original-Content-Length: 6
116
117 foobar
118 HTTP/1.1 200 OK
119 Date: %s
120 Server: %s
121 Vary: %s
122 Content-Length: 19
123 Content-Type: text/html
124 X-Original-Content-Length: 19
125
126 string(6) "foobar"
127
128 HTTP
129 , str_replace("\r", "", $this->r->getHistory()->toString(true))
130 );
131
132
133 $this->r->send($request);
134
135 $this->assertStringMatchesFormat(<<<HTTP
136 POST /ext-http/.print_request.php HTTP/1.1
137 User-Agent: %s
138 Host: dev.iworks.at
139 Accept: */*
140 Content-Length: 6
141 Content-Type: application/x-www-form-urlencoded
142 X-Original-Content-Length: 6
143
144 foobar
145 HTTP/1.1 200 OK
146 Date: %s
147 Server: %s
148 Vary: %s
149 Content-Length: 19
150 Content-Type: text/html
151 X-Original-Content-Length: 19
152
153 string(6) "foobar"
154
155 POST /ext-http/.print_request.php HTTP/1.1
156 User-Agent: %s
157 Host: dev.iworks.at
158 Accept: */*
159 Content-Length: 6
160 Content-Type: application/x-www-form-urlencoded
161 X-Original-Content-Length: 6
162
163 foobar
164 HTTP/1.1 200 OK
165 Date: %s
166 Server: %s
167 Vary: %s
168 Content-Length: 19
169 Content-Type: text/html
170 X-Original-Content-Length: 19
171
172 string(6) "foobar"
173
174 HTTP
175 , str_replace("\r", "", $this->r->getHistory()->toString(true))
176 );
177 }
178 }
179