Skip to content

GCF samples: handle {empty JSON, GET} requests + remove commas #1832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]