codename: client meltdown
[m6w6/ext-http] / phpunit / ClientTest.php
1 <?php
2
3 class ProgressObserver1 implements SplObserver {
4 function update(SplSubject $c, $r = null) {
5 if ($c->getProgressInfo($r)) $c->pi .= "-";
6 }
7 }
8 class ProgressObserver2 implements SplObserver {
9 function update(SplSubject $c, $r = null) {
10 if ($c->getProgressInfo($r)) $c->pi .= ".";
11 }
12 }
13 class CallbackObserver implements SplObserver {
14 public $callback;
15 function __construct($callback) {
16 $this->callback = $callback;
17 }
18 function update(SplSubject $c, $r = null) {
19 call_user_func($this->callback, $c, $r);
20 }
21 }
22
23 class RequestTest extends PHPUnit_Framework_TestCase
24 {
25 /**
26 * @var http\Client
27 */
28 protected $r;
29
30 function setUp() {
31 $this->r = new http\Client;
32 $this->r->pi = "";
33 $this->r->setOptions(
34 array(
35 "connecttimeout" => 30,
36 "timeout" => 300,
37 )
38 );
39 $this->r->setOptions(
40 array(
41 "timeout" => 600
42 )
43 );
44 $this->assertEquals(
45 array(
46 "connecttimeout" => 30,
47 "timeout" => 600,
48 ),
49 $this->r->getOptions()
50 );
51 }
52
53 function testObserver() {
54 $test = $this;
55 $this->r->attach($o1 = new ProgressObserver1);
56 $this->r->attach($o2 = new ProgressObserver2);
57 $this->r->attach(
58 $o3 = new CallbackObserver(
59 function ($c, $r) use ($test) {
60 $p = (array) $c->getProgressInfo($r);
61 $test->assertArrayHasKey("started", $p);
62 $test->assertArrayHasKey("finished", $p);
63 $test->assertArrayHasKey("dlnow", $p);
64 $test->assertArrayHasKey("ulnow", $p);
65 $test->assertArrayHasKey("dltotal", $p);
66 $test->assertArrayHasKey("ultotal", $p);
67 $test->assertArrayHasKey("info", $p);
68 }
69 )
70 );
71 $this->r->enqueue(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/"))->send();
72 $this->assertRegexp("/(\.-)+/", $this->r->pi);
73 $this->assertCount(3, $this->r->getObservers());
74 $this->r->detach($o1);
75 $this->assertCount(2, $this->r->getObservers());
76 $this->r->detach($o2);
77 $this->assertCount(1, $this->r->getObservers());
78 $this->r->detach($o3);
79 $this->assertCount(0, $this->r->getObservers());
80 }
81
82 function testSsl() {
83 $this->r->setSslOptions(array("verify_peer" => true));
84 $this->r->addSslOptions(array("verify_host" => 2));
85 $this->assertEquals(
86 array(
87 "verify_peer" => true,
88 "verify_host" => 2,
89 ),
90 $this->r->getSslOptions()
91 );
92 $this->r->enqueue($req = new http\Client\Request("GET", "https://twitter.com/"));
93 $this->r->send();
94 $ti = $this->r->getTransferInfo($req);
95 $this->assertArrayHasKey("ssl_engines", $ti);
96 $this->assertGreaterThan(0, count($ti["ssl_engines"]));
97 }
98
99 function testHistory() {
100 $body = new http\Message\Body;
101 $body->append("foobar");
102
103 $request = new http\Client\Request;
104 $request->setBody($body);
105 $request->setRequestMethod("POST");
106 $request->setRequestUrl("http://dev.iworks.at/ext-http/.print_request.php");
107
108 $this->r->recordHistory = true;
109 $this->r->enqueue($request)->send();
110
111 $this->assertStringMatchesFormat(<<<HTTP
112 POST /ext-http/.print_request.php HTTP/1.1
113 User-Agent: %s
114 Host: dev.iworks.at
115 Accept: */*
116 Content-Length: 6
117 Content-Type: application/x-www-form-urlencoded
118 X-Original-Content-Length: 6
119
120 foobar
121 HTTP/1.1 200 OK
122 Date: %s
123 Server: %s
124 Vary: %s
125 Content-Length: 19
126 Content-Type: text/html
127 X-Original-Content-Length: 19
128
129 string(6) "foobar"
130
131 HTTP
132 , str_replace("\r", "", $this->r->getHistory()->toString(true))
133 );
134
135
136 $this->r->requeue($request)->send();
137
138 $this->assertStringMatchesFormat(<<<HTTP
139 POST /ext-http/.print_request.php HTTP/1.1
140 User-Agent: %s
141 Host: dev.iworks.at
142 Accept: */*
143 Content-Length: 6
144 Content-Type: application/x-www-form-urlencoded
145 X-Original-Content-Length: 6
146
147 foobar
148 HTTP/1.1 200 OK
149 Date: %s
150 Server: %s
151 Vary: %s
152 Content-Length: 19
153 Content-Type: text/html
154 X-Original-Content-Length: 19
155
156 string(6) "foobar"
157
158 POST /ext-http/.print_request.php HTTP/1.1
159 User-Agent: %s
160 Host: dev.iworks.at
161 Accept: */*
162 Content-Length: 6
163 Content-Type: application/x-www-form-urlencoded
164 X-Original-Content-Length: 6
165
166 foobar
167 HTTP/1.1 200 OK
168 Date: %s
169 Server: %s
170 Vary: %s
171 Content-Length: 19
172 Content-Type: text/html
173 X-Original-Content-Length: 19
174
175 string(6) "foobar"
176
177 HTTP
178 , str_replace("\r", "", $this->r->getHistory()->toString(true))
179 );
180 }
181 }
182