Debian Codegraph @

tests/compliance.py

raw symbols SCIP
1 # 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
15 import http.client
16
17 import flask
18 from google.auth import exceptions
19 import pytest
20 from pytest_localserver.http import WSGIServer
21
22 # .invalid will never resolve, see https://tools.ietf.org/html/rfc2606
23 NXDOMAIN = "test.invalid"
24
25
26 class RequestResponseTests(object):
27 @pytest.fixture(scope="module")
28 def server(self):
29 """Provides a test HTTP server.
30
31 The test server is automatically created before
32 a test and destroyed at the end. The server is serving a test
33 application that can be used to verify requests.
34 """
35 app = flask.Flask(__name__)
36 app.debug = True
37
38 # pylint: disable=unused-variable
39 # (pylint thinks the flask routes are unusued.)
40 @app.route("/basic")
41 def index():
42 header_value = flask.request.headers.get("x-test-header", "value")
43 headers = {"X-Test-Header": header_value}
44 return "Basic Content", http.client.OK, headers
45
46 @app.route("/server_error")
47 def server_error():
48 return "Error", http.client.INTERNAL_SERVER_ERROR
49
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",
77 method="GET",
78 headers={"x-test-header": "hello world"},
79 )
80
81 assert response.status == http.client.OK
82 assert response.headers["x-test-header"] == "hello world"
83 assert response.data == b"Basic Content"
84
85 def test_request_error(self, server):
86 request = self.make_request()
87 response = request(url=server.url + "/server_error", method="GET")
88
89 assert response.status == http.client.INTERNAL_SERVER_ERROR
90 assert response.data == b"Error"
91
92 def test_connection_error(self):
93 request = self.make_request()
94 with pytest.raises(exceptions.TransportError):
95 request(url="http://{}".format(NXDOMAIN), method="GET")