requester_test.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. require 'test_helper'
  2. require 'docs'
  3. class DocsRequesterTest < MiniTest::Spec
  4. def stub_request(url)
  5. Typhoeus.stub(url).and_return(Typhoeus::Response.new)
  6. end
  7. let :requester do
  8. Docs::Requester.new(options)
  9. end
  10. let :options do
  11. Hash.new
  12. end
  13. let :url do
  14. 'http://example.com'
  15. end
  16. after do
  17. Typhoeus::Expectation.clear
  18. end
  19. describe ".new" do
  20. it "defaults the :max_concurrency to 20" do
  21. assert_equal 20, Docs::Requester.new.max_concurrency
  22. assert_equal 10, Docs::Requester.new(max_concurrency: 10).max_concurrency
  23. end
  24. it "duplicates and stores :request_options" do
  25. options[:request_options] = { params: 'test' }
  26. assert_equal options[:request_options], requester.request_options
  27. refute_same options[:request_options], requester.request_options
  28. end
  29. end
  30. describe "#request" do
  31. it "returns a request" do
  32. assert_instance_of Docs::Request, requester.request(url)
  33. end
  34. describe "the request" do
  35. it "is queued" do
  36. request = requester.request(url)
  37. assert_includes requester.queued_requests, request
  38. end
  39. it "has the given url" do
  40. request = requester.request(url)
  41. assert_equal url, request.base_url
  42. end
  43. it "has the default :request_options" do
  44. options[:request_options] = { params: 'test' }
  45. request = requester.request(url)
  46. assert_equal 'test', request.options[:params]
  47. end
  48. it "has the given options" do
  49. options[:request_options] = { params: '' }
  50. request = requester.request(url, params: 'test')
  51. assert_equal 'test', request.options[:params]
  52. end
  53. it "has the given block as an on_complete callback" do
  54. block = Proc.new {}
  55. request = requester.request(url, &block)
  56. assert_includes request.on_complete, block
  57. end
  58. end
  59. end
  60. describe "#on_response" do
  61. it "returns an array" do
  62. assert_instance_of Array, requester.on_response
  63. end
  64. it "stores a callback" do
  65. proc = Proc.new {}
  66. requester.on_response(&proc)
  67. assert_includes requester.on_response, proc
  68. end
  69. end
  70. describe "#run" do
  71. before do
  72. stub_request(url)
  73. end
  74. it "calls the #on_response callbacks after each request" do
  75. one = 0; requester.on_response { one += 1 }
  76. two = 0; requester.on_response { two += 2 }
  77. 2.times do |i|
  78. stub_request url = "example.com/#{i}"
  79. requester.request(url)
  80. end
  81. assert_difference 'one', 2 do
  82. assert_difference 'two', 4 do
  83. requester.run
  84. end
  85. end
  86. end
  87. it "passes the response to the #on_response callbacks" do
  88. requester.on_response { |arg| @arg = arg }
  89. request = requester.request(url)
  90. request.run
  91. assert @arg
  92. assert_equal request.response, @arg
  93. end
  94. context "when an #on_response callback returns an array" do
  95. it "requests the urls in the array" do
  96. requester.on_response { ['one', 'two'] }
  97. requester.request(url)
  98. mock(requester).request('one').then.request('two')
  99. requester.run
  100. end
  101. end
  102. end
  103. end