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