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