Skip to content

Commit 03515be

Browse files
authored
feat(functions): add functions_http_unit_test and functions_http_content (GoogleCloudPlatform#1194)
1 parent a221a29 commit 03515be

File tree

11 files changed

+361
-5
lines changed

11 files changed

+361
-5
lines changed
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
{
22
"require": {
33
"google/cloud-functions-framework": "^0.6"
4-
},
5-
"autoload-dev": {
6-
"psr-4": {
7-
"Google\\Cloud\\Samples\\Functions\\HelloworldHttp\\Test\\": "test"
8-
}
94
}
105
}

functions/helloworld_http/test/DeployTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
2323
use PHPUnit\Framework\TestCase;
2424

25+
require_once __DIR__ . '/TestCasesTrait.php';
26+
2527
/**
2628
* Class DeployTest.
2729
*
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
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+
// [START functions_http_unit_test]
19+
20+
namespace Google\Cloud\Samples\Functions\HelloworldHttp\Test;
21+
22+
use GuzzleHttp\Psr7\ServerRequest;
23+
use PHPUnit\Framework\TestCase;
24+
25+
/**
26+
* Class SampleUnitTest.
27+
*
28+
* Unit test for helloHttp.
29+
*/
30+
class SampleUnitTest extends TestCase
31+
{
32+
public static function setUpBeforeClass(): void
33+
{
34+
require_once __DIR__ . '/../index.php';
35+
}
36+
37+
public function testFunction() : void
38+
{
39+
$name = uniqid();
40+
$request = new ServerRequest('POST', '/', [], json_encode(['name' => $name]));
41+
$expected = sprintf('Hello, %s!', $name);
42+
$actual = helloHttp($request);
43+
$this->assertContains($expected, $actual);
44+
}
45+
}
46+
47+
// [END functions_http_unit_test]

functions/helloworld_http/test/SystemTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use PHPUnit\Framework\TestCase;
2323
use Google\Cloud\TestUtils\CloudFunctionLocalTestTrait;
2424

25+
require_once __DIR__ . '/TestCasesTrait.php';
26+
2527
/**
2628
* Class SystemTest.
2729
*/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"google/cloud-functions-framework": "^0.6"
4+
}
5+
}

functions/http_content_type/index.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
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+
// [START functions_http_content]
19+
20+
use Psr\Http\Message\ServerRequestInterface;
21+
22+
function helloContent(ServerRequestInterface $request): string
23+
{
24+
$name = 'World';
25+
$body = $request->getBody()->getContents();
26+
switch ($request->getHeader('content-type')[0]) {
27+
// '{"name":"John"}'
28+
case 'application/json':
29+
if (!empty($body)) {
30+
$json = json_decode($body, true);
31+
if (json_last_error() != JSON_ERROR_NONE) {
32+
throw new RuntimeException(sprintf(
33+
'Could not parse body: %s', json_last_error_msg()
34+
));
35+
}
36+
$name = $json['name'] ?? $name;
37+
}
38+
break;
39+
// 'John', stored in a stream
40+
case 'application/octet-stream':
41+
$name = $body;
42+
break;
43+
// 'John'
44+
case 'text/plain':
45+
$name = $body;
46+
break;
47+
// 'name=John' in the body of a POST request (not the URL)
48+
case 'application/x-www-form-urlencoded':
49+
parse_str($body, $data);
50+
$name = $data['name'] ?? $name;
51+
break;
52+
}
53+
54+
return sprintf('Hello %s!', htmlspecialchars($name));
55+
}
56+
57+
// [END functions_http_content]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="../../testing/bootstrap.php" convertWarningsToExceptions="false">
18+
<testsuites>
19+
<testsuite name="Cloud Functions Hello World HTTP Test Suite">
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">./srcdirectory>
29+
<exclude>
30+
<directory>./vendordirectory>
31+
exclude>
32+
whitelist>
33+
filter>
34+
phpunit>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
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+
namespace Google\Cloud\Samples\Functions\HttpContentType\Test;
19+
20+
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
21+
use PHPUnit\Framework\TestCase;
22+
23+
require_once __DIR__ . '/TestCasesTrait.php';
24+
25+
/**
26+
* Class DeployTest.
27+
*
28+
* This test is not run by the CI system.
29+
*
30+
* To skip deployment of a new function, run with "GOOGLE_SKIP_DEPLOYMENT=true".
31+
* To skip deletion of the tested function, run with "GOOGLE_KEEP_DEPLOYMENT=true".
32+
*/
33+
class DeployTest extends TestCase
34+
{
35+
use CloudFunctionDeploymentTrait;
36+
use TestCasesTrait;
37+
38+
private static $name = 'helloContent';
39+
40+
public function testFunction(): void
41+
{
42+
foreach (self::cases() as $test) {
43+
$resp = $this->client->post('', [
44+
'headers' => ['content-type' => $test['content-type']],
45+
'body' => $test['body'],
46+
// Uncomment and CURLOPT_VERBOSE debug content will be sent to stdout.
47+
// 'debug' => true,
48+
]);
49+
50+
$actual = trim((string) $resp->getBody());
51+
52+
$this->assertEquals($test['code'], $resp->getStatusCode(), $test['content-type'] . ':');
53+
// Failures often lead to a large HTML page in the response body.
54+
$this->assertContains($test['expected'], $actual, $test['content-type'] . ':');
55+
}
56+
}
57+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
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+
declare(strict_types=1);
19+
20+
namespace Google\Cloud\Samples\Functions\HttpContentType\Test;
21+
22+
use PHPUnit\Framework\TestCase;
23+
use Google\Cloud\TestUtils\CloudFunctionLocalTestTrait;
24+
25+
require_once __DIR__ . '/TestCasesTrait.php';
26+
27+
/**
28+
* Class SystemTest.
29+
*/
30+
class SystemTest extends TestCase
31+
{
32+
use CloudFunctionLocalTestTrait;
33+
use TestCasesTrait;
34+
35+
private static $name = 'helloContent';
36+
37+
public function testFunction(): void
38+
{
39+
foreach (self::cases() as $test) {
40+
$resp = $resp = $this->client->post('/', [
41+
'headers' => ['content-type' => $test['content-type']],
42+
'body' => $test['body'],
43+
]);
44+
$actual = trim((string) $resp->getBody());
45+
46+
$this->assertEquals($test['code'], $resp->getStatusCode(), $test['content-type'] . ':');
47+
// Failures often lead to a large HTML page in the response body.
48+
$this->assertContains($test['expected'], $actual, $test['content-type'] . ':');
49+
}
50+
}
51+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
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+
declare(strict_types=1);
18+
19+
namespace Google\Cloud\Samples\Functions\HttpContentType\Test;
20+
21+
trait TestCasesTrait
22+
{
23+
public static function cases() : array
24+
{
25+
return [
26+
[
27+
'content-type' => 'text/plain',
28+
'body' => 'John',
29+
'code' => '200',
30+
'expected' => 'Hello John!',
31+
],
32+
[
33+
'content-type' => 'application/json',
34+
'body' => json_encode(['name' => 'John']),
35+
'code' => '200',
36+
'expected' => 'Hello John!',
37+
],
38+
[
39+
'content-type' => 'application/octet-stream',
40+
'body' => 'John',
41+
'code' => '200',
42+
'expected' => 'Hello John!',
43+
],
44+
[
45+
'content-type' => 'application/x-www-form-urlencoded',
46+
'body' => 'name=John',
47+
'code' => '200',
48+
'expected' => 'Hello John!',
49+
],
50+
];
51+
}
52+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
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+
declare(strict_types=1);
19+
20+
namespace Google\Cloud\Samples\Functions\HttpContentType\Test;
21+
22+
use GuzzleHttp\Psr7\ServerRequest;
23+
use PHPUnit\Framework\TestCase;
24+
25+
require_once __DIR__ . '/TestCasesTrait.php';
26+
27+
/**
28+
* Unit tests for the Cloud Function.
29+
*/
30+
class UnitTest extends TestCase
31+
{
32+
use TestCasesTrait;
33+
34+
private static $name = 'helloContent';
35+
36+
public static function setUpBeforeClass()
37+
{
38+
require_once __DIR__ . '/../index.php';
39+
}
40+
41+
public function testFunction(): void
42+
{
43+
foreach (self::cases() as $test) {
44+
$request = new ServerRequest('POST', '/', ['content-type' => $test['content-type']], $test['body']);
45+
$actual = $this->runFunction(self::$name, [$request]);
46+
$this->assertContains($test['expected'], $actual, $test['content-type'] . ':');
47+
}
48+
}
49+
50+
private static function runFunction($functionName, array $params = []): string
51+
{
52+
return call_user_func_array($functionName, $params);
53+
}
54+
}

0 commit comments

Comments
 (0)