test_http.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. #
  3. """
  4. websocket - WebSocket client library for Python
  5. Copyright (C) 2010 Hiroki Ohtani(liris)
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. """
  18. import os
  19. import os.path
  20. import websocket as ws
  21. from websocket._http import proxy_info, read_headers, _open_proxied_socket, _tunnel
  22. import sys
  23. sys.path[0:0] = [""]
  24. if sys.version_info[0] == 2 and sys.version_info[1] < 7:
  25. import unittest2 as unittest
  26. else:
  27. import unittest
  28. class SockMock(object):
  29. def __init__(self):
  30. self.data = []
  31. self.sent = []
  32. def add_packet(self, data):
  33. self.data.append(data)
  34. def gettimeout(self):
  35. return None
  36. def recv(self, bufsize):
  37. if self.data:
  38. e = self.data.pop(0)
  39. if isinstance(e, Exception):
  40. raise e
  41. if len(e) > bufsize:
  42. self.data.insert(0, e[bufsize:])
  43. return e[:bufsize]
  44. def send(self, data):
  45. self.sent.append(data)
  46. return len(data)
  47. def close(self):
  48. pass
  49. class HeaderSockMock(SockMock):
  50. def __init__(self, fname):
  51. SockMock.__init__(self)
  52. import yatest.common
  53. path = yatest.common.source_path(os.path.join('contrib/python/websocket-client/py2/websocket/tests', fname))
  54. with open(path, "rb") as f:
  55. self.add_packet(f.read())
  56. class OptsList():
  57. def __init__(self):
  58. self.timeout = 0
  59. self.sockopt = []
  60. class HttpTest(unittest.TestCase):
  61. def testReadHeader(self):
  62. status, header, status_message = read_headers(HeaderSockMock("data/header01.txt"))
  63. self.assertEqual(status, 101)
  64. self.assertEqual(header["connection"], "Upgrade")
  65. # header02.txt is intentionally malformed
  66. self.assertRaises(ws.WebSocketException, read_headers, HeaderSockMock("data/header02.txt"))
  67. def testTunnel(self):
  68. self.assertRaises(ws.WebSocketProxyException, _tunnel, HeaderSockMock("data/header01.txt"), "example.com", 80, ("username", "password"))
  69. self.assertRaises(ws.WebSocketProxyException, _tunnel, HeaderSockMock("data/header02.txt"), "example.com", 80, ("username", "password"))
  70. def _testConnect(self):
  71. # Not currently testing an actual proxy connection, so just check whether TypeError is raised
  72. self.assertRaises(TypeError, _open_proxied_socket, "wss://example.com", OptsList(), proxy_info(http_proxy_host="example.com", http_proxy_port="8080", proxy_type="http"))
  73. self.assertRaises(TypeError, _open_proxied_socket, "wss://example.com", OptsList(), proxy_info(http_proxy_host="example.com", http_proxy_port="8080", proxy_type="socks4"))
  74. self.assertRaises(TypeError, _open_proxied_socket, "wss://example.com", OptsList(), proxy_info(http_proxy_host="example.com", http_proxy_port="8080", proxy_type="socks5h"))
  75. def testProxyInfo(self):
  76. self.assertEqual(proxy_info(http_proxy_host="127.0.0.1", http_proxy_port="8080", proxy_type="http").type, "http")
  77. self.assertRaises(ValueError, proxy_info, http_proxy_host="127.0.0.1", http_proxy_port="8080", proxy_type="badval")
  78. self.assertEqual(proxy_info(http_proxy_host="example.com", http_proxy_port="8080", proxy_type="http").host, "example.com")
  79. self.assertEqual(proxy_info(http_proxy_host="127.0.0.1", http_proxy_port="8080", proxy_type="http").port, "8080")
  80. self.assertEqual(proxy_info(http_proxy_host="127.0.0.1", http_proxy_port="8080", proxy_type="http").auth, None)
  81. if __name__ == "__main__":
  82. unittest.main()