logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

provider_discovery_spec.rb (4247B)


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