PageRenderTime 79ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/telemetry/third_party/webpagereplay/httpclient_test.py

https://gitlab.com/jonnialva90/iridium-browser
Python | 185 lines | 121 code | 34 blank | 30 comment | 3 complexity | cb8b6a5717d96afc6678ca48273123e1 MD5 | raw file
  1. #!/usr/bin/env python
  2. # Copyright 2012 Google Inc. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import unittest
  16. import httpclient
  17. import platformsettings
  18. class RealHttpFetchTest(unittest.TestCase):
  19. # Initialize test data
  20. CONTENT_TYPE = 'content-type: image/x-icon'
  21. COOKIE_1 = ('Set-Cookie: GMAIL_IMP=EXPIRED; '
  22. 'Expires=Thu, 12-Jul-2012 22:41:22 GMT; '
  23. 'Path=/mail; Secure')
  24. COOKIE_2 = ('Set-Cookie: GMAIL_STAT_205a=EXPIRED; '
  25. 'Expires=Thu, 12-Jul-2012 22:42:24 GMT; '
  26. 'Path=/mail; Secure')
  27. FIRST_LINE = 'fake-header: first line'
  28. SECOND_LINE = ' second line'
  29. THIRD_LINE = '\tthird line'
  30. BAD_HEADER = 'this is a bad header'
  31. def test__GetHeaderNameValueBasic(self):
  32. """Test _GetHeaderNameValue with normal header."""
  33. real_http_fetch = httpclient.RealHttpFetch
  34. name_value = real_http_fetch._GetHeaderNameValue(self.CONTENT_TYPE)
  35. self.assertEqual(name_value, ('content-type', 'image/x-icon'))
  36. def test__GetHeaderNameValueLowercasesName(self):
  37. """_GetHeaderNameValue lowercases header name."""
  38. real_http_fetch = httpclient.RealHttpFetch
  39. header = 'X-Google-Gfe-Backend-Request-Info: eid=1KMAUMeiK4eMiAL52YyMBg'
  40. expected = ('x-google-gfe-backend-request-info',
  41. 'eid=1KMAUMeiK4eMiAL52YyMBg')
  42. name_value = real_http_fetch._GetHeaderNameValue(header)
  43. self.assertEqual(name_value, expected)
  44. def test__GetHeaderNameValueBadLineGivesNone(self):
  45. """_GetHeaderNameValue returns None for a header in wrong format."""
  46. real_http_fetch = httpclient.RealHttpFetch
  47. name_value = real_http_fetch._GetHeaderNameValue(self.BAD_HEADER)
  48. self.assertIsNone(name_value)
  49. def test__ToTuplesBasic(self):
  50. """Test _ToTuples with normal input."""
  51. real_http_fetch = httpclient.RealHttpFetch
  52. headers = [self.CONTENT_TYPE, self.COOKIE_1, self.FIRST_LINE]
  53. result = real_http_fetch._ToTuples(headers)
  54. expected = [('content-type', 'image/x-icon'),
  55. ('set-cookie', self.COOKIE_1[12:]),
  56. ('fake-header', 'first line')]
  57. self.assertEqual(result, expected)
  58. def test__ToTuplesMultipleHeadersWithSameName(self):
  59. """Test mulitple headers with the same name."""
  60. real_http_fetch = httpclient.RealHttpFetch
  61. headers = [self.CONTENT_TYPE, self.COOKIE_1, self.COOKIE_2, self.FIRST_LINE]
  62. result = real_http_fetch._ToTuples(headers)
  63. expected = [('content-type', 'image/x-icon'),
  64. ('set-cookie', self.COOKIE_1[12:]),
  65. ('set-cookie', self.COOKIE_2[12:]),
  66. ('fake-header', 'first line')]
  67. self.assertEqual(result, expected)
  68. def test__ToTuplesAppendsContinuationLine(self):
  69. """Test continuation line is handled."""
  70. real_http_fetch = httpclient.RealHttpFetch
  71. headers = [self.CONTENT_TYPE, self.COOKIE_1, self.FIRST_LINE,
  72. self.SECOND_LINE, self.THIRD_LINE]
  73. result = real_http_fetch._ToTuples(headers)
  74. expected = [('content-type', 'image/x-icon'),
  75. ('set-cookie', self.COOKIE_1[12:]),
  76. ('fake-header', 'first line\n second line\n third line')]
  77. self.assertEqual(result, expected)
  78. def test__ToTuplesIgnoresBadHeader(self):
  79. """Test bad header is ignored."""
  80. real_http_fetch = httpclient.RealHttpFetch
  81. bad_headers = [self.CONTENT_TYPE, self.BAD_HEADER, self.COOKIE_1]
  82. expected = [('content-type', 'image/x-icon'),
  83. ('set-cookie', self.COOKIE_1[12:])]
  84. result = real_http_fetch._ToTuples(bad_headers)
  85. self.assertEqual(result, expected)
  86. def test__ToTuplesIgnoresMisplacedContinuationLine(self):
  87. """Test misplaced continuation line is ignored."""
  88. real_http_fetch = httpclient.RealHttpFetch
  89. misplaced_headers = [self.THIRD_LINE, self.CONTENT_TYPE,
  90. self.COOKIE_1, self.FIRST_LINE, self.SECOND_LINE]
  91. result = real_http_fetch._ToTuples(misplaced_headers)
  92. expected = [('content-type', 'image/x-icon'),
  93. ('set-cookie', self.COOKIE_1[12:]),
  94. ('fake-header', 'first line\n second line')]
  95. self.assertEqual(result, expected)
  96. class RealHttpFetchGetConnectionTest(unittest.TestCase):
  97. """Test that a connection is made with request IP/port or proxy IP/port."""
  98. def setUp(self):
  99. def real_dns_lookup(host):
  100. return {
  101. 'example.com': '127.127.127.127',
  102. 'proxy.com': '2.2.2.2',
  103. }[host]
  104. self.fetch = httpclient.RealHttpFetch(real_dns_lookup)
  105. self.https_proxy = None
  106. self.http_proxy = None
  107. def get_proxy(is_ssl):
  108. return self.https_proxy if is_ssl else self.http_proxy
  109. self.fetch._get_system_proxy = get_proxy
  110. def set_http_proxy(self, host, port):
  111. self.http_proxy = platformsettings.SystemProxy(host, port)
  112. def set_https_proxy(self, host, port):
  113. self.https_proxy = platformsettings.SystemProxy(host, port)
  114. def test_get_connection_without_proxy_connects_to_host_ip(self):
  115. """HTTP connection with no proxy connects to host IP."""
  116. self.set_http_proxy(host=None, port=None)
  117. connection = self.fetch._get_connection('example.com', None, is_ssl=False)
  118. self.assertEqual('127.127.127.127', connection.host)
  119. self.assertEqual(80, connection.port) # default HTTP port
  120. def test_get_connection_without_proxy_uses_nondefault_request_port(self):
  121. """HTTP connection with no proxy connects with request port."""
  122. self.set_https_proxy(host=None, port=None)
  123. connection = self.fetch._get_connection('example.com', 8888, is_ssl=False)
  124. self.assertEqual('127.127.127.127', connection.host)
  125. self.assertEqual(8888, connection.port) # request HTTP port
  126. def test_get_connection_with_proxy_uses_proxy_port(self):
  127. """HTTP connection with proxy connects used proxy port."""
  128. self.set_http_proxy(host='proxy.com', port=None)
  129. connection = self.fetch._get_connection('example.com', 8888, is_ssl=False)
  130. self.assertEqual('2.2.2.2', connection.host) # proxy IP
  131. self.assertEqual(80, connection.port) # proxy port (default HTTP)
  132. def test_ssl_get_connection_without_proxy_connects_to_host_ip(self):
  133. """HTTPS (SSL) connection with no proxy connects to host IP."""
  134. self.set_https_proxy(host=None, port=None)
  135. connection = self.fetch._get_connection('example.com', None, is_ssl=True)
  136. self.assertEqual('127.127.127.127', connection.host)
  137. self.assertEqual(443, connection.port) # default SSL port
  138. def test_ssl_get_connection_with_proxy_connects_to_proxy_ip(self):
  139. """HTTPS (SSL) connection with proxy connects to proxy IP."""
  140. self.set_https_proxy(host='proxy.com', port=8443)
  141. connection = self.fetch._get_connection('example.com', None, is_ssl=True)
  142. self.assertEqual('2.2.2.2', connection.host) # proxy IP
  143. self.assertEqual(8443, connection.port) # SSL proxy port
  144. def test_ssl_get_connection_with_proxy_tunnels_to_host(self):
  145. """HTTPS (SSL) connection with proxy tunnels to target host."""
  146. self.set_https_proxy(host='proxy.com', port=8443)
  147. connection = self.fetch._get_connection('example.com', None, is_ssl=True)
  148. self.assertEqual('example.com', connection._tunnel_host) # host name
  149. self.assertEqual(None, connection._tunnel_port) # host port
  150. if __name__ == '__main__':
  151. unittest.main()