client.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. require_relative '../phalapi_client'
  2. require 'test/unit'
  3. # open class
  4. class PhalApi::Client
  5. attr_accessor :host, :service, :filter, :parser, :timeoutMs, :params
  6. end
  7. class ClientTest < Test::Unit::TestCase
  8. def setup
  9. @a_client = PhalApi::Client.create
  10. end
  11. def test_create
  12. assert_instance_of PhalApi::Client, @a_client
  13. end
  14. def test_with_host
  15. @a_client.withHost('http://demo')
  16. assert_equal 'http://demo', @a_client.host
  17. end
  18. def test_with_filter
  19. @a_client.withFilter(PhalApi::ClientFilter.new)
  20. assert_instance_of PhalApi::ClientFilter, @a_client.filter
  21. end
  22. def test_with_parser
  23. @a_client.withParser(PhalApi::ClientParserJson.new)
  24. assert_instance_of PhalApi::ClientParserJson, @a_client.parser
  25. end
  26. def test_with_service
  27. @a_client.withService('Default.Index')
  28. assert_equal 'Default.Index', @a_client.service
  29. end
  30. def test_with_timeoutMs
  31. @a_client.withTimeout(5000)
  32. assert_equal 5000, @a_client.timeoutMs
  33. end
  34. def test_with_more_params
  35. @a_client.withParams('username', 'dogstar')
  36. @a_client.withParams('version', '1.3.0')
  37. @a_client.withParams('age', 100)
  38. params = @a_client.params
  39. assert_equal 'dogstar', params['username']
  40. assert_equal 100, params['age']
  41. end
  42. def test_with_unexpected_params
  43. assert_raise (RuntimeError) {
  44. @a_client.withParams('mess')
  45. }
  46. end
  47. def test_reset
  48. @a_client.withHost('http://demo').withService('User.Move').withParams('id', '10');
  49. @a_client.reset
  50. assert_equal '', @a_client.service
  51. @a_client.withService('Default.Index').withParams('username', 'dogstar')
  52. assert_equal 'Default.Index', @a_client.service
  53. params = @a_client.params
  54. assert_equal 'dogstar', params['username']
  55. assert_equal nil, params['id']
  56. end
  57. def test_base_request
  58. a_response = @a_client.withHost('http://demo.phalapi.net').request
  59. assert_equal 200, a_response.ret
  60. assert_not_nil a_response.data
  61. end
  62. def test_normal_request
  63. a_response = @a_client.withHost('http://demo.phalapi.net').withService('Default.Index').withParams('username', 'dogstar').withParams('v', '1.3.0').request
  64. assert_equal 200, a_response.ret
  65. assert_not_nil a_response.data
  66. end
  67. def test_wrong_request
  68. a_response = @a_client.withHost('http://demo.phalapi.net').withService('XXXX.noThisMethod').request
  69. assert_equal 400, a_response.ret
  70. end
  71. def test_timeout_request
  72. a_response = @a_client.withHost('http://xxx.phalapi.net').withService('Default.Index').request
  73. assert_equal 408, a_response.ret
  74. end
  75. end