release 2.6.0
[m6w6/ext-http] / tests / client029.phpt
1 --TEST--
2 client curl user handler
3 --SKIPIF--
4 <?php
5 include "skipif.inc";
6 skip_client_test();
7 _ext("ev");
8
9 ?>
10 --FILE--
11 <?php
12 echo "Test\n";
13
14 class UserHandler implements http\Client\Curl\User
15 {
16 private $client;
17 private $run;
18 private $ios = [];
19 private $timeout;
20
21
22 function __construct(http\Client $client) {
23 $this->client = $client;
24 }
25
26 function init($run) {
27 $this->run = $run;
28 }
29
30 function timer($timeout_ms) {
31 echo "T";
32 if (isset($this->timeout)) {
33 $this->timeout->set($timeout_ms/1000, 0);
34 $this->timeout->start();
35 } else {
36 $this->timeout = new EvTimer($timeout_ms/1000, 0, function() {
37 if (!call_user_func($this->run, $this->client)) {
38 if ($this->timeout) {
39 $this->timeout->stop();
40 $this->timeout = null;
41 }
42 }
43 });
44 }
45 }
46
47 function socket($socket, $action) {
48 echo "S";
49
50 switch ($action) {
51 case self::POLL_NONE:
52 break;
53 case self::POLL_REMOVE:
54 if (isset($this->ios[(int) $socket])) {
55 echo "U";
56 $this->ios[(int) $socket]->stop();
57 unset($this->ios[(int) $socket]);
58 }
59 break;
60
61 default:
62 $ev = 0;
63 if ($action & self::POLL_IN) {
64 $ev |= Ev::READ;
65 }
66 if ($action & self::POLL_OUT) {
67 $ev |= Ev::WRITE;
68 }
69 if (isset($this->ios[(int) $socket])) {
70 $this->ios[(int) $socket]->set($socket, $ev);
71 } else {
72 $this->ios[(int) $socket] = new EvIo($socket, $ev, function($watcher, $events) use($socket) {
73 $action = 0;
74 if ($events & Ev::READ) {
75 $action |= self::POLL_IN;
76 }
77 if ($events & Ev::WRITE) {
78 $action |= self::POLL_OUT;
79 }
80 if (!call_user_func($this->run, $this->client, $socket, $action)) {
81 if ($this->timeout) {
82 $this->timeout->stop();
83 $this->timeout = null;
84 }
85 }
86 });
87 }
88 break;
89 }
90 }
91
92 function once() {
93 throw new BadMethodCallException("this test uses Ev::run()");
94 }
95
96 function wait($timeout_ms = null) {
97 throw new BadMethodCallException("this test uses Ev::run()");
98 }
99
100 function send() {
101 throw new BadMethodCallException("this test uses Ev::run()");
102 }
103 }
104
105
106 include "helper/server.inc";
107
108 server("proxy.inc", function($port) {
109 $client = new http\Client;
110 $client->configure([
111 "use_eventloop" => new UserHandler($client)
112 ]);
113 $client->enqueue(new http\Client\Request("GET", "http://localhost:$port/"), function($r) {
114 var_dump($r->getResponseCode());
115 });
116 Ev::run();
117 });
118
119 ?>
120 ===DONE===
121 --EXPECTREGEX--
122 Test
123 T[ST]+U+int\(200\)
124 ===DONE===