web_request_concern.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. end
  49. describe "User-Agent" do
  50. before do
  51. @default_http_user_agent = ENV['DEFAULT_HTTP_USER_AGENT']
  52. ENV['DEFAULT_HTTP_USER_AGENT'] = nil
  53. end
  54. after do
  55. ENV['DEFAULT_HTTP_USER_AGENT'] = @default_http_user_agent
  56. end
  57. it "should have the default value set by Faraday" do
  58. expect(agent.user_agent).to eq(Faraday.new.headers[:user_agent])
  59. end
  60. it "should be overridden by the environment variable if present" do
  61. ENV['DEFAULT_HTTP_USER_AGENT'] = 'Huginn - https://github.com/cantino/huginn'
  62. expect(agent.user_agent).to eq('Huginn - https://github.com/cantino/huginn')
  63. end
  64. it "should be overriden by the value in options if present" do
  65. agent.options['user_agent'] = 'Override'
  66. expect(agent.user_agent).to eq('Override')
  67. end
  68. end
  69. end