user_agent_test.rb 16 KB

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