4 * Copyright 2018 gRPC authors.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 * Interface exported by the server.
22 require_once(dirname(__FILE__).'/../../lib/Grpc/BaseStub.php');
23 require_once(dirname(__FILE__).'/../../lib/Grpc/AbstractCall.php');
24 require_once(dirname(__FILE__).'/../../lib/Grpc/UnaryCall.php');
25 require_once(dirname(__FILE__).'/../../lib/Grpc/ClientStreamingCall.php');
26 require_once(dirname(__FILE__).'/../../lib/Grpc/Interceptor.php');
27 require_once(dirname(__FILE__).'/../../lib/Grpc/CallInvoker.php');
28 require_once(dirname(__FILE__).'/../../lib/Grpc/Internal/InterceptorChannel.php');
33 public function __construct($data)
37 public function setData($data)
41 public function serializeToString()
47 class InterceptorClient extends Grpc\BaseStub
51 * @param string $hostname hostname
52 * @param array $opts channel options
53 * @param Channel|InterceptorChannel $channel (optional) re-use channel object
55 public function __construct($hostname, $opts, $channel = null)
57 parent::__construct($hostname, $opts, $channel);
62 * @param SimpleRequest $argument input argument
63 * @param array $metadata metadata
64 * @param array $options call options
66 public function UnaryCall(
67 SimpleRequest $argument,
71 return $this->_simpleRequest(
81 * A client-to-server streaming RPC.
82 * @param array $metadata metadata
83 * @param array $options call options
85 public function StreamCall(
89 return $this->_clientStreamRequest('/dummy_method', [], $metadata, $options);
94 class ChangeMetadataInterceptor extends Grpc\Interceptor
96 public function interceptUnaryUnary($method,
100 array $metadata = [],
103 $metadata["foo"] = array('interceptor_from_unary_request');
104 return $continuation($method, $argument, $deserialize, $metadata, $options);
106 public function interceptStreamUnary($method,
109 array $metadata = [],
112 $metadata["foo"] = array('interceptor_from_stream_request');
113 return $continuation($method, $deserialize, $metadata, $options);
117 class ChangeMetadataInterceptor2 extends Grpc\Interceptor
119 public function interceptUnaryUnary($method,
123 array $metadata = [],
126 if (array_key_exists('foo', $metadata)) {
127 $metadata['bar'] = array('ChangeMetadataInterceptor should be executed first');
129 $metadata["bar"] = array('interceptor_from_unary_request');
131 return $continuation($method, $argument, $deserialize, $metadata, $options);
133 public function interceptStreamUnary($method,
136 array $metadata = [],
139 if (array_key_exists('foo', $metadata)) {
140 $metadata['bar'] = array('ChangeMetadataInterceptor should be executed first');
142 $metadata["bar"] = array('interceptor_from_stream_request');
144 return $continuation($method, $deserialize, $metadata, $options);
148 class ChangeRequestCall
152 public function __construct($call)
156 public function getCall()
161 public function write($request)
163 $request->setData('intercepted_stream_request');
164 $this->getCall()->write($request);
167 public function wait()
169 return $this->getCall()->wait();
173 class ChangeRequestInterceptor extends Grpc\Interceptor
175 public function interceptUnaryUnary($method,
179 array $metadata = [],
182 $argument->setData('intercepted_unary_request');
183 return $continuation($method, $argument, $deserialize, $metadata, $options);
185 public function interceptStreamUnary($method,
188 array $metadata = [],
191 return new ChangeRequestCall(
192 $continuation($method, $deserialize, $metadata, $options)
197 class StopCallInterceptor extends Grpc\Interceptor
199 public function interceptUnaryUnary($method,
203 array $metadata = [],
206 $metadata["foo"] = array('interceptor_from_request_response');
208 public function interceptStreamUnary($method,
211 array $metadata = [],
214 $metadata["foo"] = array('interceptor_from_request_response');
218 class InterceptorTest extends \PHPUnit\Framework\TestCase
220 public function setUp(): void
222 $this->server = new Grpc\Server([]);
223 $this->port = $this->server->addHttp2Port('0.0.0.0:0');
224 $this->channel = new Grpc\Channel('localhost:'.$this->port, [
226 'credentials' => Grpc\ChannelCredentials::createInsecure()]);
227 $this->server->start();
230 public function tearDown(): void
232 $this->channel->close();
233 unset($this->server);
237 public function testClientChangeMetadataOneInterceptor()
239 $req_text = 'client_request';
240 $channel_matadata_interceptor = new ChangeMetadataInterceptor();
241 $intercept_channel = Grpc\Interceptor::intercept($this->channel, $channel_matadata_interceptor);
242 $client = new InterceptorClient('localhost:'.$this->port, [
244 'credentials' => Grpc\ChannelCredentials::createInsecure(),
245 ], $intercept_channel);
246 $req = new SimpleRequest($req_text);
247 $unary_call = $client->UnaryCall($req);
248 $event = $this->server->requestCall();
249 $this->assertSame('/dummy_method', $event->method);
250 $this->assertSame(['interceptor_from_unary_request'], $event->metadata['foo']);
252 $stream_call = $client->StreamCall();
253 $stream_call->write($req);
254 $event = $this->server->requestCall();
255 $this->assertSame('/dummy_method', $event->method);
256 $this->assertSame(['interceptor_from_stream_request'], $event->metadata['foo']);
263 public function testClientChangeMetadataTwoInterceptor()
265 $req_text = 'client_request';
266 $channel_matadata_interceptor = new ChangeMetadataInterceptor();
267 $channel_matadata_intercepto2 = new ChangeMetadataInterceptor2();
268 // test intercept separately.
269 $intercept_channel1 = Grpc\Interceptor::intercept($this->channel, $channel_matadata_interceptor);
270 $intercept_channel2 = Grpc\Interceptor::intercept($intercept_channel1, $channel_matadata_intercepto2);
271 $client = new InterceptorClient('localhost:'.$this->port, [
273 'credentials' => Grpc\ChannelCredentials::createInsecure(),
274 ], $intercept_channel2);
276 $req = new SimpleRequest($req_text);
277 $unary_call = $client->UnaryCall($req);
278 $event = $this->server->requestCall();
279 $this->assertSame('/dummy_method', $event->method);
280 $this->assertSame(['interceptor_from_unary_request'], $event->metadata['foo']);
281 $this->assertSame(['interceptor_from_unary_request'], $event->metadata['bar']);
283 $stream_call = $client->StreamCall();
284 $stream_call->write($req);
285 $event = $this->server->requestCall();
286 $this->assertSame('/dummy_method', $event->method);
287 $this->assertSame(['interceptor_from_stream_request'], $event->metadata['foo']);
288 $this->assertSame(['interceptor_from_stream_request'], $event->metadata['bar']);
294 // test intercept by array.
295 $intercept_channel3 = Grpc\Interceptor::intercept($this->channel,
296 [$channel_matadata_intercepto2, $channel_matadata_interceptor]);
297 $client = new InterceptorClient('localhost:'.$this->port, [
299 'credentials' => Grpc\ChannelCredentials::createInsecure(),
300 ], $intercept_channel3);
302 $req = new SimpleRequest($req_text);
303 $unary_call = $client->UnaryCall($req);
304 $event = $this->server->requestCall();
305 $this->assertSame('/dummy_method', $event->method);
306 $this->assertSame(['interceptor_from_unary_request'], $event->metadata['foo']);
307 $this->assertSame(['interceptor_from_unary_request'], $event->metadata['bar']);
309 $stream_call = $client->StreamCall();
310 $stream_call->write($req);
311 $event = $this->server->requestCall();
312 $this->assertSame('/dummy_method', $event->method);
313 $this->assertSame(['interceptor_from_stream_request'], $event->metadata['foo']);
314 $this->assertSame(['interceptor_from_stream_request'], $event->metadata['bar']);
321 public function testClientChangeRequestInterceptor()
323 $req_text = 'client_request';
324 $change_request_interceptor = new ChangeRequestInterceptor();
325 $intercept_channel = Grpc\Interceptor::intercept($this->channel,
326 $change_request_interceptor);
327 $client = new InterceptorClient('localhost:'.$this->port, [
329 'credentials' => Grpc\ChannelCredentials::createInsecure(),
330 ], $intercept_channel);
332 $req = new SimpleRequest($req_text);
333 $unary_call = $client->UnaryCall($req);
335 $event = $this->server->requestCall();
336 $this->assertSame('/dummy_method', $event->method);
337 $server_call = $event->call;
338 $event = $server_call->startBatch([
339 Grpc\OP_SEND_INITIAL_METADATA => [],
340 Grpc\OP_SEND_STATUS_FROM_SERVER => [
342 'code' => Grpc\STATUS_OK,
345 Grpc\OP_RECV_MESSAGE => true,
346 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
348 $this->assertSame('intercepted_unary_request', $event->message);
350 $stream_call = $client->StreamCall();
351 $stream_call->write($req);
352 $event = $this->server->requestCall();
353 $this->assertSame('/dummy_method', $event->method);
354 $server_call = $event->call;
355 $event = $server_call->startBatch([
356 Grpc\OP_SEND_INITIAL_METADATA => [],
357 Grpc\OP_SEND_STATUS_FROM_SERVER => [
359 'code' => Grpc\STATUS_OK,
362 Grpc\OP_RECV_MESSAGE => true,
363 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
365 $this->assertSame('intercepted_stream_request', $event->message);
372 public function testClientChangeStopCallInterceptor()
374 $req_text = 'client_request';
375 $channel_request_interceptor = new StopCallInterceptor();
376 $intercept_channel = Grpc\Interceptor::intercept($this->channel,
377 $channel_request_interceptor);
378 $client = new InterceptorClient('localhost:'.$this->port, [
380 'credentials' => Grpc\ChannelCredentials::createInsecure(),
381 ], $intercept_channel);
383 $req = new SimpleRequest($req_text);
384 $unary_call = $client->UnaryCall($req);
385 $this->assertNull($unary_call);
388 $stream_call = $client->StreamCall();
389 $this->assertNull($stream_call);
396 public function testGetInterceptorChannelConnectivityState()
398 $channel = new Grpc\Channel(
402 'credentials' => Grpc\ChannelCredentials::createInsecure()
405 $interceptor_channel = Grpc\Interceptor::intercept($channel, new Grpc\Interceptor());
406 $state = $interceptor_channel->getConnectivityState();
407 $this->assertEquals(0, $state);
411 public function testInterceptorChannelWatchConnectivityState()
413 $channel = new Grpc\Channel(
417 'credentials' => Grpc\ChannelCredentials::createInsecure()
420 $interceptor_channel = Grpc\Interceptor::intercept($channel, new Grpc\Interceptor());
421 $now = Grpc\Timeval::now();
422 $deadline = $now->add(new Grpc\Timeval(100*1000));
423 $state = $interceptor_channel->watchConnectivityState(1, $deadline);
424 $this->assertTrue($state);
430 public function testInterceptorChannelClose()
432 $channel = new Grpc\Channel(
436 'credentials' => Grpc\ChannelCredentials::createInsecure()
439 $interceptor_channel = Grpc\Interceptor::intercept($channel, new Grpc\Interceptor());
440 $this->assertNotNull($interceptor_channel);
444 public function testInterceptorChannelGetTarget()
446 $channel = new Grpc\Channel(
450 'credentials' => Grpc\ChannelCredentials::createInsecure()
453 $interceptor_channel = Grpc\Interceptor::intercept($channel, new Grpc\Interceptor());
454 $target = $interceptor_channel->getTarget();
455 $this->assertTrue(is_string($target));