user_agent_test.rb 14 KB

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