response_test.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. require 'test_helper'
  2. require 'docs'
  3. class DocsResponseTest < MiniTest::Spec
  4. let :response do
  5. Typhoeus::Response.new(options).tap do |response|
  6. response.extend Docs::Response
  7. response.request = request
  8. end
  9. end
  10. let :request do
  11. OpenStruct.new
  12. end
  13. let :options do
  14. OpenStruct.new headers: {}
  15. end
  16. describe "#success?" do
  17. it "returns true when the code is 200" do
  18. options.code = 200
  19. assert response.success?
  20. end
  21. it "returns false when the code is 404" do
  22. options.code = 404
  23. refute response.success?
  24. end
  25. end
  26. describe "#empty?" do
  27. it "returns true when the body is empty" do
  28. options.body = ''
  29. assert response.empty?
  30. end
  31. it "returns false when the body isn't empty" do
  32. options.body = 'body'
  33. refute response.empty?
  34. end
  35. end
  36. describe "#mime_type" do
  37. it "returns the content type" do
  38. options.headers['Content-Type'] = 'type'
  39. assert_equal 'type', response.mime_type
  40. end
  41. it "defaults to text/plain" do
  42. assert_equal 'text/plain', response.mime_type
  43. end
  44. end
  45. describe "#html?" do
  46. it "returns true when the content type is 'text/html'" do
  47. options.headers['Content-Type'] = 'text/html'
  48. assert response.html?
  49. end
  50. it "returns true when the content type is 'application/xhtml'" do
  51. options.headers['Content-Type'] = 'application/xhtml'
  52. assert response.html?
  53. end
  54. it "returns false when the content type is 'text/plain'" do
  55. options.headers['Content-Type'] = 'text/plain'
  56. refute response.html?
  57. end
  58. end
  59. describe "#url" do
  60. before { request.base_url = 'http://example.com' }
  61. it "returns a Docs::URL" do
  62. assert_instance_of Docs::URL, response.url
  63. end
  64. it "returns the #request's base url" do
  65. assert_equal request.base_url, response.url.to_s
  66. end
  67. end
  68. describe "#path" do
  69. it "returns the #url's path" do
  70. request.base_url = 'http://example.com/path'
  71. assert_equal '/path', response.path
  72. end
  73. end
  74. describe "#effective_url" do
  75. before { options.effective_url = 'http://example.com' }
  76. it "returns a Docs::URL" do
  77. assert_instance_of Docs::URL, response.effective_url
  78. end
  79. it "returns the effective url" do
  80. assert_equal options.effective_url, response.effective_url.to_s
  81. end
  82. end
  83. describe "#effective_path" do
  84. it "returns the #effective_url's path" do
  85. options.effective_url = 'http://example.com/path'
  86. assert_equal '/path', response.effective_path
  87. end
  88. end
  89. end