response_test.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "#error?" do
  27. it "returns false when the code is 200" do
  28. options.code = 200
  29. refute response.error?
  30. end
  31. it "returns false when the code is 404" do
  32. options.code = 404
  33. refute response.error?
  34. end
  35. it "returns true when the code is 400" do
  36. options.code = 400
  37. assert response.error?
  38. end
  39. it "returns true when the code is 500" do
  40. options.code = 500
  41. assert response.error?
  42. end
  43. end
  44. describe "#empty?" do
  45. it "returns true when the body is empty" do
  46. options.body = ''
  47. assert response.empty?
  48. end
  49. it "returns false when the body isn't empty" do
  50. options.body = 'body'
  51. refute response.empty?
  52. end
  53. end
  54. describe "#mime_type" do
  55. it "returns the content type" do
  56. options.headers['Content-Type'] = 'type'
  57. assert_equal 'type', response.mime_type
  58. end
  59. it "defaults to text/plain" do
  60. assert_equal 'text/plain', response.mime_type
  61. end
  62. end
  63. describe "#html?" do
  64. it "returns true when the content type is 'text/html'" do
  65. options.headers['Content-Type'] = 'text/html'
  66. assert response.html?
  67. end
  68. it "returns true when the content type is 'application/xhtml'" do
  69. options.headers['Content-Type'] = 'application/xhtml'
  70. assert response.html?
  71. end
  72. it "returns false when the content type is 'text/plain'" do
  73. options.headers['Content-Type'] = 'text/plain'
  74. refute response.html?
  75. end
  76. end
  77. describe "#url" do
  78. before { request.base_url = 'http://example.com' }
  79. it "returns a Docs::URL" do
  80. assert_instance_of Docs::URL, response.url
  81. end
  82. it "returns the #request's base url" do
  83. assert_equal request.base_url, response.url.to_s
  84. end
  85. end
  86. describe "#path" do
  87. it "returns the #url's path" do
  88. request.base_url = 'http://example.com/path'
  89. assert_equal '/path', response.path
  90. end
  91. end
  92. describe "#effective_url" do
  93. before { options.effective_url = 'http://example.com' }
  94. it "returns a Docs::URL" do
  95. assert_instance_of Docs::URL, response.effective_url
  96. end
  97. it "returns the effective url" do
  98. assert_equal options.effective_url, response.effective_url.to_s
  99. end
  100. end
  101. describe "#effective_path" do
  102. it "returns the #effective_url's path" do
  103. options.effective_url = 'http://example.com/path'
  104. assert_equal '/path', response.effective_path
  105. end
  106. end
  107. end