You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2017 Marcos Pereira <marcospereira.mpj@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program 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
  13. # GNU Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. from httmock import urlmatch, response, HTTMock, all_requests
  18. from ..connection import ConnectionManager
  19. try:
  20. import unittest
  21. except ImportError:
  22. import unittest2 as unittest
  23. class TestConnection(unittest.TestCase):
  24. def setUp(self):
  25. self._conn = ConnectionManager(
  26. base_url="http://localhost/",
  27. headers={},
  28. timeout=60)
  29. @all_requests
  30. def response_content_success(self, url, request):
  31. headers = {'content-type': 'application/json'}
  32. content = b'response_ok'
  33. return response(200, content, headers, None, 5, request)
  34. def test_raw_get(self):
  35. with HTTMock(self.response_content_success):
  36. resp = self._conn.raw_get("/known_path")
  37. self.assertEqual(resp.content, b'response_ok')
  38. self.assertEqual(resp.status_code, 200)
  39. def test_raw_post(self):
  40. @urlmatch(path="/known_path", method="post")
  41. def response_post_success(url, request):
  42. headers = {'content-type': 'application/json'}
  43. content = 'response'.encode("utf-8")
  44. return response(201, content, headers, None, 5, request)
  45. with HTTMock(response_post_success):
  46. resp = self._conn.raw_post("/known_path",
  47. {'field': 'value'})
  48. self.assertEqual(resp.content, b'response')
  49. self.assertEqual(resp.status_code, 201)
  50. def test_raw_put(self):
  51. @urlmatch(netloc="localhost", path="/known_path", method="put")
  52. def response_put_success(url, request):
  53. headers = {'content-type': 'application/json'}
  54. content = 'response'.encode("utf-8")
  55. return response(200, content, headers, None, 5, request)
  56. with HTTMock(response_put_success):
  57. resp = self._conn.raw_put("/known_path",
  58. {'field': 'value'})
  59. self.assertEqual(resp.content, b'response')
  60. self.assertEqual(resp.status_code, 200)
  61. def test_raw_get_fail(self):
  62. @urlmatch(netloc="localhost", path="/known_path", method="get")
  63. def response_get_fail(url, request):
  64. headers = {'content-type': 'application/json'}
  65. content = "404 page not found".encode("utf-8")
  66. return response(404, content, headers, None, 5, request)
  67. with HTTMock(response_get_fail):
  68. resp = self._conn.raw_get("/known_path")
  69. self.assertEqual(resp.content, b"404 page not found")
  70. self.assertEqual(resp.status_code, 404)
  71. def test_raw_post_fail(self):
  72. @urlmatch(netloc="localhost", path="/known_path", method="post")
  73. def response_post_fail(url, request):
  74. headers = {'content-type': 'application/json'}
  75. content = str(["Start can't be blank"]).encode("utf-8")
  76. return response(404, content, headers, None, 5, request)
  77. with HTTMock(response_post_fail):
  78. resp = self._conn.raw_post("/known_path",
  79. {'field': 'value'})
  80. self.assertEqual(resp.content, str(["Start can't be blank"]).encode("utf-8"))
  81. self.assertEqual(resp.status_code, 404)
  82. def test_raw_put_fail(self):
  83. @urlmatch(netloc="localhost", path="/known_path", method="put")
  84. def response_put_fail(url, request):
  85. headers = {'content-type': 'application/json'}
  86. content = str(["Start can't be blank"]).encode("utf-8")
  87. return response(404, content, headers, None, 5, request)
  88. with HTTMock(response_put_fail):
  89. resp = self._conn.raw_put("/known_path",
  90. {'field': 'value'})
  91. self.assertEqual(resp.content, str(["Start can't be blank"]).encode("utf-8"))
  92. self.assertEqual(resp.status_code, 404)
  93. def test_add_param_headers(self):
  94. self._conn.add_param_headers("test", "value")
  95. self.assertEqual(self._conn.headers,
  96. {"test": "value"})
  97. def test_del_param_headers(self):
  98. self._conn.add_param_headers("test", "value")
  99. self._conn.del_param_headers("test")
  100. self.assertEqual(self._conn.headers, {})
  101. def test_clean_param_headers(self):
  102. self._conn.add_param_headers("test", "value")
  103. self.assertEqual(self._conn.headers,
  104. {"test": "value"})
  105. self._conn.clean_headers()
  106. self.assertEqual(self._conn.headers, {})
  107. def test_exist_param_headers(self):
  108. self._conn.add_param_headers("test", "value")
  109. self.assertTrue(self._conn.exist_param_headers("test"))
  110. self.assertFalse(self._conn.exist_param_headers("test_no"))
  111. def test_get_param_headers(self):
  112. self._conn.add_param_headers("test", "value")
  113. self.assertTrue(self._conn.exist_param_headers("test"))
  114. self.assertFalse(self._conn.exist_param_headers("test_no"))
  115. def test_get_headers(self):
  116. self._conn.add_param_headers("test", "value")
  117. self.assertEqual(self._conn.headers,
  118. {"test": "value"})