Skip to content

Commit cc1d047

Browse files
authored
feat: add InsecureRequestBuilder for emulator (#582)
1 parent 35ae9f7 commit cc1d047

File tree

6 files changed

+129
-6
lines changed

6 files changed

+129
-6
lines changed

src/GapicClientTrait.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ private function setClientOptions(array $options)
313313
$options['apiEndpoint'],
314314
$transport,
315315
$options['transportConfig'],
316-
$options['clientCertSource']
316+
$options['clientCertSource'],
317+
$options['hasEmulator'] ?? false
317318
);
318319
}
319320

@@ -322,14 +323,16 @@ private function setClientOptions(array $options)
322323
* @param string $transport
323324
* @param TransportOptions|array $transportConfig
324325
* @param callable $clientCertSource
326+
* @param bool $hasEmulator
325327
* @return TransportInterface
326328
* @throws ValidationException
327329
*/
328330
private function createTransport(
329331
string $apiEndpoint,
330332
$transport,
331333
$transportConfig,
332-
callable $clientCertSource = null
334+
callable $clientCertSource = null,
335+
bool $hasEmulator = false
333336
) {
334337
if (!is_string($transport)) {
335338
throw new ValidationException(
@@ -372,6 +375,8 @@ private function createTransport(
372375
);
373376
}
374377
$restConfigPath = $configForSpecifiedTransport['restClientConfigPath'];
378+
$configForSpecifiedTransport['hasEmulator'] = $hasEmulator;
379+
375380
return RestTransport::build($apiEndpoint, $restConfigPath, $configForSpecifiedTransport);
376381
default:
377382
throw new ValidationException(

src/InsecureRequestBuilder.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Google\ApiCore;
4+
5+
use GuzzleHttp\Psr7\Utils;
6+
use Psr\Http\Message\UriInterface;
7+
8+
/**
9+
* @internal
10+
*/
11+
class InsecureRequestBuilder extends RequestBuilder
12+
{
13+
/**
14+
* @param string $path
15+
* @param array $queryParams
16+
* @return UriInterface
17+
*/
18+
protected function buildUri(string $path, array $queryParams)
19+
{
20+
$uri = Utils::uriFor(sprintf(
21+
'http://%s%s',
22+
$this->baseUri,
23+
$path
24+
));
25+
if ($queryParams) {
26+
$uri = $this->buildUriWithQuery(
27+
$uri,
28+
$queryParams
29+
);
30+
}
31+
return $uri;
32+
}
33+
}

src/RequestBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class RequestBuilder
5050
use UriTrait;
5151
use ValidationTrait;
5252

53-
private $baseUri;
53+
protected $baseUri;
5454
private $restConfig;
5555

5656
/**
@@ -270,7 +270,7 @@ private function tryRenderPathTemplate(string $uriTemplate, array $bindings)
270270
* @param array $queryParams
271271
* @return UriInterface
272272
*/
273-
private function buildUri(string $path, array $queryParams)
273+
protected function buildUri(string $path, array $queryParams)
274274
{
275275
$uri = Utils::uriFor(
276276
sprintf(

src/Transport/RestTransport.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
use Google\ApiCore\ApiException;
3535
use Google\ApiCore\Call;
36+
use Google\ApiCore\InsecureRequestBuilder;
3637
use Google\ApiCore\RequestBuilder;
3738
use Google\ApiCore\ServerStream;
3839
use Google\ApiCore\ServiceAddressTrait;
@@ -83,6 +84,7 @@ public function __construct(
8384
*
8485
* @type callable $httpHandler A handler used to deliver PSR-7 requests.
8586
* @type callable $clientCertSource A callable which returns the client cert as a string.
87+
* @type bool $hasEmulator True if the emulator is enabled.
8688
* }
8789
* @return RestTransport
8890
* @throws ValidationException
@@ -92,9 +94,12 @@ public static function build(string $apiEndpoint, string $restConfigPath, array
9294
$config += [
9395
'httpHandler' => null,
9496
'clientCertSource' => null,
97+
'hasEmulator' => false,
9598
];
9699
list($baseUri, $port) = self::normalizeServiceAddress($apiEndpoint);
97-
$requestBuilder = new RequestBuilder("$baseUri:$port", $restConfigPath);
100+
$requestBuilder = $config['hasEmulator']
101+
? new InsecureRequestBuilder("$baseUri:$port", $restConfigPath)
102+
: new RequestBuilder("$baseUri:$port", $restConfigPath);
98103
$httpHandler = $config['httpHandler'] ?: self::buildHttpHandlerAsync();
99104
$transport = new RestTransport($requestBuilder, $httpHandler);
100105
if ($config['clientCertSource']) {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
use Google\ApiCore\InsecureRequestBuilder;
20+
use Google\ApiCore\Testing\MockRequestBody;
21+
use PHPUnit\Framework\TestCase;
22+
23+
/**
24+
* @group core
25+
*/
26+
class InsecureRequestBuilderTest extends TestCase
27+
{
28+
private $builder;
29+
30+
const SERVICE_NAME = 'test.interface.v1.api';
31+
32+
public function setUp(): void
33+
{
34+
$this->builder = new InsecureRequestBuilder(
35+
'www.example.com',
36+
__DIR__ . '/testdata/test_service_rest_client_config.php'
37+
);
38+
}
39+
40+
public function testMethodWithUrlPlaceholder()
41+
{
42+
$message = new MockRequestBody();
43+
$message->setName('message/foo');
44+
45+
$request = $this->builder->build(self::SERVICE_NAME . '/MethodWithUrlPlaceholder', $message);
46+
$uri = $request->getUri();
47+
48+
$this->assertSame('http', $uri->getScheme());
49+
$this->assertEmpty($uri->getQuery());
50+
$this->assertEmpty((string) $request->getBody());
51+
$this->assertSame('/v1/message/foo', $uri->getPath());
52+
}
53+
54+
public function testMethodWithBody()
55+
{
56+
$message = new MockRequestBody();
57+
$message->setName('message/foo');
58+
$nestedMessage = new MockRequestBody();
59+
$nestedMessage->setName('nested/foo');
60+
$message->setNestedMessage($nestedMessage);
61+
62+
$request = $this->builder->build(self::SERVICE_NAME . '/MethodWithBodyAndUrlPlaceholder', $message);
63+
$uri = $request->getUri();
64+
65+
$this->assertSame('http', $uri->getScheme());
66+
$this->assertEmpty($uri->getQuery());
67+
$this->assertSame('/v1/message/foo', $uri->getPath());
68+
$this->assertEquals(
69+
['name' => 'message/foo', 'nestedMessage' => ['name' => 'nested/foo']],
70+
json_decode($request->getBody(), true)
71+
);
72+
}
73+
}

tests/Tests/Unit/RequestBuilderTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function testMethodWithUrlPlaceholder()
6262
$request = $this->builder->build(self::SERVICE_NAME . '/MethodWithUrlPlaceholder', $message);
6363
$uri = $request->getUri();
6464

65+
$this->assertSame('https', $uri->getScheme());
6566
$this->assertEmpty($uri->getQuery());
6667
$this->assertEmpty((string) $request->getBody());
6768
$this->assertSame('/v1/message/foo', $uri->getPath());
@@ -78,6 +79,7 @@ public function testMethodWithBody()
7879
$request = $this->builder->build(self::SERVICE_NAME . '/MethodWithBodyAndUrlPlaceholder', $message);
7980
$uri = $request->getUri();
8081

82+
$this->assertSame('https', $uri->getScheme());
8183
$this->assertEmpty($uri->getQuery());
8284
$this->assertSame('/v1/message/foo', $uri->getPath());
8385
$this->assertEquals(
@@ -97,6 +99,7 @@ public function testMethodWithNestedMessageAsBody()
9799
$request = $this->builder->build(self::SERVICE_NAME . '/MethodWithNestedMessageAsBody', $message);
98100
$uri = $request->getUri();
99101

102+
$this->assertSame('https', $uri->getScheme());
100103
$this->assertEmpty($uri->getQuery());
101104
$this->assertSame('/v1/message/foo', $uri->getPath());
102105
$this->assertEquals(
@@ -161,6 +164,7 @@ public function testMethodWithNestedUrlPlaceholder()
161164
$request = $this->builder->build(self::SERVICE_NAME . '/MethodWithNestedUrlPlaceholder', $message);
162165
$uri = $request->getUri();
163166

167+
$this->assertSame('https', $uri->getScheme());
164168
$this->assertEmpty($uri->getQuery());
165169
$this->assertSame('/v1/nested/foo', $uri->getPath());
166170
$this->assertEquals(
@@ -179,6 +183,7 @@ public function testMethodWithUrlRepeatedField()
179183
$uri = $request->getUri();
180184

181185
$this->assertEmpty((string) $request->getBody());
186+
$this->assertSame('https', $uri->getScheme());
182187
$this->assertSame('/v1/message/foo', $uri->getPath());
183188
$this->assertSame('repeatedField=bar1&repeatedField=bar2', $uri->getQuery());
184189
}
@@ -206,6 +211,7 @@ public function testMethodWithColon()
206211
$request = $this->builder->build(self::SERVICE_NAME . '/MethodWithColonInUrl', $message);
207212
$uri = $request->getUri();
208213

214+
$this->assertSame('https', $uri->getScheme());
209215
$this->assertEmpty($uri->getQuery());
210216
$this->assertSame('/v1/message/foo:action', $uri->getPath());
211217
}
@@ -222,6 +228,7 @@ public function testMethodWithMultipleWildcardsAndColonInUrl()
222228
);
223229
$uri = $request->getUri();
224230

231+
$this->assertSame('https', $uri->getScheme());
225232
$this->assertEmpty($uri->getQuery());
226233
$this->assertSame('/v1/message/foo/number/10:action', $uri->getPath());
227234
}
@@ -236,7 +243,7 @@ public function testMethodWithSimplePlaceholder()
236243
$message
237244
);
238245
$uri = $request->getUri();
239-
246+
$this->assertSame('https', $uri->getScheme());
240247
$this->assertSame('/v1/message-name', $uri->getPath());
241248
}
242249

0 commit comments

Comments
 (0)