From 6928491ed3d52b0bec694e6b30257f08caac5f2b Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Tue, 20 Nov 2018 14:13:27 -0800 Subject: [PATCH] GCF samples: handle {empty JSON, GET} requests + remove commas (#1832) * Handle missing data appropriately Change-Id: I007b124aecadce7d8e189cf4031c1c809ec793ca * Add missing data case to tests Change-Id: Ieb4ef73ac5a675908904ffccac902daff69551c1 * Console consistency: remove commas + support GETs Change-Id: I2315d6dd468da3acc2dc6c855ad28bfb03490626 --- functions/helloworld/.gcloudignore | 1 + functions/helloworld/main.py | 26 ++++++++++++------ functions/helloworld/main_test.py | 32 ++++++++++++++++++---- functions/helloworld/sample_http_test.py | 10 ++++--- functions/helloworld/sample_pubsub_test.py | 4 +-- 5 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 functions/helloworld/.gcloudignore diff --git a/functions/helloworld/.gcloudignore b/functions/helloworld/.gcloudignore new file mode 100644 index 000000000000..0ba261093e3b --- /dev/null +++ b/functions/helloworld/.gcloudignore @@ -0,0 +1 @@ +*test.py diff --git a/functions/helloworld/main.py b/functions/helloworld/main.py index dba338efc944..da9b593d4750 100644 --- a/functions/helloworld/main.py +++ b/functions/helloworld/main.py @@ -34,7 +34,7 @@ def hello_get(request): Response object using `make_response` . """ - return 'Hello, World!' + return 'Hello World!' # [END functions_helloworld_get] @@ -50,7 +50,7 @@ def hello_background(data, context): name = data['name'] else: name = 'World' - return 'Hello, {}!'.format(name) + return 'Hello {}!'.format(name) # [END functions_helloworld_background] # [END functions_tips_terminate] @@ -66,12 +66,16 @@ def hello_http(request): Response object using `make_response` . """ - request_json = request.get_json() + request_json = request.get_json(silent=True) + request_args = request.args + if request_json and 'name' in request_json: - name = escape(request_json['name']) + name = request_json['name'] + elif request_args and 'name' in request_args: + name = request_args['name'] else: name = 'World' - return 'Hello, {}!'.format(name) + return 'Hello {}!'.format(escape(name)) # [END functions_helloworld_http] @@ -89,7 +93,7 @@ def hello_pubsub(data, context): name = base64.b64decode(data['data']).decode('utf-8') else: name = 'World' - print('Hello, {}!'.format(name)) + print('Hello {}!'.format(name)) # [END functions_helloworld_pubsub] @@ -119,7 +123,11 @@ def hello_content(request): """ content_type = request.headers['content-type'] if content_type == 'application/json': - name = request.json.get('name') + request_json = request.get_json(silent=True) + if request_json and 'name' in request_json: + name = request_json['name'] + else: + raise ValueError("JSON is invalid, or missing a 'name' property") elif content_type == 'application/octet-stream': name = request.data elif content_type == 'text/plain': @@ -128,7 +136,7 @@ def hello_content(request): name = request.form.get('name') else: raise ValueError("Unknown content type: {}".format(content_type)) - return 'Hello, {}!'.format(escape(name)) + return 'Hello {}!'.format(escape(name)) # [END functions_http_content] @@ -146,7 +154,7 @@ def hello_method(request): from flask import abort if request.method == 'GET': - return 'Hello, World!' + return 'Hello World!' elif request.method == 'PUT': return abort(403) else: diff --git a/functions/helloworld/main_test.py b/functions/helloworld/main_test.py index d4f509ee1eeb..e033276749c0 100644 --- a/functions/helloworld/main_test.py +++ b/functions/helloworld/main_test.py @@ -27,19 +27,31 @@ def app(): def test_hello_get(app): with app.test_request_context(): res = main.hello_get(flask.request) - assert 'Hello, World!' in res + assert 'Hello World!' in res def test_hello_http_no_args(app): with app.test_request_context(): res = main.hello_http(flask.request) - assert 'Hello, World!' in res + assert 'Hello World!' in res + + +def test_hello_http_get(app): + with app.test_request_context(query_string={'name': 'test'}): + res = main.hello_http(flask.request) + assert 'Hello test!' in res def test_hello_http_args(app): with app.test_request_context(json={'name': 'test'}): res = main.hello_http(flask.request) - assert 'Hello, test!' in res + assert 'Hello test!' in res + + +def test_hello_http_empty_json(app): + with app.test_request_context(json=''): + res = main.hello_http(flask.request) + assert 'Hello World!' in res def test_hello_http_xss(app): @@ -51,7 +63,15 @@ def test_hello_http_xss(app): def test_hello_content_json(app): with app.test_request_context(json={'name': 'test'}): res = main.hello_content(flask.request) - assert 'Hello, test!' in res + assert 'Hello test!' in res + + +def test_hello_content_empty_json(app): + with app.test_request_context(json=''): + with pytest.raises( + ValueError, + message="JSON is invalid, or missing a 'name' property"): + main.hello_content(flask.request) def test_hello_content_urlencoded(app): @@ -59,7 +79,7 @@ def test_hello_content_urlencoded(app): data={'name': 'test'}, content_type='application/x-www-form-urlencoded'): res = main.hello_content(flask.request) - assert 'Hello, test!' in res + assert 'Hello test!' in res def test_hello_content_xss(app): @@ -71,4 +91,4 @@ def test_hello_content_xss(app): def test_hello_method(app): with app.test_request_context(method='GET'): res = main.hello_method(flask.request) - assert 'Hello, World!' in res + assert 'Hello World!' in res diff --git a/functions/helloworld/sample_http_test.py b/functions/helloworld/sample_http_test.py index 98ddddd9366b..0d58cf21021d 100644 --- a/functions/helloworld/sample_http_test.py +++ b/functions/helloworld/sample_http_test.py @@ -20,15 +20,17 @@ def test_print_name(): name = 'test' - req = Mock(get_json=Mock(return_value={'name': name})) + data = {'name': name} + req = Mock(get_json=Mock(return_value=data), args=data) # Call tested function - assert main.hello_http(req) == 'Hello, {}!'.format(name) + assert main.hello_http(req) == 'Hello {}!'.format(name) def test_print_hello_world(): - req = Mock(get_json=Mock(return_value={})) + data = {} + req = Mock(get_json=Mock(return_value=data), args=data) # Call tested function - assert main.hello_http(req) == 'Hello, World!' + assert main.hello_http(req) == 'Hello World!' # [END functions_http_unit_test] diff --git a/functions/helloworld/sample_pubsub_test.py b/functions/helloworld/sample_pubsub_test.py index 26ef363feafa..64fc8e1d02af 100644 --- a/functions/helloworld/sample_pubsub_test.py +++ b/functions/helloworld/sample_pubsub_test.py @@ -24,7 +24,7 @@ def test_print_hello_world(capsys): # Call tested function main.hello_pubsub(data, None) out, err = capsys.readouterr() - assert out == 'Hello, World!\n' + assert out == 'Hello World!\n' def test_print_name(capsys): @@ -34,5 +34,5 @@ def test_print_name(capsys): # Call tested function main.hello_pubsub(data, None) out, err = capsys.readouterr() - assert out == 'Hello, {}!\n'.format(name) + assert out == 'Hello {}!\n'.format(name) # [END functions_pubsub_unit_test]