闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.8 KiB

  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 endpoint is cached' do
  99. before do
  100. stub_request(:get, 'http://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=dqwpQarrDwk').to_return(
  101. status: 200,
  102. headers: { 'Content-Type': 'text/html' },
  103. body: request_fixture('oembed_json_empty.html')
  104. )
  105. end
  106. it 'returns new provider without fetching original URL first' do
  107. subject.call('https://www.youtube.com/watch?v=dqwpQarrDwk', cached_endpoint: { endpoint: 'http://www.youtube.com/oembed?format=json&url={url}', format: :json })
  108. expect(a_request(:get, 'https://www.youtube.com/watch?v=dqwpQarrDwk')).to_not have_been_made
  109. expect(subject.endpoint_url).to eq 'http://www.youtube.com/oembed?format=json&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdqwpQarrDwk'
  110. expect(subject.format).to eq :json
  111. 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
  112. end
  113. end
  114. context 'when status code is not 200' do
  115. before do
  116. stub_request(:get, 'https://host.test/oembed.html').to_return(
  117. status: 400,
  118. headers: { 'Content-Type': 'text/html' },
  119. body: request_fixture('oembed_xml.html')
  120. )
  121. end
  122. it 'returns nil' do
  123. expect(subject.call('https://host.test/oembed.html')).to be_nil
  124. end
  125. end
  126. context 'when MIME type is not text/html' do
  127. before do
  128. stub_request(:get, 'https://host.test/oembed.html').to_return(
  129. status: 200,
  130. body: request_fixture('oembed_xml.html')
  131. )
  132. end
  133. it 'returns nil' do
  134. expect(subject.call('https://host.test/oembed.html')).to be_nil
  135. end
  136. end
  137. end
  138. end