test_http.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. from urllib.parse import urlencode
  2. import pytest
  3. from core.app.entities.app_invoke_entities import InvokeFrom
  4. from core.workflow.entities.variable_pool import VariablePool
  5. from core.workflow.nodes.base_node import UserFrom
  6. from core.workflow.nodes.http_request.http_request_node import HttpRequestNode
  7. from tests.integration_tests.workflow.nodes.__mock.http import setup_http_mock
  8. BASIC_NODE_DATA = {
  9. 'tenant_id': '1',
  10. 'app_id': '1',
  11. 'workflow_id': '1',
  12. 'user_id': '1',
  13. 'user_from': UserFrom.ACCOUNT,
  14. 'invoke_from': InvokeFrom.WEB_APP,
  15. }
  16. # construct variable pool
  17. pool = VariablePool(system_variables={}, user_inputs={})
  18. pool.append_variable(node_id='a', variable_key_list=['b123', 'args1'], value=1)
  19. pool.append_variable(node_id='a', variable_key_list=['b123', 'args2'], value=2)
  20. @pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
  21. def test_get(setup_http_mock):
  22. node = HttpRequestNode(config={
  23. 'id': '1',
  24. 'data': {
  25. 'title': 'http',
  26. 'desc': '',
  27. 'method': 'get',
  28. 'url': 'http://example.com',
  29. 'authorization': {
  30. 'type': 'api-key',
  31. 'config': {
  32. 'type': 'basic',
  33. 'api_key': 'ak-xxx',
  34. 'header': 'api-key',
  35. }
  36. },
  37. 'headers': 'X-Header:123',
  38. 'params': 'A:b',
  39. 'body': None,
  40. }
  41. }, **BASIC_NODE_DATA)
  42. result = node.run(pool)
  43. data = result.process_data.get('request', '')
  44. assert '?A=b' in data
  45. assert 'X-Header: 123' in data
  46. @pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
  47. def test_no_auth(setup_http_mock):
  48. node = HttpRequestNode(config={
  49. 'id': '1',
  50. 'data': {
  51. 'title': 'http',
  52. 'desc': '',
  53. 'method': 'get',
  54. 'url': 'http://example.com',
  55. 'authorization': {
  56. 'type': 'no-auth',
  57. 'config': None,
  58. },
  59. 'headers': 'X-Header:123',
  60. 'params': 'A:b',
  61. 'body': None,
  62. }
  63. }, **BASIC_NODE_DATA)
  64. result = node.run(pool)
  65. data = result.process_data.get('request', '')
  66. assert '?A=b' in data
  67. assert 'X-Header: 123' in data
  68. @pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
  69. def test_custom_authorization_header(setup_http_mock):
  70. node = HttpRequestNode(config={
  71. 'id': '1',
  72. 'data': {
  73. 'title': 'http',
  74. 'desc': '',
  75. 'method': 'get',
  76. 'url': 'http://example.com',
  77. 'authorization': {
  78. 'type': 'api-key',
  79. 'config': {
  80. 'type': 'custom',
  81. 'api_key': 'Auth',
  82. 'header': 'X-Auth',
  83. },
  84. },
  85. 'headers': 'X-Header:123',
  86. 'params': 'A:b',
  87. 'body': None,
  88. }
  89. }, **BASIC_NODE_DATA)
  90. result = node.run(pool)
  91. data = result.process_data.get('request', '')
  92. assert '?A=b' in data
  93. assert 'X-Header: 123' in data
  94. @pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
  95. def test_template(setup_http_mock):
  96. node = HttpRequestNode(config={
  97. 'id': '1',
  98. 'data': {
  99. 'title': 'http',
  100. 'desc': '',
  101. 'method': 'get',
  102. 'url': 'http://example.com/{{#a.b123.args2#}}',
  103. 'authorization': {
  104. 'type': 'api-key',
  105. 'config': {
  106. 'type': 'basic',
  107. 'api_key': 'ak-xxx',
  108. 'header': 'api-key',
  109. }
  110. },
  111. 'headers': 'X-Header:123\nX-Header2:{{#a.b123.args2#}}',
  112. 'params': 'A:b\nTemplate:{{#a.b123.args2#}}',
  113. 'body': None,
  114. }
  115. }, **BASIC_NODE_DATA)
  116. result = node.run(pool)
  117. data = result.process_data.get('request', '')
  118. assert '?A=b' in data
  119. assert 'Template=2' 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 'X-Header: 123' in data
  151. def test_x_www_form_urlencoded(setup_http_mock):
  152. node = HttpRequestNode(config={
  153. 'id': '1',
  154. 'data': {
  155. 'title': 'http',
  156. 'desc': '',
  157. 'method': 'post',
  158. 'url': 'http://example.com',
  159. 'authorization': {
  160. 'type': 'api-key',
  161. 'config': {
  162. 'type': 'basic',
  163. 'api_key': 'ak-xxx',
  164. 'header': 'api-key',
  165. }
  166. },
  167. 'headers': 'X-Header:123',
  168. 'params': 'A:b',
  169. 'body': {
  170. 'type': 'x-www-form-urlencoded',
  171. 'data': 'a:{{#a.b123.args1#}}\nb:{{#a.b123.args2#}}'
  172. },
  173. }
  174. }, **BASIC_NODE_DATA)
  175. result = node.run(pool)
  176. data = result.process_data.get('request', '')
  177. assert 'a=1&b=2' in data
  178. assert 'X-Header: 123' in data
  179. def test_form_data(setup_http_mock):
  180. node = HttpRequestNode(config={
  181. 'id': '1',
  182. 'data': {
  183. 'title': 'http',
  184. 'desc': '',
  185. 'method': 'post',
  186. 'url': 'http://example.com',
  187. 'authorization': {
  188. 'type': 'api-key',
  189. 'config': {
  190. 'type': 'basic',
  191. 'api_key': 'ak-xxx',
  192. 'header': 'api-key',
  193. }
  194. },
  195. 'headers': 'X-Header:123',
  196. 'params': 'A:b',
  197. 'body': {
  198. 'type': 'form-data',
  199. 'data': 'a:{{#a.b123.args1#}}\nb:{{#a.b123.args2#}}'
  200. },
  201. }
  202. }, **BASIC_NODE_DATA)
  203. result = node.run(pool)
  204. data = result.process_data.get('request', '')
  205. assert 'form-data; name="a"' in data
  206. assert '1' in data
  207. assert 'form-data; name="b"' in data
  208. assert '2' in data
  209. assert 'X-Header: 123' in data
  210. def test_none_data(setup_http_mock):
  211. node = HttpRequestNode(config={
  212. 'id': '1',
  213. 'data': {
  214. 'title': 'http',
  215. 'desc': '',
  216. 'method': 'post',
  217. 'url': 'http://example.com',
  218. 'authorization': {
  219. 'type': 'api-key',
  220. 'config': {
  221. 'type': 'basic',
  222. 'api_key': 'ak-xxx',
  223. 'header': 'api-key',
  224. }
  225. },
  226. 'headers': 'X-Header:123',
  227. 'params': 'A:b',
  228. 'body': {
  229. 'type': 'none',
  230. 'data': '123123123'
  231. },
  232. }
  233. }, **BASIC_NODE_DATA)
  234. result = node.run(pool)
  235. data = result.process_data.get('request', '')
  236. assert 'X-Header: 123' in data
  237. assert '123123123' not in data
  238. def test_mock_404(setup_http_mock):
  239. node = HttpRequestNode(config={
  240. 'id': '1',
  241. 'data': {
  242. 'title': 'http',
  243. 'desc': '',
  244. 'method': 'get',
  245. 'url': 'http://404.com',
  246. 'authorization': {
  247. 'type': 'no-auth',
  248. 'config': None,
  249. },
  250. 'body': None,
  251. 'params': '',
  252. 'headers': 'X-Header:123',
  253. }
  254. }, **BASIC_NODE_DATA)
  255. result = node.run(pool)
  256. resp = result.outputs
  257. assert 404 == resp.get('status_code')
  258. assert 'Not Found' in resp.get('body')
  259. def test_multi_colons_parse(setup_http_mock):
  260. node = HttpRequestNode(config={
  261. 'id': '1',
  262. 'data': {
  263. 'title': 'http',
  264. 'desc': '',
  265. 'method': 'get',
  266. 'url': 'http://example.com',
  267. 'authorization': {
  268. 'type': 'no-auth',
  269. 'config': None,
  270. },
  271. 'params': 'Referer:http://example1.com\nRedirect:http://example2.com',
  272. 'headers': 'Referer:http://example3.com\nRedirect:http://example4.com',
  273. 'body': {
  274. 'type': 'form-data',
  275. 'data': 'Referer:http://example5.com\nRedirect:http://example6.com'
  276. },
  277. }
  278. }, **BASIC_NODE_DATA)
  279. result = node.run(pool)
  280. resp = result.outputs
  281. assert urlencode({'Redirect': 'http://example2.com'}) in result.process_data.get('request')
  282. assert 'form-data; name="Redirect"\n\nhttp://example6.com' in result.process_data.get('request')
  283. assert 'http://example3.com' == resp.get('headers').get('referer')