web_request_concern.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. require 'rails_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/huginn/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. it "should use faradays default params_encoder" do
  110. expect(agent.faraday.options.params_encoder).to eq(nil)
  111. agent.options['disable_url_encoding'] = 'false'
  112. expect(agent.faraday.options.params_encoder).to eq(nil)
  113. agent.options['disable_url_encoding'] = false
  114. expect(agent.faraday.options.params_encoder).to eq(nil)
  115. end
  116. it "should use WebRequestConcern::DoNotEncoder when disable_url_encoding is truthy" do
  117. agent.options['disable_url_encoding'] = true
  118. expect(agent.faraday.options.params_encoder).to eq(WebRequestConcern::DoNotEncoder)
  119. agent.options['disable_url_encoding'] = 'true'
  120. expect(agent.faraday.options.params_encoder).to eq(WebRequestConcern::DoNotEncoder)
  121. end
  122. describe "redirect follow" do
  123. it "should use FollowRedirects by default" do
  124. expect(agent.faraday.builder.handlers).to include(FaradayMiddleware::FollowRedirects)
  125. end
  126. it "should not use FollowRedirects when disabled" do
  127. agent.options['disable_redirect_follow'] = true
  128. expect(agent.faraday.builder.handlers).not_to include(FaradayMiddleware::FollowRedirects)
  129. end
  130. end
  131. end
  132. describe WebRequestConcern::DoNotEncoder do
  133. it "should not encode special characters" do
  134. expect(WebRequestConcern::DoNotEncoder.encode('GetRss?CategoryNr=39207' => 'test')).to eq('GetRss?CategoryNr=39207=test')
  135. end
  136. it "should work without a value present" do
  137. expect(WebRequestConcern::DoNotEncoder.encode('GetRss?CategoryNr=39207' => nil)).to eq('GetRss?CategoryNr=39207')
  138. end
  139. it "should work without an empty value" do
  140. expect(WebRequestConcern::DoNotEncoder.encode('GetRss?CategoryNr=39207' => '')).to eq('GetRss?CategoryNr=39207=')
  141. end
  142. it "should return the value when decoding" do
  143. expect(WebRequestConcern::DoNotEncoder.decode('val')).to eq(['val'])
  144. end
  145. end
  146. end