test_http.py 9.8 KB

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