Skip to content

Commit

Permalink
GCF samples: handle {empty JSON, GET} requests + remove commas (#1832)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Ace Nassri authored and engelke committed Nov 20, 2018
1 parent c22840f commit 6928491
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 21 deletions.
1 change: 1 addition & 0 deletions functions/helloworld/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*test.py
26 changes: 17 additions & 9 deletions functions/helloworld/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def hello_get(request):
Response object using `make_response`
<http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
"""
return 'Hello, World!'
return 'Hello World!'
# [END functions_helloworld_get]


Expand All @@ -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]

Expand All @@ -66,12 +66,16 @@ def hello_http(request):
Response object using `make_response`
<http://flask.pocoo.org/docs/1.0/api/#flask.Flask.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]


Expand All @@ -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]


Expand Down Expand Up @@ -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':
Expand All @@ -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]


Expand All @@ -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:
Expand Down
32 changes: 26 additions & 6 deletions functions/helloworld/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -51,15 +63,23 @@ 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):
with app.test_request_context(
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):
Expand All @@ -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
10 changes: 6 additions & 4 deletions functions/helloworld/sample_http_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
4 changes: 2 additions & 2 deletions functions/helloworld/sample_pubsub_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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]

0 comments on commit 6928491

Please sign in to comment.