fetch_oembed_service_spec.rb 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 'when OEmbed endpoint contains URL as parameter' do
  13. before do
  14. stub_request(:get, 'https://www.youtube.com/watch?v=IPSbNdBmWKE').to_return(
  15. status: 200,
  16. headers: { 'Content-Type': 'text/html' },
  17. body: request_fixture('oembed_youtube.html'),
  18. )
  19. stub_request(:get, 'https://www.youtube.com/oembed?format=json&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DIPSbNdBmWKE').to_return(
  20. status: 200,
  21. headers: { 'Content-Type': 'text/html' },
  22. body: request_fixture('oembed_json_empty.html')
  23. )
  24. end
  25. it 'returns new OEmbed::Provider for JSON provider' do
  26. subject.call('https://www.youtube.com/watch?v=IPSbNdBmWKE')
  27. expect(subject.endpoint_url).to eq 'https://www.youtube.com/oembed?format=json&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DIPSbNdBmWKE'
  28. expect(subject.format).to eq :json
  29. end
  30. it 'stores URL template' do
  31. subject.call('https://www.youtube.com/watch?v=IPSbNdBmWKE')
  32. expect(Rails.cache.read('oembed_endpoint:www.youtube.com')[:endpoint]).to eq 'https://www.youtube.com/oembed?format=json&url={url}'
  33. end
  34. end
  35. context 'Both of JSON and XML provider are discoverable' do
  36. before do
  37. stub_request(:get, 'https://host.test/oembed.html').to_return(
  38. status: 200,
  39. headers: { 'Content-Type': 'text/html' },
  40. body: request_fixture('oembed_json_xml.html')
  41. )
  42. end
  43. it 'returns new OEmbed::Provider for JSON provider if :format option is set to :json' do
  44. subject.call('https://host.test/oembed.html', format: :json)
  45. expect(subject.endpoint_url).to eq 'https://host.test/provider.json'
  46. expect(subject.format).to eq :json
  47. end
  48. it 'returns new OEmbed::Provider for XML provider if :format option is set to :xml' do
  49. subject.call('https://host.test/oembed.html', format: :xml)
  50. expect(subject.endpoint_url).to eq 'https://host.test/provider.xml'
  51. expect(subject.format).to eq :xml
  52. end
  53. it 'does not cache OEmbed endpoint' do
  54. subject.call('https://host.test/oembed.html', format: :xml)
  55. expect(Rails.cache.exist?('oembed_endpoint:host.test')).to eq false
  56. end
  57. end
  58. context 'JSON provider is discoverable while XML provider is not' do
  59. before do
  60. stub_request(:get, 'https://host.test/oembed.html').to_return(
  61. status: 200,
  62. headers: { 'Content-Type': 'text/html' },
  63. body: request_fixture('oembed_json.html')
  64. )
  65. end
  66. it 'returns new OEmbed::Provider for JSON provider' do
  67. subject.call('https://host.test/oembed.html')
  68. expect(subject.endpoint_url).to eq 'https://host.test/provider.json'
  69. expect(subject.format).to eq :json
  70. end
  71. it 'does not cache OEmbed endpoint' do
  72. subject.call('https://host.test/oembed.html')
  73. expect(Rails.cache.exist?('oembed_endpoint:host.test')).to eq false
  74. end
  75. end
  76. context 'XML provider is discoverable while JSON provider is not' do
  77. before do
  78. stub_request(:get, 'https://host.test/oembed.html').to_return(
  79. status: 200,
  80. headers: { 'Content-Type': 'text/html' },
  81. body: request_fixture('oembed_xml.html')
  82. )
  83. end
  84. it 'returns new OEmbed::Provider for XML provider' do
  85. subject.call('https://host.test/oembed.html')
  86. expect(subject.endpoint_url).to eq 'https://host.test/provider.xml'
  87. expect(subject.format).to eq :xml
  88. end
  89. it 'does not cache OEmbed endpoint' do
  90. subject.call('https://host.test/oembed.html')
  91. expect(Rails.cache.exist?('oembed_endpoint:host.test')).to eq false
  92. end
  93. end
  94. context 'Invalid XML provider is discoverable while JSON provider is not' do
  95. before do
  96. stub_request(:get, 'https://host.test/oembed.html').to_return(
  97. status: 200,
  98. headers: { 'Content-Type': 'text/html' },
  99. body: request_fixture('oembed_invalid_xml.html')
  100. )
  101. end
  102. it 'returns nil' do
  103. expect(subject.call('https://host.test/oembed.html')).to be_nil
  104. end
  105. end
  106. context 'Neither of JSON and XML provider is discoverable' do
  107. before do
  108. stub_request(:get, 'https://host.test/oembed.html').to_return(
  109. status: 200,
  110. headers: { 'Content-Type': 'text/html' },
  111. body: request_fixture('oembed_undiscoverable.html')
  112. )
  113. end
  114. it 'returns nil' do
  115. expect(subject.call('https://host.test/oembed.html')).to be_nil
  116. end
  117. end
  118. context 'Empty JSON provider is discoverable' do
  119. before do
  120. stub_request(:get, 'https://host.test/oembed.html').to_return(
  121. status: 200,
  122. headers: { 'Content-Type': 'text/html' },
  123. body: request_fixture('oembed_json_empty.html')
  124. )
  125. end
  126. it 'returns new OEmbed::Provider for JSON provider' do
  127. subject.call('https://host.test/oembed.html')
  128. expect(subject.endpoint_url).to eq 'https://host.test/empty_provider.json'
  129. expect(subject.format).to eq :json
  130. end
  131. end
  132. end
  133. context 'when endpoint is cached' do
  134. before do
  135. stub_request(:get, 'http://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=dqwpQarrDwk').to_return(
  136. status: 200,
  137. headers: { 'Content-Type': 'text/html' },
  138. body: request_fixture('oembed_json_empty.html')
  139. )
  140. end
  141. it 'returns new provider without fetching original URL first' do
  142. subject.call('https://www.youtube.com/watch?v=dqwpQarrDwk', cached_endpoint: { endpoint: 'http://www.youtube.com/oembed?format=json&url={url}', format: :json })
  143. expect(a_request(:get, 'https://www.youtube.com/watch?v=dqwpQarrDwk')).to_not have_been_made
  144. expect(subject.endpoint_url).to eq 'http://www.youtube.com/oembed?format=json&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdqwpQarrDwk'
  145. expect(subject.format).to eq :json
  146. expect(a_request(:get, 'http://www.youtube.com/oembed?format=json&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdqwpQarrDwk')).to have_been_made
  147. end
  148. end
  149. context 'when status code is not 200' do
  150. before do
  151. stub_request(:get, 'https://host.test/oembed.html').to_return(
  152. status: 400,
  153. headers: { 'Content-Type': 'text/html' },
  154. body: request_fixture('oembed_xml.html')
  155. )
  156. end
  157. it 'returns nil' do
  158. expect(subject.call('https://host.test/oembed.html')).to be_nil
  159. end
  160. end
  161. context 'when MIME type is not text/html' do
  162. before do
  163. stub_request(:get, 'https://host.test/oembed.html').to_return(
  164. status: 200,
  165. body: request_fixture('oembed_xml.html')
  166. )
  167. end
  168. it 'returns nil' do
  169. expect(subject.call('https://host.test/oembed.html')).to be_nil
  170. end
  171. end
  172. end
  173. end