github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/internal/http_client_test.py (about)

     1  #
     2  # Licensed to the Apache Software Foundation (ASF) under one or more
     3  # contributor license agreements.  See the NOTICE file distributed with
     4  # this work for additional information regarding copyright ownership.
     5  # The ASF licenses this file to You under the Apache License, Version 2.0
     6  # (the "License"); you may not use this file except in compliance with
     7  # the License.  You may obtain a copy of the License at
     8  #
     9  #    http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  #
    17  
    18  """Unit tests for the http_client module."""
    19  # pytype: skip-file
    20  
    21  import os
    22  import unittest
    23  
    24  import mock
    25  from httplib2 import ProxyInfo
    26  
    27  from apache_beam.internal.http_client import DEFAULT_HTTP_TIMEOUT_SECONDS
    28  from apache_beam.internal.http_client import get_new_http
    29  from apache_beam.internal.http_client import proxy_info_from_environment_var
    30  
    31  
    32  class HttpClientTest(unittest.TestCase):
    33    def test_proxy_from_env_http_with_port(self):
    34      with mock.patch.dict(os.environ, http_proxy='http://localhost:9000'):
    35        proxy_info = proxy_info_from_environment_var('http_proxy')
    36        expected = ProxyInfo(3, 'localhost', 9000)
    37        self.assertEqual(str(expected), str(proxy_info))
    38  
    39    def test_proxy_from_env_https_with_port(self):
    40      with mock.patch.dict(os.environ, https_proxy='https://localhost:9000'):
    41        proxy_info = proxy_info_from_environment_var('https_proxy')
    42        expected = ProxyInfo(3, 'localhost', 9000)
    43        self.assertEqual(str(expected), str(proxy_info))
    44  
    45    def test_proxy_from_env_http_without_port(self):
    46      with mock.patch.dict(os.environ, http_proxy='http://localhost'):
    47        proxy_info = proxy_info_from_environment_var('http_proxy')
    48        expected = ProxyInfo(3, 'localhost', 80)
    49        self.assertEqual(str(expected), str(proxy_info))
    50  
    51    def test_proxy_from_env_https_without_port(self):
    52      with mock.patch.dict(os.environ, https_proxy='https://localhost'):
    53        proxy_info = proxy_info_from_environment_var('https_proxy')
    54        expected = ProxyInfo(3, 'localhost', 443)
    55        self.assertEqual(str(expected), str(proxy_info))
    56  
    57    def test_proxy_from_env_http_without_method(self):
    58      with mock.patch.dict(os.environ, http_proxy='localhost:8000'):
    59        proxy_info = proxy_info_from_environment_var('http_proxy')
    60        expected = ProxyInfo(3, 'localhost', 8000)
    61        self.assertEqual(str(expected), str(proxy_info))
    62  
    63    def test_proxy_from_env_https_without_method(self):
    64      with mock.patch.dict(os.environ, https_proxy='localhost:8000'):
    65        proxy_info = proxy_info_from_environment_var('https_proxy')
    66        expected = ProxyInfo(3, 'localhost', 8000)
    67        self.assertEqual(str(expected), str(proxy_info))
    68  
    69    def test_proxy_from_env_http_without_port_without_method(self):
    70      with mock.patch.dict(os.environ, http_proxy='localhost'):
    71        proxy_info = proxy_info_from_environment_var('http_proxy')
    72        expected = ProxyInfo(3, 'localhost', 80)
    73        self.assertEqual(str(expected), str(proxy_info))
    74  
    75    def test_proxy_from_env_https_without_port_without_method(self):
    76      with mock.patch.dict(os.environ, https_proxy='localhost'):
    77        proxy_info = proxy_info_from_environment_var('https_proxy')
    78        expected = ProxyInfo(3, 'localhost', 443)
    79        self.assertEqual(str(expected), str(proxy_info))
    80  
    81    def test_proxy_from_env_invalid_var(self):
    82      proxy_info = proxy_info_from_environment_var('http_proxy_host')
    83      expected = None
    84      self.assertEqual(str(expected), str(proxy_info))
    85  
    86    def test_proxy_from_env_wrong_method_in_var_name(self):
    87      with mock.patch.dict(os.environ, smtp_proxy='localhost'):
    88        with self.assertRaises(KeyError):
    89          proxy_info_from_environment_var('smtp_proxy')
    90  
    91    def test_proxy_from_env_wrong_method_in_url(self):
    92      with mock.patch.dict(os.environ, http_proxy='smtp://localhost:8000'):
    93        proxy_info = proxy_info_from_environment_var('http_proxy')
    94        expected = ProxyInfo(3, 'smtp', 80)  # wrong proxy info generated
    95        self.assertEqual(str(expected), str(proxy_info))
    96  
    97    def test_get_new_http_proxy_info(self):
    98      with mock.patch.dict(os.environ, http_proxy='localhost'):
    99        http = get_new_http()
   100        expected = ProxyInfo(3, 'localhost', 80)
   101        self.assertEqual(str(http.proxy_info), str(expected))
   102  
   103    def test_get_new_http_timeout(self):
   104      http = get_new_http()
   105      self.assertEqual(http.timeout, DEFAULT_HTTP_TIMEOUT_SECONDS)
   106  
   107  
   108  if __name__ == '__main__':
   109    unittest.main()