Skip to content

Commit 573d1e7

Browse files
author
Jon Wayne Parrott
committed
Merge pull request #257 from GoogleCloudPlatform/gae-requests
Adding app engine request handling samples.
2 parents 19bd9c8 + 1c04431 commit 573d1e7

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

appengine/requests/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## App Engine Requests Docs Snippets
2+
3+
These snippets demonstrate various aspects of App Engine Python request handling.
4+
5+
<!-- auto-doc-link -->
6+
These samples are used on the following documentation page:
7+
8+
> https://cloud.google.com/appengine/docs/python/requests
9+
10+
<!-- end-auto-doc-link -->

appengine/requests/app.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
runtime: python27
2+
api_version: 1
3+
threadsafe: yes
4+
5+
handlers:
6+
- url: .* # This regex directs all routes to main.app
7+
script: main.app

appengine/requests/main.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
Sample application that demonstrates various aspects of App Engine's request
17+
handling.
18+
"""
19+
20+
import os
21+
import time
22+
23+
import webapp2
24+
25+
26+
# [START request_timer]
27+
class TimerHandler(webapp2.RequestHandler):
28+
def get(self):
29+
from google.appengine.runtime import DeadlineExceededError
30+
31+
try:
32+
time.sleep(70)
33+
self.response.write('Completed.')
34+
except DeadlineExceededError:
35+
self.response.clear()
36+
self.response.set_status(500)
37+
self.response.out.write(
38+
'The request did not complete in time.')
39+
# [END request_timer]
40+
41+
42+
# [START environment]
43+
class PrintEnvironmentHandler(webapp2.RequestHandler):
44+
def get(self):
45+
self.response.headers['Content-Type'] = 'text/plain'
46+
for key, value in os.environ.iteritems():
47+
self.response.out.write(
48+
"{} = {}\n".format(key, value))
49+
# [END environment]
50+
51+
52+
# [START request_ids]
53+
class RequestIdHandler(webapp2.RequestHandler):
54+
def get(self):
55+
self.response.headers['Content-Type'] = 'text/plain'
56+
request_id = os.environ.get('REQUEST_LOG_ID')
57+
self.response.write(
58+
'REQUEST_LOG_ID={}'.format(request_id))
59+
# [END request_ids]
60+
61+
62+
app = webapp2.WSGIApplication([
63+
('/timer', TimerHandler),
64+
('/environment', PrintEnvironmentHandler),
65+
('/requestid', RequestIdHandler)
66+
], debug=True)

appengine/requests/main_test.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
from google.appengine.runtime import DeadlineExceededError
18+
import main
19+
import mock
20+
import webtest
21+
22+
23+
def test_timer(testbed):
24+
app = webtest.TestApp(main.app)
25+
26+
with mock.patch('main.time.sleep') as sleep_mock:
27+
sleep_mock.side_effect = DeadlineExceededError()
28+
app.get('/timer', status=500)
29+
assert sleep_mock.called
30+
31+
32+
def test_environment(testbed):
33+
app = webtest.TestApp(main.app)
34+
response = app.get('/environment')
35+
assert response.headers['Content-Type'] == 'text/plain'
36+
assert response.body
37+
38+
39+
def test_request_id(testbed):
40+
app = webtest.TestApp(main.app)
41+
os.environ['REQUEST_LOG_ID'] = '1234'
42+
response = app.get('/requestid')
43+
assert response.headers['Content-Type'] == 'text/plain'
44+
assert '1234' in response.body

0 commit comments

Comments
 (0)