Skip to content

Commit 6928491

Browse files
Ace Nassriengelke
Ace Nassri
authored andcommitted
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
1 parent c22840f commit 6928491

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

functions/helloworld/.gcloudignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*test.py

functions/helloworld/main.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def hello_get(request):
3434
Response object using `make_response`
3535
<http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
3636
"""
37-
return 'Hello, World!'
37+
return 'Hello World!'
3838
# [END functions_helloworld_get]
3939

4040

@@ -50,7 +50,7 @@ def hello_background(data, context):
5050
name = data['name']
5151
else:
5252
name = 'World'
53-
return 'Hello, {}!'.format(name)
53+
return 'Hello {}!'.format(name)
5454
# [END functions_helloworld_background]
5555
# [END functions_tips_terminate]
5656

@@ -66,12 +66,16 @@ def hello_http(request):
6666
Response object using `make_response`
6767
<http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
6868
"""
69-
request_json = request.get_json()
69+
request_json = request.get_json(silent=True)
70+
request_args = request.args
71+
7072
if request_json and 'name' in request_json:
71-
name = escape(request_json['name'])
73+
name = request_json['name']
74+
elif request_args and 'name' in request_args:
75+
name = request_args['name']
7276
else:
7377
name = 'World'
74-
return 'Hello, {}!'.format(name)
78+
return 'Hello {}!'.format(escape(name))
7579
# [END functions_helloworld_http]
7680

7781

@@ -89,7 +93,7 @@ def hello_pubsub(data, context):
8993
name = base64.b64decode(data['data']).decode('utf-8')
9094
else:
9195
name = 'World'
92-
print('Hello, {}!'.format(name))
96+
print('Hello {}!'.format(name))
9397
# [END functions_helloworld_pubsub]
9498

9599

@@ -119,7 +123,11 @@ def hello_content(request):
119123
"""
120124
content_type = request.headers['content-type']
121125
if content_type == 'application/json':
122-
name = request.json.get('name')
126+
request_json = request.get_json(silent=True)
127+
if request_json and 'name' in request_json:
128+
name = request_json['name']
129+
else:
130+
raise ValueError("JSON is invalid, or missing a 'name' property")
123131
elif content_type == 'application/octet-stream':
124132
name = request.data
125133
elif content_type == 'text/plain':
@@ -128,7 +136,7 @@ def hello_content(request):
128136
name = request.form.get('name')
129137
else:
130138
raise ValueError("Unknown content type: {}".format(content_type))
131-
return 'Hello, {}!'.format(escape(name))
139+
return 'Hello {}!'.format(escape(name))
132140
# [END functions_http_content]
133141

134142

@@ -146,7 +154,7 @@ def hello_method(request):
146154
from flask import abort
147155

148156
if request.method == 'GET':
149-
return 'Hello, World!'
157+
return 'Hello World!'
150158
elif request.method == 'PUT':
151159
return abort(403)
152160
else:

functions/helloworld/main_test.py

+26-6
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,31 @@ def app():
2727
def test_hello_get(app):
2828
with app.test_request_context():
2929
res = main.hello_get(flask.request)
30-
assert 'Hello, World!' in res
30+
assert 'Hello World!' in res
3131

3232

3333
def test_hello_http_no_args(app):
3434
with app.test_request_context():
3535
res = main.hello_http(flask.request)
36-
assert 'Hello, World!' in res
36+
assert 'Hello World!' in res
37+
38+
39+
def test_hello_http_get(app):
40+
with app.test_request_context(query_string={'name': 'test'}):
41+
res = main.hello_http(flask.request)
42+
assert 'Hello test!' in res
3743

3844

3945
def test_hello_http_args(app):
4046
with app.test_request_context(json={'name': 'test'}):
4147
res = main.hello_http(flask.request)
42-
assert 'Hello, test!' in res
48+
assert 'Hello test!' in res
49+
50+
51+
def test_hello_http_empty_json(app):
52+
with app.test_request_context(json=''):
53+
res = main.hello_http(flask.request)
54+
assert 'Hello World!' in res
4355

4456

4557
def test_hello_http_xss(app):
@@ -51,15 +63,23 @@ def test_hello_http_xss(app):
5163
def test_hello_content_json(app):
5264
with app.test_request_context(json={'name': 'test'}):
5365
res = main.hello_content(flask.request)
54-
assert 'Hello, test!' in res
66+
assert 'Hello test!' in res
67+
68+
69+
def test_hello_content_empty_json(app):
70+
with app.test_request_context(json=''):
71+
with pytest.raises(
72+
ValueError,
73+
message="JSON is invalid, or missing a 'name' property"):
74+
main.hello_content(flask.request)
5575

5676

5777
def test_hello_content_urlencoded(app):
5878
with app.test_request_context(
5979
data={'name': 'test'},
6080
content_type='application/x-www-form-urlencoded'):
6181
res = main.hello_content(flask.request)
62-
assert 'Hello, test!' in res
82+
assert 'Hello test!' in res
6383

6484

6585
def test_hello_content_xss(app):
@@ -71,4 +91,4 @@ def test_hello_content_xss(app):
7191
def test_hello_method(app):
7292
with app.test_request_context(method='GET'):
7393
res = main.hello_method(flask.request)
74-
assert 'Hello, World!' in res
94+
assert 'Hello World!' in res

functions/helloworld/sample_http_test.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@
2020

2121
def test_print_name():
2222
name = 'test'
23-
req = Mock(get_json=Mock(return_value={'name': name}))
23+
data = {'name': name}
24+
req = Mock(get_json=Mock(return_value=data), args=data)
2425

2526
# Call tested function
26-
assert main.hello_http(req) == 'Hello, {}!'.format(name)
27+
assert main.hello_http(req) == 'Hello {}!'.format(name)
2728

2829

2930
def test_print_hello_world():
30-
req = Mock(get_json=Mock(return_value={}))
31+
data = {}
32+
req = Mock(get_json=Mock(return_value=data), args=data)
3133

3234
# Call tested function
33-
assert main.hello_http(req) == 'Hello, World!'
35+
assert main.hello_http(req) == 'Hello World!'
3436
# [END functions_http_unit_test]

functions/helloworld/sample_pubsub_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_print_hello_world(capsys):
2424
# Call tested function
2525
main.hello_pubsub(data, None)
2626
out, err = capsys.readouterr()
27-
assert out == 'Hello, World!\n'
27+
assert out == 'Hello World!\n'
2828

2929

3030
def test_print_name(capsys):
@@ -34,5 +34,5 @@ def test_print_name(capsys):
3434
# Call tested function
3535
main.hello_pubsub(data, None)
3636
out, err = capsys.readouterr()
37-
assert out == 'Hello, {}!\n'.format(name)
37+
assert out == 'Hello {}!\n'.format(name)
3838
# [END functions_pubsub_unit_test]

0 commit comments

Comments
 (0)