fetch_oembed_service_spec.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe FetchOEmbedService, type: :service do
  4. subject { described_class.new }
  5. before do
  6. stub_request(:get, "https://host.test/provider.json").to_return(status: 404)
  7. stub_request(:get, "https://host.test/provider.xml").to_return(status: 404)
  8. stub_request(:get, "https://host.test/empty_provider.json").to_return(status: 200)
  9. end
  10. describe 'discover_provider' do
  11. context 'when status code is 200 and MIME type is text/html' do
  12. context 'Both of JSON and XML provider are discoverable' do
  13. before do
  14. stub_request(:get, 'https://host.test/oembed.html').to_return(
  15. status: 200,
  16. headers: { 'Content-Type': 'text/html' },
  17. body: request_fixture('oembed_json_xml.html')
  18. )
  19. end
  20. it 'returns new OEmbed::Provider for JSON provider if :format option is set to :json' do
  21. subject.call('https://host.test/oembed.html', format: :json)
  22. expect(subject.endpoint_url).to eq 'https://host.test/provider.json'
  23. expect(subject.format).to eq :json
  24. end
  25. it 'returns new OEmbed::Provider for XML provider if :format option is set to :xml' do
  26. subject.call('https://host.test/oembed.html', format: :xml)
  27. expect(subject.endpoint_url).to eq 'https://host.test/provider.xml'
  28. expect(subject.format).to eq :xml
  29. end
  30. end
  31. context 'JSON provider is discoverable while XML provider is not' do
  32. before do
  33. stub_request(:get, 'https://host.test/oembed.html').to_return(
  34. status: 200,
  35. headers: { 'Content-Type': 'text/html' },
  36. body: request_fixture('oembed_json.html')
  37. )
  38. end
  39. it 'returns new OEmbed::Provider for JSON provider' do
  40. subject.call('https://host.test/oembed.html')
  41. expect(subject.endpoint_url).to eq 'https://host.test/provider.json'
  42. expect(subject.format).to eq :json
  43. end
  44. end
  45. context 'XML provider is discoverable while JSON provider is not' do
  46. before do
  47. stub_request(:get, 'https://host.test/oembed.html').to_return(
  48. status: 200,
  49. headers: { 'Content-Type': 'text/html' },
  50. body: request_fixture('oembed_xml.html')
  51. )
  52. end
  53. it 'returns new OEmbed::Provider for XML provider' do
  54. subject.call('https://host.test/oembed.html')
  55. expect(subject.endpoint_url).to eq 'https://host.test/provider.xml'
  56. expect(subject.format).to eq :xml
  57. end
  58. end
  59. context 'Invalid XML provider is discoverable while JSON provider is not' do
  60. before do
  61. stub_request(:get, 'https://host.test/oembed.html').to_return(
  62. status: 200,
  63. headers: { 'Content-Type': 'text/html' },
  64. body: request_fixture('oembed_invalid_xml.html')
  65. )
  66. end
  67. it 'returns nil' do
  68. expect(subject.call('https://host.test/oembed.html')).to be_nil
  69. end
  70. end
  71. context 'Neither of JSON and XML provider is discoverable' do
  72. before do
  73. stub_request(:get, 'https://host.test/oembed.html').to_return(
  74. status: 200,
  75. headers: { 'Content-Type': 'text/html' },
  76. body: request_fixture('oembed_undiscoverable.html')
  77. )
  78. end
  79. it 'returns nil' do
  80. expect(subject.call('https://host.test/oembed.html')).to be_nil
  81. end
  82. end
  83. context 'Empty JSON provider is discoverable' do
  84. before do
  85. stub_request(:get, 'https://host.test/oembed.html').to_return(
  86. status: 200,
  87. headers: { 'Content-Type': 'text/html' },
  88. body: request_fixture('oembed_json_empty.html')
  89. )
  90. end
  91. it 'returns new OEmbed::Provider for JSON provider' do
  92. subject.call('https://host.test/oembed.html')
  93. expect(subject.endpoint_url).to eq 'https://host.test/empty_provider.json'
  94. expect(subject.format).to eq :json
  95. end
  96. end
  97. end
  98. context 'when status code is not 200' do
  99. before do
  100. stub_request(:get, 'https://host.test/oembed.html').to_return(
  101. status: 400,
  102. headers: { 'Content-Type': 'text/html' },
  103. body: request_fixture('oembed_xml.html')
  104. )
  105. end
  106. it 'returns nil' do
  107. expect(subject.call('https://host.test/oembed.html')).to be_nil
  108. end
  109. end
  110. context 'when MIME type is not text/html' do
  111. before do
  112. stub_request(:get, 'https://host.test/oembed.html').to_return(
  113. status: 200,
  114. body: request_fixture('oembed_xml.html')
  115. )
  116. end
  117. it 'returns nil' do
  118. expect(subject.call('https://host.test/oembed.html')).to be_nil
  119. end
  120. end
  121. end
  122. end