blob: 6d2a304521576a2dfcafe783b340d0dfc5b1fe9a [file] [log] [blame]
Jon Wayne Parrott01782462016-10-07 14:05:31 -07001# Copyright 2016 Google Inc.
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
15import flask
16import pytest
17from pytest_localserver.http import WSGIServer
18from six.moves import http_client
19
20from google.auth import exceptions
21
22# .invalid will never resolve, see https://tools.ietf.org/html/rfc2606
23NXDOMAIN = 'test.invalid'
24
25
26class RequestResponseTests(object):
27
Jon Wayne Parrott92c7b942016-10-10 12:57:18 -070028 @pytest.fixture(scope='module')
Jon Wayne Parrott01782462016-10-07 14:05:31 -070029 def server(self):
30 """Provides a test HTTP server.
31
32 The test server is automatically created before
33 a test and destroyed at the end. The server is serving a test
34 application that can be used to verify requests.
35 """
36 app = flask.Flask(__name__)
37 app.debug = True
38
39 # pylint: disable=unused-variable
40 # (pylint thinks the flask routes are unusued.)
41 @app.route('/basic')
42 def index():
43 header_value = flask.request.headers.get('x-test-header', 'value')
44 headers = {'X-Test-Header': header_value}
45 return 'Basic Content', http_client.OK, headers
46
47 @app.route('/server_error')
48 def server_error():
49 return 'Error', http_client.INTERNAL_SERVER_ERROR
50 # pylint: enable=unused-variable
51
52 server = WSGIServer(application=app.wsgi_app)
53 server.start()
54 yield server
55 server.stop()
56
57 def test_request_basic(self, server):
58 request = self.make_request()
59 response = request(url=server.url + '/basic', method='GET')
60
61 assert response.status == http_client.OK
62 assert response.headers['x-test-header'] == 'value'
63 assert response.data == b'Basic Content'
64
65 def test_request_timeout(self, server):
66 request = self.make_request()
67 response = request(url=server.url + '/basic', method='GET', timeout=2)
68
69 assert response.status == http_client.OK
70 assert response.headers['x-test-header'] == 'value'
71 assert response.data == b'Basic Content'
72
73 def test_request_headers(self, server):
74 request = self.make_request()
75 response = request(
76 url=server.url + '/basic', method='GET', headers={
77 'x-test-header': 'hello world'})
78
79 assert response.status == http_client.OK
80 assert response.headers['x-test-header'] == 'hello world'
81 assert response.data == b'Basic Content'
82
83 def test_request_error(self, server):
84 request = self.make_request()
85 response = request(url=server.url + '/server_error', method='GET')
86
87 assert response.status == http_client.INTERNAL_SERVER_ERROR
88 assert response.data == b'Error'
89
90 def test_connection_error(self):
91 request = self.make_request()
92 with pytest.raises(exceptions.TransportError):
93 request(url='http://{}'.format(NXDOMAIN), method='GET')