request_test.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. require 'test_helper'
  2. require 'docs'
  3. class DocsRequestTest < MiniTest::Spec
  4. let :url do
  5. 'http://example.com'
  6. end
  7. def request(url = self.url, options = {})
  8. Docs::Request.new(url, options).tap do |request|
  9. request.extend FakeInstrumentation
  10. end
  11. end
  12. let :response do
  13. Typhoeus::Response.new.tap do |response|
  14. Typhoeus.stub(url).and_return(response)
  15. end
  16. end
  17. after do
  18. Typhoeus::Expectation.clear
  19. end
  20. describe ".run" do
  21. before { response }
  22. it "makes a request and returns the response" do
  23. assert_equal response, Docs::Request.run(url)
  24. end
  25. it "calls the given block with the response" do
  26. Docs::Request.run(url) { |arg| @arg = arg }
  27. assert_equal response, @arg
  28. end
  29. end
  30. describe ".new" do
  31. it "accepts a Docs::URL" do
  32. url = Docs::URL.parse 'http://example.com'
  33. assert_equal url.to_s, request(url).base_url
  34. end
  35. it "defaults :followlocation to true" do
  36. assert request.options[:followlocation]
  37. refute request(url, followlocation: false).options[:followlocation]
  38. end
  39. end
  40. describe "#run" do
  41. before { response }
  42. it "instruments 'response'" do
  43. req = request.tap(&:run)
  44. assert req.last_instrumentation
  45. assert_equal 'response.request', req.last_instrumentation[:event]
  46. assert_equal url, req.last_instrumentation[:payload][:url]
  47. assert_equal response, req.last_instrumentation[:payload][:response]
  48. end
  49. end
  50. describe "#response=" do
  51. it "extends the object with Docs::Response" do
  52. response = Object.new
  53. request.response = response
  54. assert_includes response.singleton_class.ancestors, Docs::Response
  55. end
  56. end
  57. end