test_http.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. 'mask_authorization_header': False,
  41. }
  42. }, **BASIC_NODE_DATA)
  43. result = node.run(pool)
  44. data = result.process_data.get('request', '')
  45. assert '?A=b' in data
  46. assert 'api-key: Basic ak-xxx' in data
  47. assert 'X-Header: 123' in data
  48. @pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
  49. def test_no_auth(setup_http_mock):
  50. node = HttpRequestNode(config={
  51. 'id': '1',
  52. 'data': {
  53. 'title': 'http',
  54. 'desc': '',
  55. 'method': 'get',
  56. 'url': 'http://example.com',
  57. 'authorization': {
  58. 'type': 'no-auth',
  59. 'config': None,
  60. },
  61. 'headers': 'X-Header:123',
  62. 'params': 'A:b',
  63. 'body': None,
  64. }
  65. }, **BASIC_NODE_DATA)
  66. result = node.run(pool)
  67. data = result.process_data.get('request', '')
  68. assert '?A=b' in data
  69. assert 'X-Header: 123' in data
  70. @pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
  71. def test_custom_authorization_header(setup_http_mock):
  72. node = HttpRequestNode(config={
  73. 'id': '1',
  74. 'data': {
  75. 'title': 'http',
  76. 'desc': '',
  77. 'method': 'get',
  78. 'url': 'http://example.com',
  79. 'authorization': {
  80. 'type': 'api-key',
  81. 'config': {
  82. 'type': 'custom',
  83. 'api_key': 'Auth',
  84. 'header': 'X-Auth',
  85. },
  86. },
  87. 'headers': 'X-Header:123',
  88. 'params': 'A:b',
  89. 'body': None,
  90. 'mask_authorization_header': False,
  91. }
  92. }, **BASIC_NODE_DATA)
  93. result = node.run(pool)
  94. data = result.process_data.get('request', '')
  95. assert '?A=b' in data
  96. assert 'X-Header: 123' in data
  97. assert 'X-Auth: Auth' in data
  98. @pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
  99. def test_template(setup_http_mock):
  100. node = HttpRequestNode(config={
  101. 'id': '1',
  102. 'data': {
  103. 'title': 'http',
  104. 'desc': '',
  105. 'method': 'get',
  106. 'url': 'http://example.com/{{#a.b123.args2#}}',
  107. 'authorization': {
  108. 'type': 'api-key',
  109. 'config': {
  110. 'type': 'basic',
  111. 'api_key': 'ak-xxx',
  112. 'header': 'api-key',
  113. }
  114. },
  115. 'headers': 'X-Header:123\nX-Header2:{{#a.b123.args2#}}',
  116. 'params': 'A:b\nTemplate:{{#a.b123.args2#}}',
  117. 'body': None,
  118. 'mask_authorization_header': False,
  119. }
  120. }, **BASIC_NODE_DATA)
  121. result = node.run(pool)
  122. data = result.process_data.get('request', '')
  123. assert '?A=b' in data
  124. assert 'Template=2' in data
  125. assert 'api-key: Basic ak-xxx' in data
  126. assert 'X-Header: 123' in data
  127. assert 'X-Header2: 2' in data
  128. @pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
  129. def test_json(setup_http_mock):
  130. node = HttpRequestNode(config={
  131. 'id': '1',
  132. 'data': {
  133. 'title': 'http',
  134. 'desc': '',
  135. 'method': 'post',
  136. 'url': 'http://example.com',
  137. 'authorization': {
  138. 'type': 'api-key',
  139. 'config': {
  140. 'type': 'basic',
  141. 'api_key': 'ak-xxx',
  142. 'header': 'api-key',
  143. }
  144. },
  145. 'headers': 'X-Header:123',
  146. 'params': 'A:b',
  147. 'body': {
  148. 'type': 'json',
  149. 'data': '{"a": "{{#a.b123.args1#}}"}'
  150. },
  151. 'mask_authorization_header': False,
  152. }
  153. }, **BASIC_NODE_DATA)
  154. result = node.run(pool)
  155. data = result.process_data.get('request', '')
  156. assert '{"a": "1"}' in data
  157. assert 'api-key: Basic ak-xxx' in data
  158. assert 'X-Header: 123' in data
  159. def test_x_www_form_urlencoded(setup_http_mock):
  160. node = HttpRequestNode(config={
  161. 'id': '1',
  162. 'data': {
  163. 'title': 'http',
  164. 'desc': '',
  165. 'method': 'post',
  166. 'url': 'http://example.com',
  167. 'authorization': {
  168. 'type': 'api-key',
  169. 'config': {
  170. 'type': 'basic',
  171. 'api_key': 'ak-xxx',
  172. 'header': 'api-key',
  173. }
  174. },
  175. 'headers': 'X-Header:123',
  176. 'params': 'A:b',
  177. 'body': {
  178. 'type': 'x-www-form-urlencoded',
  179. 'data': 'a:{{#a.b123.args1#}}\nb:{{#a.b123.args2#}}'
  180. },
  181. 'mask_authorization_header': False,
  182. }
  183. }, **BASIC_NODE_DATA)
  184. result = node.run(pool)
  185. data = result.process_data.get('request', '')
  186. assert 'a=1&b=2' in data
  187. assert 'api-key: Basic ak-xxx' in data
  188. assert 'X-Header: 123' in data
  189. def test_form_data(setup_http_mock):
  190. node = HttpRequestNode(config={
  191. 'id': '1',
  192. 'data': {
  193. 'title': 'http',
  194. 'desc': '',
  195. 'method': 'post',
  196. 'url': 'http://example.com',
  197. 'authorization': {
  198. 'type': 'api-key',
  199. 'config': {
  200. 'type': 'basic',
  201. 'api_key': 'ak-xxx',
  202. 'header': 'api-key',
  203. }
  204. },
  205. 'headers': 'X-Header:123',
  206. 'params': 'A:b',
  207. 'body': {
  208. 'type': 'form-data',
  209. 'data': 'a:{{#a.b123.args1#}}\nb:{{#a.b123.args2#}}'
  210. },
  211. 'mask_authorization_header': False,
  212. }
  213. }, **BASIC_NODE_DATA)
  214. result = node.run(pool)
  215. data = result.process_data.get('request', '')
  216. assert 'form-data; name="a"' in data
  217. assert '1' in data
  218. assert 'form-data; name="b"' in data
  219. assert '2' in data
  220. assert 'api-key: Basic ak-xxx' in data
  221. assert 'X-Header: 123' in data
  222. def test_none_data(setup_http_mock):
  223. node = HttpRequestNode(config={
  224. 'id': '1',
  225. 'data': {
  226. 'title': 'http',
  227. 'desc': '',
  228. 'method': 'post',
  229. 'url': 'http://example.com',
  230. 'authorization': {
  231. 'type': 'api-key',
  232. 'config': {
  233. 'type': 'basic',
  234. 'api_key': 'ak-xxx',
  235. 'header': 'api-key',
  236. }
  237. },
  238. 'headers': 'X-Header:123',
  239. 'params': 'A:b',
  240. 'body': {
  241. 'type': 'none',
  242. 'data': '123123123'
  243. },
  244. 'mask_authorization_header': False,
  245. }
  246. }, **BASIC_NODE_DATA)
  247. result = node.run(pool)
  248. data = result.process_data.get('request', '')
  249. assert 'api-key: Basic ak-xxx' in data
  250. assert 'X-Header: 123' in data
  251. assert '123123123' not in data
  252. def test_mock_404(setup_http_mock):
  253. node = HttpRequestNode(config={
  254. 'id': '1',
  255. 'data': {
  256. 'title': 'http',
  257. 'desc': '',
  258. 'method': 'get',
  259. 'url': 'http://404.com',
  260. 'authorization': {
  261. 'type': 'no-auth',
  262. 'config': None,
  263. },
  264. 'body': None,
  265. 'params': '',
  266. 'headers': 'X-Header:123',
  267. 'mask_authorization_header': False,
  268. }
  269. }, **BASIC_NODE_DATA)
  270. result = node.run(pool)
  271. resp = result.outputs
  272. assert 404 == resp.get('status_code')
  273. assert 'Not Found' in resp.get('body')
  274. def test_multi_colons_parse(setup_http_mock):
  275. node = HttpRequestNode(config={
  276. 'id': '1',
  277. 'data': {
  278. 'title': 'http',
  279. 'desc': '',
  280. 'method': 'get',
  281. 'url': 'http://example.com',
  282. 'authorization': {
  283. 'type': 'no-auth',
  284. 'config': None,
  285. },
  286. 'params': 'Referer:http://example1.com\nRedirect:http://example2.com',
  287. 'headers': 'Referer:http://example3.com\nRedirect:http://example4.com',
  288. 'body': {
  289. 'type': 'form-data',
  290. 'data': 'Referer:http://example5.com\nRedirect:http://example6.com'
  291. },
  292. 'mask_authorization_header': False,
  293. }
  294. }, **BASIC_NODE_DATA)
  295. result = node.run(pool)
  296. resp = result.outputs
  297. assert urlencode({'Redirect': 'http://example2.com'}) in result.process_data.get('request')
  298. assert 'form-data; name="Redirect"\n\nhttp://example6.com' in result.process_data.get('request')
  299. assert 'http://example3.com' == resp.get('headers').get('referer')