web_request_concern.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. require 'spec_helper'
  2. shared_examples_for WebRequestConcern do
  3. let(:agent) do
  4. _agent = described_class.new(:name => "some agent", :options => @valid_options || {})
  5. _agent.user = users(:jane)
  6. _agent
  7. end
  8. describe "validations" do
  9. it "should be valid" do
  10. expect(agent).to be_valid
  11. end
  12. it "should validate user_agent" do
  13. agent.options['user_agent'] = nil
  14. expect(agent).to be_valid
  15. agent.options['user_agent'] = ""
  16. expect(agent).to be_valid
  17. agent.options['user_agent'] = "foo"
  18. expect(agent).to be_valid
  19. agent.options['user_agent'] = ["foo"]
  20. expect(agent).not_to be_valid
  21. agent.options['user_agent'] = 1
  22. expect(agent).not_to be_valid
  23. end
  24. it "should validate headers" do
  25. agent.options['headers'] = "blah"
  26. expect(agent).not_to be_valid
  27. agent.options['headers'] = ""
  28. expect(agent).to be_valid
  29. agent.options['headers'] = {}
  30. expect(agent).to be_valid
  31. agent.options['headers'] = { 'foo' => 'bar' }
  32. expect(agent).to be_valid
  33. end
  34. it "should validate basic_auth" do
  35. agent.options['basic_auth'] = "foo:bar"
  36. expect(agent).to be_valid
  37. agent.options['basic_auth'] = ["foo", "bar"]
  38. expect(agent).to be_valid
  39. agent.options['basic_auth'] = ""
  40. expect(agent).to be_valid
  41. agent.options['basic_auth'] = nil
  42. expect(agent).to be_valid
  43. agent.options['basic_auth'] = "blah"
  44. expect(agent).not_to be_valid
  45. agent.options['basic_auth'] = ["blah"]
  46. expect(agent).not_to be_valid
  47. end
  48. it "should validate disable_ssl_verification" do
  49. agent.options['disable_ssl_verification'] = nil
  50. expect(agent).to be_valid
  51. agent.options['disable_ssl_verification'] = true
  52. expect(agent).to be_valid
  53. agent.options['disable_ssl_verification'] = false
  54. expect(agent).to be_valid
  55. agent.options['disable_ssl_verification'] = 'true'
  56. expect(agent).to be_valid
  57. agent.options['disable_ssl_verification'] = 'false'
  58. expect(agent).to be_valid
  59. agent.options['disable_ssl_verification'] = 'blah'
  60. expect(agent).not_to be_valid
  61. agent.options['disable_ssl_verification'] = 51
  62. expect(agent).not_to be_valid
  63. end
  64. end
  65. describe "User-Agent" do
  66. before do
  67. @default_http_user_agent = ENV['DEFAULT_HTTP_USER_AGENT']
  68. ENV['DEFAULT_HTTP_USER_AGENT'] = nil
  69. end
  70. after do
  71. ENV['DEFAULT_HTTP_USER_AGENT'] = @default_http_user_agent
  72. end
  73. it "should have the default value of Huginn" do
  74. expect(agent.user_agent).to eq('Huginn - https://github.com/cantino/huginn')
  75. end
  76. it "should be overridden by the environment variable if present" do
  77. ENV['DEFAULT_HTTP_USER_AGENT'] = 'Something'
  78. expect(agent.user_agent).to eq('Something')
  79. end
  80. it "should be overriden by the value in options if present" do
  81. agent.options['user_agent'] = 'Override'
  82. expect(agent.user_agent).to eq('Override')
  83. end
  84. end
  85. describe "#faraday" do
  86. it "should enable SSL verification by default" do
  87. expect(agent.faraday.ssl.verify).to eq(true)
  88. end
  89. it "should enable SSL verification when nil" do
  90. agent.options['disable_ssl_verification'] = nil
  91. expect(agent.faraday.ssl.verify).to eq(true)
  92. end
  93. it "should disable SSL verification if disable_ssl_verification option is 'true'" do
  94. agent.options['disable_ssl_verification'] = 'true'
  95. expect(agent.faraday.ssl.verify).to eq(false)
  96. end
  97. it "should disable SSL verification if disable_ssl_verification option is true" do
  98. agent.options['disable_ssl_verification'] = true
  99. expect(agent.faraday.ssl.verify).to eq(false)
  100. end
  101. it "should not disable SSL verification if disable_ssl_verification option is 'false'" do
  102. agent.options['disable_ssl_verification'] = 'false'
  103. expect(agent.faraday.ssl.verify).to eq(true)
  104. end
  105. it "should not disable SSL verification if disable_ssl_verification option is false" do
  106. agent.options['disable_ssl_verification'] = false
  107. expect(agent.faraday.ssl.verify).to eq(true)
  108. end
  109. end
  110. end