The code powering m.abunchtell.com https://m.abunchtell.com
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

126 rader
4.1 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. end
  9. describe 'discover_provider' do
  10. context 'when status code is 200 and MIME type is text/html' do
  11. context 'Both of JSON and XML provider are discoverable' do
  12. before do
  13. stub_request(:get, 'https://host.test/oembed.html').to_return(
  14. status: 200,
  15. headers: { 'Content-Type': 'text/html' },
  16. body: request_fixture('oembed_json_xml.html')
  17. )
  18. end
  19. it 'returns new OEmbed::Provider for JSON provider if :format option is set to :json' do
  20. subject.call('https://host.test/oembed.html', format: :json)
  21. expect(subject.endpoint_url).to eq 'https://host.test/provider.json'
  22. expect(subject.format).to eq :json
  23. end
  24. it 'returns new OEmbed::Provider for XML provider if :format option is set to :xml' do
  25. subject.call('https://host.test/oembed.html', format: :xml)
  26. expect(subject.endpoint_url).to eq 'https://host.test/provider.xml'
  27. expect(subject.format).to eq :xml
  28. end
  29. end
  30. context 'JSON provider is discoverable while XML provider is not' do
  31. before do
  32. stub_request(:get, 'https://host.test/oembed.html').to_return(
  33. status: 200,
  34. headers: { 'Content-Type': 'text/html' },
  35. body: request_fixture('oembed_json.html')
  36. )
  37. end
  38. it 'returns new OEmbed::Provider for JSON provider' do
  39. subject.call('https://host.test/oembed.html')
  40. expect(subject.endpoint_url).to eq 'https://host.test/provider.json'
  41. expect(subject.format).to eq :json
  42. end
  43. end
  44. context 'XML provider is discoverable while JSON provider is not' do
  45. before do
  46. stub_request(:get, 'https://host.test/oembed.html').to_return(
  47. status: 200,
  48. headers: { 'Content-Type': 'text/html' },
  49. body: request_fixture('oembed_xml.html')
  50. )
  51. end
  52. it 'returns new OEmbed::Provider for XML provider' do
  53. subject.call('https://host.test/oembed.html')
  54. expect(subject.endpoint_url).to eq 'https://host.test/provider.xml'
  55. expect(subject.format).to eq :xml
  56. end
  57. end
  58. context 'Invalid XML provider is discoverable while JSON 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_invalid_xml.html')
  64. )
  65. end
  66. it 'returns nil' do
  67. expect(subject.call('https://host.test/oembed.html')).to be_nil
  68. end
  69. end
  70. context 'Neither of JSON and XML provider is discoverable' do
  71. before do
  72. stub_request(:get, 'https://host.test/oembed.html').to_return(
  73. status: 200,
  74. headers: { 'Content-Type': 'text/html' },
  75. body: request_fixture('oembed_undiscoverable.html')
  76. )
  77. end
  78. it 'returns nil' do
  79. expect(subject.call('https://host.test/oembed.html')).to be_nil
  80. end
  81. end
  82. end
  83. context 'when status code is not 200' do
  84. before do
  85. stub_request(:get, 'https://host.test/oembed.html').to_return(
  86. status: 400,
  87. headers: { 'Content-Type': 'text/html' },
  88. body: request_fixture('oembed_xml.html')
  89. )
  90. end
  91. it 'returns nil' do
  92. expect(subject.call('https://host.test/oembed.html')).to be_nil
  93. end
  94. end
  95. context 'when MIME type is not text/html' do
  96. before do
  97. stub_request(:get, 'https://host.test/oembed.html').to_return(
  98. status: 200,
  99. body: request_fixture('oembed_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. end
  107. end