compliance.py 3.7 KB

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