From 636617d364b561702bf816bdd33ae1ca728b362a Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Fri, 9 Nov 2018 16:58:25 -0800 Subject: [PATCH 1/3] Handle missing data appropriately Change-Id: I007b124aecadce7d8e189cf4031c1c809ec793ca --- functions/helloworld/main.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/functions/helloworld/main.py b/functions/helloworld/main.py index c2c20d6c7ed..49b4ec1dd3a 100644 --- a/functions/helloworld/main.py +++ b/functions/helloworld/main.py @@ -66,7 +66,7 @@ def hello_http(request): Response object using `make_response` . """ - request_json = request.get_json() + request_json = request.get_json(silent=True) if request_json and 'name' in request_json: name = escape(request_json['name']) else: @@ -119,7 +119,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': From 1ad33863f4e11921adf78f7c901528af99ea4d23 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Fri, 9 Nov 2018 17:06:13 -0800 Subject: [PATCH 2/3] Add missing data case to tests Change-Id: Ieb4ef73ac5a675908904ffccac902daff69551c1 --- functions/helloworld/main_test.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/functions/helloworld/main_test.py b/functions/helloworld/main_test.py index d4f509ee1ee..7d1e70b6b5b 100644 --- a/functions/helloworld/main_test.py +++ b/functions/helloworld/main_test.py @@ -42,6 +42,12 @@ def test_hello_http_args(app): 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): with app.test_request_context(json={'name': ''}): res = main.hello_http(flask.request) @@ -54,6 +60,14 @@ def test_hello_content_json(app): 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): with app.test_request_context( data={'name': 'test'}, From 14948a23bebd6ec9f0beaa2ab4d4a4a57cd3fb55 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Tue, 13 Nov 2018 14:00:55 -0800 Subject: [PATCH 3/3] Console consistency: remove commas + support GETs Change-Id: I2315d6dd468da3acc2dc6c855ad28bfb03490626 --- functions/helloworld/.gcloudignore | 1 + functions/helloworld/main.py | 18 +++++++++++------- functions/helloworld/main_test.py | 20 +++++++++++++------- functions/helloworld/sample_http_test.py | 10 ++++++---- functions/helloworld/sample_pubsub_test.py | 4 ++-- 5 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 functions/helloworld/.gcloudignore diff --git a/functions/helloworld/.gcloudignore b/functions/helloworld/.gcloudignore new file mode 100644 index 00000000000..0ba261093e3 --- /dev/null +++ b/functions/helloworld/.gcloudignore @@ -0,0 +1 @@ +*test.py diff --git a/functions/helloworld/main.py b/functions/helloworld/main.py index 49b4ec1dd3a..ffe4e10b35d 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] @@ -67,11 +67,15 @@ def hello_http(request): . """ 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] @@ -132,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] @@ -150,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 7d1e70b6b5b..e033276749c 100644 --- a/functions/helloworld/main_test.py +++ b/functions/helloworld/main_test.py @@ -27,25 +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 + assert 'Hello World!' in res def test_hello_http_xss(app): @@ -57,7 +63,7 @@ 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): @@ -73,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): @@ -85,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 98ddddd9366..0d58cf21021 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 26ef363feaf..64fc8e1d02a 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]