test_uri_neuron.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import json
  2. import unittest
  3. from httpretty import httpretty
  4. from core.NeuronModule import InvalidParameterException
  5. from neurons.uri.uri import Uri
  6. class TestUri(unittest.TestCase):
  7. def setUp(self):
  8. self.test_url = "http://kalliope.fr/voices/"
  9. def testGet(self):
  10. expected_content = '{"voice": "nico"}'
  11. httpretty.enable()
  12. httpretty.register_uri(httpretty.GET, self.test_url, body=expected_content)
  13. parameters = {
  14. "url": self.test_url
  15. }
  16. uri_neuron = Uri(**parameters)
  17. self.assertEqual(uri_neuron.text, expected_content)
  18. def testGetRaw(self):
  19. expected_content = 'raw line'
  20. httpretty.enable()
  21. httpretty.register_uri(httpretty.GET, self.test_url, body=expected_content)
  22. parameters = {
  23. "url": self.test_url
  24. }
  25. uri_neuron = Uri(**parameters)
  26. self.assertEqual(uri_neuron.content, expected_content)
  27. def testPost(self):
  28. expected_content = '{"voice": "nico"}'
  29. httpretty.enable()
  30. httpretty.register_uri(httpretty.POST, self.test_url, body=expected_content)
  31. parameters = {
  32. "url": self.test_url,
  33. "method": "POST"
  34. }
  35. uri_neuron = Uri(**parameters)
  36. self.assertEqual(uri_neuron.text, expected_content)
  37. def testPut(self):
  38. expected_content = '{"voice": "nico"}'
  39. httpretty.enable()
  40. httpretty.register_uri(httpretty.PUT, self.test_url, body=expected_content)
  41. parameters = {
  42. "url": self.test_url,
  43. "method": "PUT"
  44. }
  45. uri_neuron = Uri(**parameters)
  46. self.assertEqual(uri_neuron.text, expected_content)
  47. def testDelete(self):
  48. expected_content = '{"voice": "nico"}'
  49. httpretty.enable()
  50. httpretty.register_uri(httpretty.DELETE, self.test_url, body=expected_content)
  51. parameters = {
  52. "url": self.test_url,
  53. "method": "DELETE"
  54. }
  55. uri_neuron = Uri(**parameters)
  56. self.assertEqual(uri_neuron.text, expected_content)
  57. def testOptions(self):
  58. expected_content = '{"voice": "nico"}'
  59. httpretty.enable()
  60. httpretty.register_uri(httpretty.OPTIONS, self.test_url, body=expected_content)
  61. parameters = {
  62. "url": self.test_url,
  63. "method": "OPTIONS"
  64. }
  65. uri_neuron = Uri(**parameters)
  66. self.assertEqual(uri_neuron.text, expected_content)
  67. def testHead(self):
  68. expected_content = '{"voice": "nico"}'
  69. httpretty.enable()
  70. httpretty.register_uri(httpretty.HEAD, self.test_url, body=expected_content)
  71. parameters = {
  72. "url": self.test_url,
  73. "method": "HEAD"
  74. }
  75. uri_neuron = Uri(**parameters)
  76. self.assertEqual(uri_neuron.status_code, 200)
  77. def testPatch(self):
  78. expected_content = '{"voice": "nico"}'
  79. httpretty.enable()
  80. httpretty.register_uri(httpretty.PATCH, self.test_url, body=expected_content)
  81. parameters = {
  82. "url": self.test_url,
  83. "method": "PATCH"
  84. }
  85. uri_neuron = Uri(**parameters)
  86. self.assertEqual(uri_neuron.text, expected_content)
  87. def testParameters(self):
  88. def run_test(parameters_to_test):
  89. with self.assertRaises(InvalidParameterException):
  90. Uri(**parameters_to_test)
  91. parameters = dict()
  92. run_test(parameters)
  93. parameters = {
  94. "url": self.test_url,
  95. "headers": 1
  96. }
  97. run_test(parameters)
  98. parameters = {
  99. "url": self.test_url,
  100. "timeout": "string"
  101. }
  102. run_test(parameters)
  103. parameters = {
  104. "url": self.test_url,
  105. "data": "this is a data",
  106. "data_from_file": "this is another data"
  107. }
  108. run_test(parameters)
  109. parameters = {
  110. "url": self.test_url,
  111. "method": "NONEXIST"
  112. }
  113. run_test(parameters)
  114. def testPostJsonFromFile(self):
  115. """
  116. Test that we are able to send json data through a file
  117. :return:
  118. """
  119. def request_callback(request, url, headers):
  120. data = json.loads(request.body)
  121. if "title" in data and "body" in data and "userId" in data:
  122. return 200, headers, "all key received from URL %s" % url
  123. return 400, headers, "server did not receive all keys from URL %s" % url
  124. httpretty.enable()
  125. httpretty.register_uri(httpretty.POST, self.test_url, body=request_callback)
  126. parameters = {
  127. "url": self.test_url,
  128. "method": "POST",
  129. "data_from_file": "data_post_test.json",
  130. "headers": {
  131. "Content-Type": 'application/json'
  132. }
  133. }
  134. uri_neuron = Uri(**parameters)
  135. self.assertEqual(uri_neuron.status_code, 200)
  136. def testPostJson(self):
  137. """
  138. Test that we are able to send json data directly from the data variable
  139. :return:
  140. """
  141. def request_callback(request, url, headers):
  142. data = json.loads(request.body)
  143. if "title" in data and "body" in data and "userId" in data:
  144. return 200, headers, "all key received from URL %s" % url
  145. return 400, headers, "server did not receive all keys from URL %s" % url
  146. httpretty.enable()
  147. httpretty.register_uri(httpretty.POST, self.test_url, body=request_callback)
  148. parameters = {
  149. "url": self.test_url,
  150. "method": "POST",
  151. "data": "{\"id\": 1,\"title\": \"foo\", \"body\": \"bar\", \"userId\": 1}",
  152. "headers": {
  153. "Content-Type": 'application/json'
  154. }
  155. }
  156. uri_neuron = Uri(**parameters)
  157. self.assertEqual(uri_neuron.status_code, 200)
  158. if __name__ == '__main__':
  159. unittest.main()