4 * Copyright 2015 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 class CallCredentialsTest extends \PHPUnit\Framework\TestCase
22 public function setUp(): void
24 $this->credentials = Grpc\ChannelCredentials::createSsl(
25 file_get_contents(dirname(__FILE__).'/../data/ca.pem'));
26 $this->call_credentials = Grpc\CallCredentials::createFromPlugin(
27 [$this, 'callbackFunc']);
28 $this->credentials = Grpc\ChannelCredentials::createComposite(
30 $this->call_credentials
32 $server_credentials = Grpc\ServerCredentials::createSsl(
34 file_get_contents(dirname(__FILE__).'/../data/server1.key'),
35 file_get_contents(dirname(__FILE__).'/../data/server1.pem'));
36 $this->server = new Grpc\Server();
37 $this->port = $this->server->addSecureHttp2Port('0.0.0.0:0',
39 $this->server->start();
40 $this->host_override = 'foo.test.google.fr';
41 $this->channel = new Grpc\Channel(
42 'localhost:'.$this->port,
45 'grpc.ssl_target_name_override' => $this->host_override,
46 'grpc.default_authority' => $this->host_override,
47 'credentials' => $this->credentials,
52 public function tearDown(): void
54 unset($this->channel);
58 public function callbackFunc($context)
60 $this->assertTrue(is_string($context->service_url));
61 $this->assertTrue(is_string($context->method_name));
63 return ['k1' => ['v1'], 'k2' => ['v2']];
66 public function testCreateFromPlugin()
68 $deadline = Grpc\Timeval::infFuture();
70 $call = new Grpc\Call($this->channel,
73 $this->host_override);
75 $event = $call->startBatch([
76 Grpc\OP_SEND_INITIAL_METADATA => [],
77 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
80 $this->assertTrue($event->send_metadata);
81 $this->assertTrue($event->send_close);
83 $event = $this->server->requestCall();
85 $this->assertTrue(is_array($event->metadata));
86 $metadata = $event->metadata;
87 $this->assertTrue(array_key_exists('k1', $metadata));
88 $this->assertTrue(array_key_exists('k2', $metadata));
89 $this->assertSame($metadata['k1'], ['v1']);
90 $this->assertSame($metadata['k2'], ['v2']);
92 $this->assertSame('/abc/dummy_method', $event->method);
93 $server_call = $event->call;
95 $event = $server_call->startBatch([
96 Grpc\OP_SEND_INITIAL_METADATA => [],
97 Grpc\OP_SEND_STATUS_FROM_SERVER => [
99 'code' => Grpc\STATUS_OK,
100 'details' => $status_text,
102 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
105 $this->assertTrue($event->send_metadata);
106 $this->assertTrue($event->send_status);
107 $this->assertFalse($event->cancelled);
109 $event = $call->startBatch([
110 Grpc\OP_RECV_INITIAL_METADATA => true,
111 Grpc\OP_RECV_STATUS_ON_CLIENT => true,
114 $this->assertSame([], $event->metadata);
115 $status = $event->status;
116 $this->assertSame([], $status->metadata);
117 $this->assertSame(Grpc\STATUS_OK, $status->code);
118 $this->assertSame($status_text, $status->details);
124 public function callbackFunc2($context)
129 public function testCreateComposite()
131 $call_credentials2 = Grpc\CallCredentials::createFromPlugin(
132 [$this, 'callbackFunc2']);
133 $call_credentials3 = Grpc\CallCredentials::createComposite(
134 $this->call_credentials,
137 $this->assertSame('Grpc\CallCredentials',
138 get_class($call_credentials3));
142 * @expectedException InvalidArgumentException
144 public function testCreateFromPluginInvalidParam()
146 $call_credentials = Grpc\CallCredentials::createFromPlugin(
152 * @expectedException InvalidArgumentException
154 public function testCreateCompositeInvalidParam()
156 $call_credentials3 = Grpc\CallCredentials::createComposite(
157 $this->call_credentials,