response_test.rb 2.5 KB

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