test_http.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import pytest
  2. from core.app.entities.app_invoke_entities import InvokeFrom
  3. from core.workflow.entities.variable_pool import VariablePool
  4. from core.workflow.nodes.http_request.http_request_node import HttpRequestNode
  5. from tests.integration_tests.workflow.nodes.__mock.http import setup_http_mock
  6. BASIC_NODE_DATA = {
  7. 'tenant_id': '1',
  8. 'app_id': '1',
  9. 'workflow_id': '1',
  10. 'user_id': '1',
  11. 'user_from': InvokeFrom.WEB_APP,
  12. }
  13. # construct variable pool
  14. pool = VariablePool(system_variables={}, user_inputs={})
  15. pool.append_variable(node_id='a', variable_key_list=['b123', 'args1'], value=1)
  16. pool.append_variable(node_id='a', variable_key_list=['b123', 'args2'], value=2)
  17. @pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
  18. def test_get(setup_http_mock):
  19. node = HttpRequestNode(config={
  20. 'id': '1',
  21. 'data': {
  22. 'title': 'http',
  23. 'desc': '',
  24. 'method': 'get',
  25. 'url': 'http://example.com',
  26. 'authorization': {
  27. 'type': 'api-key',
  28. 'config': {
  29. 'type': 'basic',
  30. 'api_key':'ak-xxx',
  31. 'header': 'api-key',
  32. }
  33. },
  34. 'headers': 'X-Header:123',
  35. 'params': 'A:b',
  36. 'body': None,
  37. }
  38. }, **BASIC_NODE_DATA)
  39. result = node.run(pool)
  40. data = result.process_data.get('request', '')
  41. assert '?A=b' in data
  42. assert 'api-key: Basic ak-xxx' in data
  43. assert 'X-Header: 123' in data
  44. @pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
  45. def test_no_auth(setup_http_mock):
  46. node = HttpRequestNode(config={
  47. 'id': '1',
  48. 'data': {
  49. 'title': 'http',
  50. 'desc': '',
  51. 'method': 'get',
  52. 'url': 'http://example.com',
  53. 'authorization': {
  54. 'type': 'no-auth',
  55. 'config': None,
  56. },
  57. 'headers': 'X-Header:123',
  58. 'params': 'A:b',
  59. 'body': None,
  60. }
  61. }, **BASIC_NODE_DATA)
  62. result = node.run(pool)
  63. data = result.process_data.get('request', '')
  64. assert '?A=b' in data
  65. assert 'X-Header: 123' in data
  66. @pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
  67. def test_custom_authorization_header(setup_http_mock):
  68. node = HttpRequestNode(config={
  69. 'id': '1',
  70. 'data': {
  71. 'title': 'http',
  72. 'desc': '',
  73. 'method': 'get',
  74. 'url': 'http://example.com',
  75. 'authorization': {
  76. 'type': 'api-key',
  77. 'config': {
  78. 'type': 'custom',
  79. 'api_key': 'Auth',
  80. 'header': 'X-Auth',
  81. },
  82. },
  83. 'headers': 'X-Header:123',
  84. 'params': 'A:b',
  85. 'body': None,
  86. }
  87. }, **BASIC_NODE_DATA)
  88. result = node.run(pool)
  89. data = result.process_data.get('request', '')
  90. assert '?A=b' in data
  91. assert 'X-Header: 123' in data
  92. assert 'X-Auth: Auth' in data
  93. @pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
  94. def test_template(setup_http_mock):
  95. node = HttpRequestNode(config={
  96. 'id': '1',
  97. 'data': {
  98. 'title': 'http',
  99. 'desc': '',
  100. 'method': 'get',
  101. 'url': 'http://example.com/{{#a.b123.args2#}}',
  102. 'authorization': {
  103. 'type': 'api-key',
  104. 'config': {
  105. 'type': 'basic',
  106. 'api_key':'ak-xxx',
  107. 'header': 'api-key',
  108. }
  109. },
  110. 'headers': 'X-Header:123\nX-Header2:{{#a.b123.args2#}}',
  111. 'params': 'A:b\nTemplate:{{#a.b123.args2#}}',
  112. 'body': None,
  113. }
  114. }, **BASIC_NODE_DATA)
  115. result = node.run(pool)
  116. data = result.process_data.get('request', '')
  117. assert '?A=b' in data
  118. assert 'Template=2' in data
  119. assert 'api-key: Basic ak-xxx' in data
  120. assert 'X-Header: 123' in data
  121. assert 'X-Header2: 2' in data
  122. @pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
  123. def test_json(setup_http_mock):
  124. node = HttpRequestNode(config={
  125. 'id': '1',
  126. 'data': {
  127. 'title': 'http',
  128. 'desc': '',
  129. 'method': 'post',
  130. 'url': 'http://example.com',
  131. 'authorization': {
  132. 'type': 'api-key',
  133. 'config': {
  134. 'type': 'basic',
  135. 'api_key':'ak-xxx',
  136. 'header': 'api-key',
  137. }
  138. },
  139. 'headers': 'X-Header:123',
  140. 'params': 'A:b',
  141. 'body': {
  142. 'type': 'json',
  143. 'data': '{"a": "{{#a.b123.args1#}}"}'
  144. },
  145. }
  146. }, **BASIC_NODE_DATA)
  147. result = node.run(pool)
  148. data = result.process_data.get('request', '')
  149. assert '{"a": "1"}' in data
  150. assert 'api-key: Basic ak-xxx' in data
  151. assert 'X-Header: 123' in data
  152. def test_x_www_form_urlencoded(setup_http_mock):
  153. node = HttpRequestNode(config={
  154. 'id': '1',
  155. 'data': {
  156. 'title': 'http',
  157. 'desc': '',
  158. 'method': 'post',
  159. 'url': 'http://example.com',
  160. 'authorization': {
  161. 'type': 'api-key',
  162. 'config': {
  163. 'type': 'basic',
  164. 'api_key':'ak-xxx',
  165. 'header': 'api-key',
  166. }
  167. },
  168. 'headers': 'X-Header:123',
  169. 'params': 'A:b',
  170. 'body': {
  171. 'type': 'x-www-form-urlencoded',
  172. 'data': 'a:{{#a.b123.args1#}}\nb:{{#a.b123.args2#}}'
  173. },
  174. }
  175. }, **BASIC_NODE_DATA)
  176. result = node.run(pool)
  177. data = result.process_data.get('request', '')
  178. assert 'a=1&b=2' in data
  179. assert 'api-key: Basic ak-xxx' in data
  180. assert 'X-Header: 123' in data
  181. def test_form_data(setup_http_mock):
  182. node = HttpRequestNode(config={
  183. 'id': '1',
  184. 'data': {
  185. 'title': 'http',
  186. 'desc': '',
  187. 'method': 'post',
  188. 'url': 'http://example.com',
  189. 'authorization': {
  190. 'type': 'api-key',
  191. 'config': {
  192. 'type': 'basic',
  193. 'api_key':'ak-xxx',
  194. 'header': 'api-key',
  195. }
  196. },
  197. 'headers': 'X-Header:123',
  198. 'params': 'A:b',
  199. 'body': {
  200. 'type': 'form-data',
  201. 'data': 'a:{{#a.b123.args1#}}\nb:{{#a.b123.args2#}}'
  202. },
  203. }
  204. }, **BASIC_NODE_DATA)
  205. result = node.run(pool)
  206. data = result.process_data.get('request', '')
  207. assert 'form-data; name="a"' in data
  208. assert '1' in data
  209. assert 'form-data; name="b"' in data
  210. assert '2' in data
  211. assert 'api-key: Basic ak-xxx' in data
  212. assert 'X-Header: 123' in data
  213. def test_none_data(setup_http_mock):
  214. node = HttpRequestNode(config={
  215. 'id': '1',
  216. 'data': {
  217. 'title': 'http',
  218. 'desc': '',
  219. 'method': 'post',
  220. 'url': 'http://example.com',
  221. 'authorization': {
  222. 'type': 'api-key',
  223. 'config': {
  224. 'type': 'basic',
  225. 'api_key':'ak-xxx',
  226. 'header': 'api-key',
  227. }
  228. },
  229. 'headers': 'X-Header:123',
  230. 'params': 'A:b',
  231. 'body': {
  232. 'type': 'none',
  233. 'data': '123123123'
  234. },
  235. }
  236. }, **BASIC_NODE_DATA)
  237. result = node.run(pool)
  238. data = result.process_data.get('request', '')
  239. assert 'api-key: Basic ak-xxx' in data
  240. assert 'X-Header: 123' in data
  241. assert '123123123' not in data