Skip to content

Commit 14948a2

Browse files
author
Ace Nassri
committed
Console consistency: remove commas + support GETs
Change-Id: I2315d6dd468da3acc2dc6c855ad28bfb03490626
1 parent 1ad3386 commit 14948a2

File tree

5 files changed

+33
-20
lines changed

5 files changed

+33
-20
lines changed

functions/helloworld/.gcloudignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*test.py

functions/helloworld/main.py

Lines changed: 11 additions & 7 deletions
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/0.12/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

@@ -67,11 +67,15 @@ def hello_http(request):
6767
<http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>.
6868
"""
6969
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

@@ -132,7 +136,7 @@ def hello_content(request):
132136
name = request.form.get('name')
133137
else:
134138
raise ValueError("Unknown content type: {}".format(content_type))
135-
return 'Hello, {}!'.format(escape(name))
139+
return 'Hello {}!'.format(escape(name))
136140
# [END functions_http_content]
137141

138142

@@ -150,7 +154,7 @@ def hello_method(request):
150154
from flask import abort
151155

152156
if request.method == 'GET':
153-
return 'Hello, World!'
157+
return 'Hello World!'
154158
elif request.method == 'PUT':
155159
return abort(403)
156160
else:

functions/helloworld/main_test.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +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
4349

4450

4551
def test_hello_http_empty_json(app):
4652
with app.test_request_context(json=''):
4753
res = main.hello_http(flask.request)
48-
assert 'Hello, World!' in res
54+
assert 'Hello World!' in res
4955

5056

5157
def test_hello_http_xss(app):
@@ -57,7 +63,7 @@ def test_hello_http_xss(app):
5763
def test_hello_content_json(app):
5864
with app.test_request_context(json={'name': 'test'}):
5965
res = main.hello_content(flask.request)
60-
assert 'Hello, test!' in res
66+
assert 'Hello test!' in res
6167

6268

6369
def test_hello_content_empty_json(app):
@@ -73,7 +79,7 @@ def test_hello_content_urlencoded(app):
7379
data={'name': 'test'},
7480
content_type='application/x-www-form-urlencoded'):
7581
res = main.hello_content(flask.request)
76-
assert 'Hello, test!' in res
82+
assert 'Hello test!' in res
7783

7884

7985
def test_hello_content_xss(app):
@@ -85,4 +91,4 @@ def test_hello_content_xss(app):
8591
def test_hello_method(app):
8692
with app.test_request_context(method='GET'):
8793
res = main.hello_method(flask.request)
88-
assert 'Hello, World!' in res
94+
assert 'Hello World!' in res

functions/helloworld/sample_http_test.py

Lines changed: 6 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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)