about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRenato "Lond" Cerqueira <renato@lond.com.br>2019-01-14 17:28:41 +0100
committerEugen Rochko <eugen@zeonfederated.com>2019-01-14 17:28:41 +0100
commit5c5e14c816eb0871344bb69a96bc4bb38e0d3061 (patch)
treeccb3b71f7c2c570530938f61ee375f7414b36592
parentb4e6384aeafc9011707aa27d4948aaa9ca907db3 (diff)
Fix undefined method error in sidekiq (#9807)
* Fix undefined method error in sidekiq

Body can be not nil but still be empty, which causes a
`NoMethodError: undefined method `[]' for nil:NilClass` further in the
code. This checks for an empty body to avoid the issue.

* Fix codeclimate issue
-rw-r--r--app/services/fetch_oembed_service.rb2
-rw-r--r--spec/fixtures/requests/oembed_json_empty.html7
-rw-r--r--spec/services/fetch_oembed_service_spec.rb18
3 files changed, 26 insertions, 1 deletions
diff --git a/app/services/fetch_oembed_service.rb b/app/services/fetch_oembed_service.rb
index 9ddf9b13b..10176cfb9 100644
--- a/app/services/fetch_oembed_service.rb
+++ b/app/services/fetch_oembed_service.rb
@@ -43,7 +43,7 @@ class FetchOEmbedService
       res.code != 200 ? nil : res.body_with_limit
     end
 
-    validate(parse_for_format(body)) unless body.nil?
+    validate(parse_for_format(body)) if body.present?
   rescue Oj::ParseError, Ox::ParseError
     nil
   end
diff --git a/spec/fixtures/requests/oembed_json_empty.html b/spec/fixtures/requests/oembed_json_empty.html
new file mode 100644
index 000000000..4b02413aa
--- /dev/null
+++ b/spec/fixtures/requests/oembed_json_empty.html
@@ -0,0 +1,7 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <link href='https://host.test/empty_provider.json' rel='alternate' type='application/json+oembed'>
+  </head>
+  <body></body>
+</html>
diff --git a/spec/services/fetch_oembed_service_spec.rb b/spec/services/fetch_oembed_service_spec.rb
index 706eb3f2a..5789fb53b 100644
--- a/spec/services/fetch_oembed_service_spec.rb
+++ b/spec/services/fetch_oembed_service_spec.rb
@@ -8,6 +8,7 @@ describe FetchOEmbedService, type: :service do
   before do
     stub_request(:get, "https://host.test/provider.json").to_return(status: 404)
     stub_request(:get, "https://host.test/provider.xml").to_return(status: 404)
+    stub_request(:get, "https://host.test/empty_provider.json").to_return(status: 200)
   end
 
   describe 'discover_provider' do
@@ -93,6 +94,23 @@ describe FetchOEmbedService, type: :service do
           expect(subject.call('https://host.test/oembed.html')).to be_nil
         end
       end
+
+      context 'Empty JSON provider is discoverable' do
+        before do
+          stub_request(:get, 'https://host.test/oembed.html').to_return(
+            status: 200,
+            headers: { 'Content-Type': 'text/html' },
+            body: request_fixture('oembed_json_empty.html')
+          )
+        end
+
+        it 'returns new OEmbed::Provider for JSON provider' do
+          subject.call('https://host.test/oembed.html')
+          expect(subject.endpoint_url).to eq 'https://host.test/empty_provider.json'
+          expect(subject.format).to eq :json
+        end
+      end
+
     end
 
     context 'when status code is not 200' do