user_agent_test.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'integration_test_helper'
  3. class UserAgentTest < ActiveSupport::TestCase
  4. host = 'https://web-test.dc.zammad.com'
  5. # host = 'http://127.0.0.1:3003'
  6. setup do
  7. if ENV['ZAMMAD_PROXY_TEST'] == 'true'
  8. Setting.set('proxy', ENV['ZAMMAD_PROXY'])
  9. Setting.set('proxy_username', ENV['ZAMMAD_PROXY_USERNAME'])
  10. Setting.set('proxy_password', ENV['ZAMMAD_PROXY_PASSWORD'])
  11. end
  12. end
  13. # check
  14. test 'check some results' do
  15. # get / 200
  16. result = UserAgent.get(
  17. "#{host}/test/get/1?submitted=123",
  18. )
  19. assert(result)
  20. assert_equal(true, result.success?)
  21. assert_equal('200', result.code)
  22. assert_equal(String, result.body.class)
  23. assert_equal(Hash, result.header.class)
  24. assert_equal('application/json; charset=utf-8', result.header['content-type'])
  25. assert(result.body.include?('"get"'))
  26. assert(result.body.include?('"123"'))
  27. assert(result.body.include?('"content_type_requested":null'))
  28. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  29. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  30. end
  31. # get / 202
  32. result = UserAgent.get(
  33. "#{host}/test/get_accepted/1?submitted=123",
  34. )
  35. assert(result)
  36. assert_equal(true, result.success?)
  37. assert_equal('202', result.code)
  38. assert_equal(String, result.body.class)
  39. assert(result.body.include?('"get"'))
  40. assert(result.body.include?('"123"'))
  41. assert(result.body.include?('"content_type_requested":null'))
  42. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  43. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  44. end
  45. # get / 404
  46. result = UserAgent.get(
  47. "#{host}/test/not_existing",
  48. )
  49. assert(result)
  50. assert_equal(false, result.success?)
  51. assert_equal('404', result.code)
  52. assert_equal(NilClass, result.body.class)
  53. # post / 200
  54. result = UserAgent.post(
  55. "#{host}/test/post/1",
  56. {
  57. submitted: 'some value',
  58. }
  59. )
  60. assert(result)
  61. assert_equal(true, result.success?)
  62. assert_equal('201', result.code)
  63. assert_equal(String, result.body.class)
  64. assert(result.body.include?('"post"'))
  65. assert(result.body.include?('"some value"'))
  66. assert(result.body.include?('"application/x-www-form-urlencoded"'))
  67. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  68. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  69. end
  70. # post / 404
  71. result = UserAgent.post(
  72. "#{host}/test/not_existing",
  73. {
  74. submitted: 'some value',
  75. }
  76. )
  77. assert(result)
  78. assert_equal(false, result.success?)
  79. assert_equal('404', result.code)
  80. assert_equal(NilClass, result.body.class)
  81. # put / 200
  82. result = UserAgent.put(
  83. "#{host}/test/put/1",
  84. {
  85. submitted: 'some value',
  86. }
  87. )
  88. assert(result)
  89. assert_equal(true, result.success?)
  90. assert_equal('200', result.code)
  91. assert_equal(String, result.body.class)
  92. assert(result.body.include?('"put"'))
  93. assert(result.body.include?('"some value"'))
  94. assert(result.body.include?('"application/x-www-form-urlencoded"'))
  95. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  96. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  97. end
  98. # put / 404
  99. result = UserAgent.put(
  100. "#{host}/test/not_existing",
  101. {
  102. submitted: 'some value',
  103. }
  104. )
  105. assert(result)
  106. assert_equal(false, result.success?)
  107. assert_equal('404', result.code)
  108. assert_equal(NilClass, result.body.class)
  109. # delete / 200
  110. result = UserAgent.delete(
  111. "#{host}/test/delete/1",
  112. )
  113. assert(result)
  114. assert_equal(true, result.success?)
  115. assert_equal('200', result.code)
  116. assert_equal(String, result.body.class)
  117. assert(result.body.include?('"delete"'))
  118. assert(result.body.include?('"content_type_requested":null'))
  119. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  120. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  121. end
  122. # delete / 404
  123. result = UserAgent.delete(
  124. "#{host}/test/not_existing",
  125. )
  126. assert(result)
  127. assert_equal(false, result.success?)
  128. assert_equal('404', result.code)
  129. assert_equal(NilClass, result.body.class)
  130. # with http basic auth
  131. # get / 200
  132. result = UserAgent.get(
  133. "#{host}/test_basic_auth/get/1?submitted=123",
  134. {},
  135. {
  136. user: 'basic_auth_user',
  137. password: 'test123',
  138. }
  139. )
  140. assert(result)
  141. assert_equal(true, result.success?)
  142. assert_equal('200', result.code)
  143. assert_equal(String, result.body.class)
  144. assert(result.body.include?('"get"'))
  145. assert(result.body.include?('"123"'))
  146. assert(result.body.include?('"content_type_requested":null'))
  147. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  148. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  149. end
  150. # get / 401
  151. result = UserAgent.get(
  152. "#{host}/test_basic_auth/get/1?submitted=123",
  153. {},
  154. {
  155. user: 'basic_auth_user_not_existing',
  156. password: 'test<>123',
  157. }
  158. )
  159. assert(result)
  160. assert_equal(false, result.success?)
  161. assert_equal('401', result.code)
  162. assert_equal("HTTP Basic: Access denied.\n", result.body)
  163. # post / 200
  164. result = UserAgent.post(
  165. "#{host}/test_basic_auth/post/1",
  166. {
  167. submitted: 'some value',
  168. },
  169. {
  170. user: 'basic_auth_user',
  171. password: 'test123',
  172. }
  173. )
  174. assert(result)
  175. assert_equal(true, result.success?)
  176. assert_equal('201', result.code)
  177. assert_equal(String, result.body.class)
  178. assert(result.body.include?('"post"'))
  179. assert(result.body.include?('"some value"'))
  180. assert(result.body.include?('"application/x-www-form-urlencoded"'))
  181. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  182. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  183. end
  184. # post / 401
  185. result = UserAgent.post(
  186. "#{host}/test_basic_auth/post/1",
  187. {
  188. submitted: 'some value',
  189. },
  190. {
  191. user: 'basic_auth_user_not_existing',
  192. password: 'test<>123',
  193. }
  194. )
  195. assert(result)
  196. assert_equal(false, result.success?)
  197. assert_equal('401', result.code)
  198. assert_equal("HTTP Basic: Access denied.\n", result.body)
  199. # put / 200
  200. result = UserAgent.put(
  201. "#{host}/test_basic_auth/put/1",
  202. {
  203. submitted: 'some value',
  204. },
  205. {
  206. user: 'basic_auth_user',
  207. password: 'test123',
  208. }
  209. )
  210. assert(result)
  211. assert_equal(true, result.success?)
  212. assert_equal('200', result.code)
  213. assert_equal(String, result.body.class)
  214. assert(result.body.include?('"put"'))
  215. assert(result.body.include?('"some value"'))
  216. assert(result.body.include?('"application/x-www-form-urlencoded"'))
  217. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  218. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  219. end
  220. # put / 401
  221. result = UserAgent.put(
  222. "#{host}/test_basic_auth/put/1",
  223. {
  224. submitted: 'some value',
  225. },
  226. {
  227. user: 'basic_auth_user_not_existing',
  228. password: 'test<>123',
  229. }
  230. )
  231. assert(result)
  232. assert_equal(false, result.success?)
  233. assert_equal('401', result.code)
  234. assert_equal("HTTP Basic: Access denied.\n", result.body)
  235. # delete / 200
  236. result = UserAgent.delete(
  237. "#{host}/test_basic_auth/delete/1",
  238. {},
  239. {
  240. user: 'basic_auth_user',
  241. password: 'test123',
  242. }
  243. )
  244. assert(result)
  245. assert_equal(true, result.success?)
  246. assert_equal('200', result.code)
  247. assert_equal(String, result.body.class)
  248. assert(result.body.include?('"delete"'))
  249. assert(result.body.include?('"content_type_requested":null'))
  250. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  251. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  252. end
  253. # delete / 401
  254. result = UserAgent.delete(
  255. "#{host}/test_basic_auth/delete/1",
  256. {},
  257. {
  258. user: 'basic_auth_user_not_existing',
  259. password: 'test<>123',
  260. }
  261. )
  262. assert(result)
  263. assert_equal(false, result.success?)
  264. assert_equal('401', result.code)
  265. assert_equal("HTTP Basic: Access denied.\n", result.body)
  266. end
  267. # check
  268. test 'check redirect' do
  269. # get / 301
  270. result = UserAgent.request(
  271. "#{host}/test/redirect",
  272. )
  273. assert(result)
  274. assert_equal(true, result.success?)
  275. assert_equal('200', result.code)
  276. assert_equal(String, result.body.class)
  277. assert(result.body.include?('"get"'))
  278. assert(result.body.include?('"abc"'))
  279. assert(result.body.include?('"content_type_requested":null'))
  280. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  281. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  282. end
  283. # get / 301
  284. result = UserAgent.request(
  285. "#{host}/test_basic_auth/redirect",
  286. {
  287. user: 'basic_auth_user',
  288. password: 'test123',
  289. }
  290. )
  291. assert(result)
  292. assert_equal(true, result.success?)
  293. assert_equal('200', result.code)
  294. assert_equal(String, result.body.class)
  295. assert(result.body.include?('"get"'))
  296. assert(result.body.include?('"abc"'))
  297. assert(result.body.include?('"content_type_requested":null'))
  298. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  299. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  300. end
  301. # get / 401
  302. result = UserAgent.request(
  303. "#{host}/test_basic_auth/redirect",
  304. {
  305. user: 'basic_auth_user_not_existing',
  306. password: 'test123',
  307. }
  308. )
  309. assert(result)
  310. assert_equal(false, result.success?)
  311. assert_equal('401', result.code)
  312. assert_equal("HTTP Basic: Access denied.\n", result.body)
  313. end
  314. # check
  315. test 'check request' do
  316. # get / 200
  317. result = UserAgent.request(
  318. "#{host}/test/get/1?submitted=123",
  319. )
  320. assert(result)
  321. assert_equal(true, result.success?)
  322. assert_equal('200', result.code)
  323. assert_equal(String, result.body.class)
  324. assert(result.body.include?('"get"'))
  325. assert(result.body.include?('"123"'))
  326. assert(result.body.include?('"content_type_requested":null'))
  327. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  328. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  329. end
  330. # ftp / 200
  331. result = UserAgent.request(
  332. # 'ftp://root.cern.ch/pub/README-root-build.txt',
  333. 'ftp://ftp.gwdg.de/pub/rfc/rfc-index.txt',
  334. )
  335. assert(result)
  336. assert_equal(true, result.success?)
  337. assert_equal('200', result.code)
  338. assert_equal(String, result.body.class)
  339. assert(result.body =~ %r{instructions}i)
  340. # ftp / 401
  341. result = UserAgent.request(
  342. # 'ftp://root.cern.ch/pub/not_existing.msg',
  343. 'ftp://ftp.gwdg.de/pub/rfc/not_existing.txt',
  344. )
  345. assert(result)
  346. assert_equal(false, result.success?)
  347. assert_equal('550', result.code)
  348. assert_equal(NilClass, result.body.class)
  349. # get / 200 / gzip
  350. result = UserAgent.request(
  351. 'https://httpbin.org/gzip',
  352. )
  353. assert(result)
  354. assert_equal(true, result.success?)
  355. assert_equal('200', result.code)
  356. assert_equal(String, result.body.class)
  357. assert(result.body.include?('"GET"'))
  358. # get / 200 / gzip
  359. result = UserAgent.request(
  360. 'http://httpbin.org/gzip',
  361. )
  362. assert(result)
  363. assert_equal(true, result.success?)
  364. assert_equal('200', result.code)
  365. assert_equal(String, result.body.class)
  366. assert(result.body.include?('"GET"'))
  367. # get / 200 / gzip
  368. result = UserAgent.request(
  369. 'https://httpbin.org/deflate',
  370. )
  371. assert(result)
  372. assert_equal(true, result.success?)
  373. assert_equal('200', result.code)
  374. assert_equal(String, result.body.class)
  375. assert(result.body.include?('"GET"'))
  376. # get / 200 / gzip
  377. result = UserAgent.request(
  378. 'http://httpbin.org/deflate',
  379. )
  380. assert(result)
  381. assert_equal(true, result.success?)
  382. assert_equal('200', result.code)
  383. assert_equal(String, result.body.class)
  384. assert(result.body.include?('"GET"'))
  385. end
  386. # check
  387. test 'check not existing' do
  388. # get / 0
  389. result = UserAgent.request(
  390. 'http://not.existing.host.tld/test.php',
  391. )
  392. assert(result)
  393. assert_equal(false, result.success?)
  394. assert_equal(0, result.code)
  395. assert_equal(NilClass, result.body.class)
  396. # ftp / 0
  397. result = UserAgent.request(
  398. 'ftp://not.existing.host.tld/test.bin',
  399. )
  400. assert(result)
  401. assert_equal(false, result.success?)
  402. assert_equal(0, result.code)
  403. assert_equal(NilClass, result.body.class)
  404. end
  405. # check
  406. test 'check timeout' do
  407. # get / timeout
  408. result = UserAgent.get(
  409. "#{host}/test/get/3?submitted=123",
  410. {},
  411. {
  412. open_timeout: 1,
  413. read_timeout: 1,
  414. }
  415. )
  416. assert(result)
  417. assert_equal(false, result.success?)
  418. assert_equal(0, result.code)
  419. assert_equal(NilClass, result.body.class)
  420. # post / timeout
  421. result = UserAgent.post(
  422. "#{host}/test/post/3",
  423. {
  424. submitted: 'timeout',
  425. },
  426. {
  427. open_timeout: 1,
  428. read_timeout: 1,
  429. }
  430. )
  431. assert(result)
  432. assert_equal(false, result.success?)
  433. assert_equal(0, result.code)
  434. assert_equal(NilClass, result.body.class)
  435. end
  436. # check
  437. test 'check json' do
  438. # get / 200
  439. result = UserAgent.get(
  440. "#{host}/test/get/1",
  441. {
  442. submitted: { key: 'some value ' }
  443. },
  444. {
  445. json: true,
  446. }
  447. )
  448. assert(result)
  449. assert_equal(true, result.success?)
  450. assert_equal('200', result.code)
  451. assert_equal(String, result.body.class)
  452. assert(result.body.include?('"content_type_requested"'))
  453. assert(result.body.include?('"application/json"'))
  454. assert_equal('some value ', result.data['submitted']['key'])
  455. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  456. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  457. end
  458. # get / 404
  459. result = UserAgent.get(
  460. "#{host}/test/not_existing",
  461. {
  462. submitted: { key: 'some value ' }
  463. },
  464. {
  465. json: true,
  466. }
  467. )
  468. assert(result)
  469. assert_equal(false, result.success?)
  470. assert_equal('404', result.code)
  471. assert_equal(NilClass, result.body.class)
  472. assert_not(result.data)
  473. # post / 200
  474. result = UserAgent.post(
  475. "#{host}/test/post/1",
  476. {
  477. submitted: { key: 'some value ' }
  478. },
  479. {
  480. json: true,
  481. }
  482. )
  483. assert(result)
  484. assert_equal(true, result.success?)
  485. assert_equal('201', result.code)
  486. assert_equal(String, result.body.class)
  487. assert(result.body.include?('"content_type_requested"'))
  488. assert(result.body.include?('"application/json"'))
  489. assert_equal('some value ', result.data['submitted']['key'])
  490. if ENV['ZAMMAD_PROXY_TEST'] == 'true' && ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']
  491. assert_match(%r{"remote_ip":"#{ENV['ZAMMAD_PROXY_REMOTE_IP_CHECK']}"}, result.body)
  492. end
  493. end
  494. end